blob: 658049a07aaf7d660ec71868663f1a895348d377 [file] [log] [blame]
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_x86.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010018
Mathieu Chartiere401d142015-04-22 13:56:20 -070019#include "art_method.h"
Guillaume Sanchez0f88e872015-03-30 17:55:45 +010020#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000021#include "compiled_method.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010022#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000023#include "entrypoints/quick/quick_entrypoints_enum.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010024#include "gc/accounting/card_table.h"
Mark Mendell09ed1a32015-03-25 08:30:06 -040025#include "intrinsics.h"
26#include "intrinsics_x86.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070027#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070028#include "mirror/class-inl.h"
Andreas Gamped4901292017-05-30 18:41:34 -070029#include "lock_word.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 //
512 int32_t entry_point_offset =
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100513 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kX86PointerSize>(ref_reg);
Roland Levillaindec8f632016-07-22 17:10:06 +0100514 // This runtime call does not require a stack map.
515 x86_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillain7c1559a2015-12-15 10:55:36 +0000516 __ jmp(GetExitLabel());
517 }
518
519 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100520 // The location (register) of the marked object reference.
521 const Location ref_;
522 // Should the reference in `ref_` be unpoisoned prior to marking it?
523 const bool unpoison_ref_before_marking_;
Roland Levillain7c1559a2015-12-15 10:55:36 +0000524
525 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86);
526};
527
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100528// Slow path marking an object reference `ref` during a read barrier,
529// and if needed, atomically updating the field `obj.field` in the
530// object `obj` holding this reference after marking (contrary to
531// ReadBarrierMarkSlowPathX86 above, which never tries to update
532// `obj.field`).
533//
534// This means that after the execution of this slow path, both `ref`
535// and `obj.field` will be up-to-date; i.e., after the flip, both will
536// hold the same to-space reference (unless another thread installed
537// another object reference (different from `ref`) in `obj.field`).
538class ReadBarrierMarkAndUpdateFieldSlowPathX86 : public SlowPathCode {
539 public:
540 ReadBarrierMarkAndUpdateFieldSlowPathX86(HInstruction* instruction,
541 Location ref,
542 Register obj,
543 const Address& field_addr,
544 bool unpoison_ref_before_marking,
545 Register temp)
546 : SlowPathCode(instruction),
547 ref_(ref),
548 obj_(obj),
549 field_addr_(field_addr),
550 unpoison_ref_before_marking_(unpoison_ref_before_marking),
551 temp_(temp) {
552 DCHECK(kEmitCompilerReadBarrier);
553 }
554
555 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkAndUpdateFieldSlowPathX86"; }
556
557 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
558 LocationSummary* locations = instruction_->GetLocations();
559 Register ref_reg = ref_.AsRegister<Register>();
560 DCHECK(locations->CanCall());
561 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
562 // This slow path is only used by the UnsafeCASObject intrinsic.
563 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
564 << "Unexpected instruction in read barrier marking and field updating slow path: "
565 << instruction_->DebugName();
566 DCHECK(instruction_->GetLocations()->Intrinsified());
567 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
568
569 __ Bind(GetEntryLabel());
570 if (unpoison_ref_before_marking_) {
571 // Object* ref = ref_addr->AsMirrorPtr()
572 __ MaybeUnpoisonHeapReference(ref_reg);
573 }
574
575 // Save the old (unpoisoned) reference.
576 __ movl(temp_, ref_reg);
577
578 // No need to save live registers; it's taken care of by the
579 // entrypoint. Also, there is no need to update the stack mask,
580 // as this runtime call will not trigger a garbage collection.
581 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
582 DCHECK_NE(ref_reg, ESP);
583 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCpuRegisters) << ref_reg;
584 // "Compact" slow path, saving two moves.
585 //
586 // Instead of using the standard runtime calling convention (input
587 // and output in EAX):
588 //
589 // EAX <- ref
590 // EAX <- ReadBarrierMark(EAX)
591 // ref <- EAX
592 //
593 // we just use rX (the register containing `ref`) as input and output
594 // of a dedicated entrypoint:
595 //
596 // rX <- ReadBarrierMarkRegX(rX)
597 //
598 int32_t entry_point_offset =
599 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kX86PointerSize>(ref_reg);
600 // This runtime call does not require a stack map.
601 x86_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
602
603 // If the new reference is different from the old reference,
604 // update the field in the holder (`*field_addr`).
605 //
606 // Note that this field could also hold a different object, if
607 // another thread had concurrently changed it. In that case, the
608 // LOCK CMPXCHGL instruction in the compare-and-set (CAS)
609 // operation below would abort the CAS, leaving the field as-is.
610 NearLabel done;
611 __ cmpl(temp_, ref_reg);
612 __ j(kEqual, &done);
613
614 // Update the the holder's field atomically. This may fail if
615 // mutator updates before us, but it's OK. This is achieved
616 // using a strong compare-and-set (CAS) operation with relaxed
617 // memory synchronization ordering, where the expected value is
618 // the old reference and the desired value is the new reference.
619 // This operation is implemented with a 32-bit LOCK CMPXLCHG
620 // instruction, which requires the expected value (the old
621 // reference) to be in EAX. Save EAX beforehand, and move the
622 // expected value (stored in `temp_`) into EAX.
623 __ pushl(EAX);
624 __ movl(EAX, temp_);
625
626 // Convenience aliases.
627 Register base = obj_;
628 Register expected = EAX;
629 Register value = ref_reg;
630
631 bool base_equals_value = (base == value);
632 if (kPoisonHeapReferences) {
633 if (base_equals_value) {
634 // If `base` and `value` are the same register location, move
635 // `value` to a temporary register. This way, poisoning
636 // `value` won't invalidate `base`.
637 value = temp_;
638 __ movl(value, base);
639 }
640
641 // Check that the register allocator did not assign the location
642 // of `expected` (EAX) to `value` nor to `base`, so that heap
643 // poisoning (when enabled) works as intended below.
644 // - If `value` were equal to `expected`, both references would
645 // be poisoned twice, meaning they would not be poisoned at
646 // all, as heap poisoning uses address negation.
647 // - If `base` were equal to `expected`, poisoning `expected`
648 // would invalidate `base`.
649 DCHECK_NE(value, expected);
650 DCHECK_NE(base, expected);
651
652 __ PoisonHeapReference(expected);
653 __ PoisonHeapReference(value);
654 }
655
656 __ LockCmpxchgl(field_addr_, value);
657
658 // If heap poisoning is enabled, we need to unpoison the values
659 // that were poisoned earlier.
660 if (kPoisonHeapReferences) {
661 if (base_equals_value) {
662 // `value` has been moved to a temporary register, no need
663 // to unpoison it.
664 } else {
665 __ UnpoisonHeapReference(value);
666 }
667 // No need to unpoison `expected` (EAX), as it is be overwritten below.
668 }
669
670 // Restore EAX.
671 __ popl(EAX);
672
673 __ Bind(&done);
674 __ jmp(GetExitLabel());
675 }
676
677 private:
678 // The location (register) of the marked object reference.
679 const Location ref_;
680 // The register containing the object holding the marked object reference field.
681 const Register obj_;
682 // The address of the marked reference field. The base of this address must be `obj_`.
683 const Address field_addr_;
684
685 // Should the reference in `ref_` be unpoisoned prior to marking it?
686 const bool unpoison_ref_before_marking_;
687
688 const Register temp_;
689
690 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathX86);
691};
692
Roland Levillain0d5a2812015-11-13 10:07:31 +0000693// Slow path generating a read barrier for a heap reference.
694class ReadBarrierForHeapReferenceSlowPathX86 : public SlowPathCode {
695 public:
696 ReadBarrierForHeapReferenceSlowPathX86(HInstruction* instruction,
697 Location out,
698 Location ref,
699 Location obj,
700 uint32_t offset,
701 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000702 : SlowPathCode(instruction),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000703 out_(out),
704 ref_(ref),
705 obj_(obj),
706 offset_(offset),
707 index_(index) {
708 DCHECK(kEmitCompilerReadBarrier);
709 // If `obj` is equal to `out` or `ref`, it means the initial object
710 // has been overwritten by (or after) the heap object reference load
711 // to be instrumented, e.g.:
712 //
713 // __ movl(out, Address(out, offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000714 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000715 //
716 // In that case, we have lost the information about the original
717 // object, and the emitted read barrier cannot work properly.
718 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
719 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
720 }
721
722 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
723 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
724 LocationSummary* locations = instruction_->GetLocations();
725 Register reg_out = out_.AsRegister<Register>();
726 DCHECK(locations->CanCall());
727 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain3d312422016-06-23 13:53:42 +0100728 DCHECK(instruction_->IsInstanceFieldGet() ||
729 instruction_->IsStaticFieldGet() ||
730 instruction_->IsArrayGet() ||
731 instruction_->IsInstanceOf() ||
732 instruction_->IsCheckCast() ||
Andreas Gamped9911ee2017-03-27 13:27:24 -0700733 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain7c1559a2015-12-15 10:55:36 +0000734 << "Unexpected instruction in read barrier for heap reference slow path: "
735 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000736
737 __ Bind(GetEntryLabel());
738 SaveLiveRegisters(codegen, locations);
739
740 // We may have to change the index's value, but as `index_` is a
741 // constant member (like other "inputs" of this slow path),
742 // introduce a copy of it, `index`.
743 Location index = index_;
744 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100745 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain0d5a2812015-11-13 10:07:31 +0000746 if (instruction_->IsArrayGet()) {
747 // Compute the actual memory offset and store it in `index`.
748 Register index_reg = index_.AsRegister<Register>();
749 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
750 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
751 // We are about to change the value of `index_reg` (see the
752 // calls to art::x86::X86Assembler::shll and
753 // art::x86::X86Assembler::AddImmediate below), but it has
754 // not been saved by the previous call to
755 // art::SlowPathCode::SaveLiveRegisters, as it is a
756 // callee-save register --
757 // art::SlowPathCode::SaveLiveRegisters does not consider
758 // callee-save registers, as it has been designed with the
759 // assumption that callee-save registers are supposed to be
760 // handled by the called function. So, as a callee-save
761 // register, `index_reg` _would_ eventually be saved onto
762 // the stack, but it would be too late: we would have
763 // changed its value earlier. Therefore, we manually save
764 // it here into another freely available register,
765 // `free_reg`, chosen of course among the caller-save
766 // registers (as a callee-save `free_reg` register would
767 // exhibit the same problem).
768 //
769 // Note we could have requested a temporary register from
770 // the register allocator instead; but we prefer not to, as
771 // this is a slow path, and we know we can find a
772 // caller-save register that is available.
773 Register free_reg = FindAvailableCallerSaveRegister(codegen);
774 __ movl(free_reg, index_reg);
775 index_reg = free_reg;
776 index = Location::RegisterLocation(index_reg);
777 } else {
778 // The initial register stored in `index_` has already been
779 // saved in the call to art::SlowPathCode::SaveLiveRegisters
780 // (as it is not a callee-save register), so we can freely
781 // use it.
782 }
783 // Shifting the index value contained in `index_reg` by the scale
784 // factor (2) cannot overflow in practice, as the runtime is
785 // unable to allocate object arrays with a size larger than
786 // 2^26 - 1 (that is, 2^28 - 4 bytes).
787 __ shll(index_reg, Immediate(TIMES_4));
788 static_assert(
789 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
790 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
791 __ AddImmediate(index_reg, Immediate(offset_));
792 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100793 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
794 // intrinsics, `index_` is not shifted by a scale factor of 2
795 // (as in the case of ArrayGet), as it is actually an offset
796 // to an object field within an object.
797 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000798 DCHECK(instruction_->GetLocations()->Intrinsified());
799 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
800 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
801 << instruction_->AsInvoke()->GetIntrinsic();
802 DCHECK_EQ(offset_, 0U);
803 DCHECK(index_.IsRegisterPair());
804 // UnsafeGet's offset location is a register pair, the low
805 // part contains the correct offset.
806 index = index_.ToLow();
807 }
808 }
809
810 // We're moving two or three locations to locations that could
811 // overlap, so we need a parallel move resolver.
812 InvokeRuntimeCallingConvention calling_convention;
813 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
814 parallel_move.AddMove(ref_,
815 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
816 Primitive::kPrimNot,
817 nullptr);
818 parallel_move.AddMove(obj_,
819 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
820 Primitive::kPrimNot,
821 nullptr);
822 if (index.IsValid()) {
823 parallel_move.AddMove(index,
824 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
825 Primitive::kPrimInt,
826 nullptr);
827 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
828 } else {
829 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
830 __ movl(calling_convention.GetRegisterAt(2), Immediate(offset_));
831 }
Serban Constantinescuba45db02016-07-12 22:53:02 +0100832 x86_codegen->InvokeRuntime(kQuickReadBarrierSlow, instruction_, instruction_->GetDexPc(), this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000833 CheckEntrypointTypes<
834 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
835 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
836
837 RestoreLiveRegisters(codegen, locations);
838 __ jmp(GetExitLabel());
839 }
840
841 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathX86"; }
842
843 private:
844 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
845 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
846 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
847 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
848 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
849 return static_cast<Register>(i);
850 }
851 }
852 // We shall never fail to find a free caller-save register, as
853 // there are more than two core caller-save registers on x86
854 // (meaning it is possible to find one which is different from
855 // `ref` and `obj`).
856 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
857 LOG(FATAL) << "Could not find a free caller-save register";
858 UNREACHABLE();
859 }
860
Roland Levillain0d5a2812015-11-13 10:07:31 +0000861 const Location out_;
862 const Location ref_;
863 const Location obj_;
864 const uint32_t offset_;
865 // An additional location containing an index to an array.
866 // Only used for HArrayGet and the UnsafeGetObject &
867 // UnsafeGetObjectVolatile intrinsics.
868 const Location index_;
869
870 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86);
871};
872
873// Slow path generating a read barrier for a GC root.
874class ReadBarrierForRootSlowPathX86 : public SlowPathCode {
875 public:
876 ReadBarrierForRootSlowPathX86(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000877 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000878 DCHECK(kEmitCompilerReadBarrier);
879 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000880
881 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
882 LocationSummary* locations = instruction_->GetLocations();
883 Register reg_out = out_.AsRegister<Register>();
884 DCHECK(locations->CanCall());
885 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000886 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
887 << "Unexpected instruction in read barrier for GC root slow path: "
888 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000889
890 __ Bind(GetEntryLabel());
891 SaveLiveRegisters(codegen, locations);
892
893 InvokeRuntimeCallingConvention calling_convention;
894 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
895 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100896 x86_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000897 instruction_,
898 instruction_->GetDexPc(),
899 this);
900 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
901 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
902
903 RestoreLiveRegisters(codegen, locations);
904 __ jmp(GetExitLabel());
905 }
906
907 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathX86"; }
908
909 private:
Roland Levillain0d5a2812015-11-13 10:07:31 +0000910 const Location out_;
911 const Location root_;
912
913 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86);
914};
915
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100916#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100917// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
918#define __ down_cast<X86Assembler*>(GetAssembler())-> // NOLINT
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100919
Aart Bike9f37602015-10-09 11:15:55 -0700920inline Condition X86Condition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700921 switch (cond) {
922 case kCondEQ: return kEqual;
923 case kCondNE: return kNotEqual;
924 case kCondLT: return kLess;
925 case kCondLE: return kLessEqual;
926 case kCondGT: return kGreater;
927 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700928 case kCondB: return kBelow;
929 case kCondBE: return kBelowEqual;
930 case kCondA: return kAbove;
931 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700932 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100933 LOG(FATAL) << "Unreachable";
934 UNREACHABLE();
935}
936
Aart Bike9f37602015-10-09 11:15:55 -0700937// Maps signed condition to unsigned condition and FP condition to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100938inline Condition X86UnsignedOrFPCondition(IfCondition cond) {
939 switch (cond) {
940 case kCondEQ: return kEqual;
941 case kCondNE: return kNotEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700942 // Signed to unsigned, and FP to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100943 case kCondLT: return kBelow;
944 case kCondLE: return kBelowEqual;
945 case kCondGT: return kAbove;
946 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700947 // Unsigned remain unchanged.
948 case kCondB: return kBelow;
949 case kCondBE: return kBelowEqual;
950 case kCondA: return kAbove;
951 case kCondAE: return kAboveEqual;
Roland Levillain4fa13f62015-07-06 18:11:54 +0100952 }
953 LOG(FATAL) << "Unreachable";
954 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700955}
956
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100957void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100958 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100959}
960
961void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100962 stream << XmmRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100963}
964
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100965size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
966 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
967 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100968}
969
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100970size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
971 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
972 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100973}
974
Mark Mendell7c8d0092015-01-26 11:21:33 -0500975size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Aart Bikb13c65b2017-03-21 20:14:07 -0700976 if (GetGraph()->HasSIMD()) {
Aart Bik5576f372017-03-23 16:17:37 -0700977 __ movups(Address(ESP, stack_index), XmmRegister(reg_id));
Aart Bikb13c65b2017-03-21 20:14:07 -0700978 } else {
979 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
980 }
Mark Mendell7c8d0092015-01-26 11:21:33 -0500981 return GetFloatingPointSpillSlotSize();
982}
983
984size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Aart Bikb13c65b2017-03-21 20:14:07 -0700985 if (GetGraph()->HasSIMD()) {
Aart Bik5576f372017-03-23 16:17:37 -0700986 __ movups(XmmRegister(reg_id), Address(ESP, stack_index));
Aart Bikb13c65b2017-03-21 20:14:07 -0700987 } else {
988 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
989 }
Mark Mendell7c8d0092015-01-26 11:21:33 -0500990 return GetFloatingPointSpillSlotSize();
991}
992
Calin Juravle175dc732015-08-25 15:42:32 +0100993void CodeGeneratorX86::InvokeRuntime(QuickEntrypointEnum entrypoint,
994 HInstruction* instruction,
995 uint32_t dex_pc,
996 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +0100997 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100998 GenerateInvokeRuntime(GetThreadOffset<kX86PointerSize>(entrypoint).Int32Value());
999 if (EntrypointRequiresStackMap(entrypoint)) {
1000 RecordPcInfo(instruction, dex_pc, slow_path);
1001 }
Alexandre Rames8158f282015-08-07 10:26:17 +01001002}
1003
Roland Levillaindec8f632016-07-22 17:10:06 +01001004void CodeGeneratorX86::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1005 HInstruction* instruction,
1006 SlowPathCode* slow_path) {
1007 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +01001008 GenerateInvokeRuntime(entry_point_offset);
1009}
1010
1011void CodeGeneratorX86::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001012 __ fs()->call(Address::Absolute(entry_point_offset));
1013}
1014
Mark Mendellfb8d2792015-03-31 22:16:59 -04001015CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +00001016 const X86InstructionSetFeatures& isa_features,
1017 const CompilerOptions& compiler_options,
1018 OptimizingCompilerStats* stats)
Mark Mendell5f874182015-03-04 15:42:45 -05001019 : CodeGenerator(graph,
1020 kNumberOfCpuRegisters,
1021 kNumberOfXmmRegisters,
1022 kNumberOfRegisterPairs,
1023 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1024 arraysize(kCoreCalleeSaves))
1025 | (1 << kFakeReturnRegister),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001026 0,
1027 compiler_options,
1028 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +01001029 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001030 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001031 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -04001032 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001033 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +00001034 isa_features_(isa_features),
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001035 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko65979462017-05-19 17:25:12 +01001036 boot_image_method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +00001037 boot_image_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1038 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko65979462017-05-19 17:25:12 +01001039 string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001040 jit_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001041 jit_class_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko93205e32016-04-13 11:59:46 +01001042 constant_area_start_(-1),
1043 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001044 method_address_offset_(std::less<uint32_t>(),
1045 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001046 // Use a fake return address register to mimic Quick.
1047 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001048}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001049
David Brazdil58282f42016-01-14 12:45:10 +00001050void CodeGeneratorX86::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001051 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001052 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001053}
1054
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001055InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001056 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001057 assembler_(codegen->GetAssembler()),
1058 codegen_(codegen) {}
1059
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001060static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001061 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001062}
1063
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001064void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001065 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001066 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +00001067 bool skip_overflow_check =
1068 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001069 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +00001070
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001071 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +01001072 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001073 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +01001074 }
1075
Mark Mendell5f874182015-03-04 15:42:45 -05001076 if (HasEmptyFrame()) {
1077 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001078 }
Mark Mendell5f874182015-03-04 15:42:45 -05001079
1080 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
1081 Register reg = kCoreCalleeSaves[i];
1082 if (allocated_registers_.ContainsCoreRegister(reg)) {
1083 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001084 __ cfi().AdjustCFAOffset(kX86WordSize);
1085 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -05001086 }
1087 }
1088
Mingyao Yang063fc772016-08-02 11:02:54 -07001089 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1090 // Initialize should_deoptimize flag to 0.
1091 __ movl(Address(ESP, -kShouldDeoptimizeFlagSize), Immediate(0));
1092 }
1093
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001094 int adjust = GetFrameSize() - FrameEntrySpillSize();
1095 __ subl(ESP, Immediate(adjust));
1096 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001097 // Save the current method if we need it. Note that we do not
1098 // do this in HCurrentMethod, as the instruction might have been removed
1099 // in the SSA graph.
1100 if (RequiresCurrentMethod()) {
1101 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
1102 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001103}
1104
1105void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001106 __ cfi().RememberState();
1107 if (!HasEmptyFrame()) {
1108 int adjust = GetFrameSize() - FrameEntrySpillSize();
1109 __ addl(ESP, Immediate(adjust));
1110 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -05001111
David Srbeckyc34dc932015-04-12 09:27:43 +01001112 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1113 Register reg = kCoreCalleeSaves[i];
1114 if (allocated_registers_.ContainsCoreRegister(reg)) {
1115 __ popl(reg);
1116 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
1117 __ cfi().Restore(DWARFReg(reg));
1118 }
Mark Mendell5f874182015-03-04 15:42:45 -05001119 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001120 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001121 __ ret();
1122 __ cfi().RestoreState();
1123 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001124}
1125
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001126void CodeGeneratorX86::Bind(HBasicBlock* block) {
1127 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001128}
1129
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001130Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(Primitive::Type type) const {
1131 switch (type) {
1132 case Primitive::kPrimBoolean:
1133 case Primitive::kPrimByte:
1134 case Primitive::kPrimChar:
1135 case Primitive::kPrimShort:
1136 case Primitive::kPrimInt:
1137 case Primitive::kPrimNot:
1138 return Location::RegisterLocation(EAX);
1139
1140 case Primitive::kPrimLong:
1141 return Location::RegisterPairLocation(EAX, EDX);
1142
1143 case Primitive::kPrimVoid:
1144 return Location::NoLocation();
1145
1146 case Primitive::kPrimDouble:
1147 case Primitive::kPrimFloat:
1148 return Location::FpuRegisterLocation(XMM0);
1149 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001150
1151 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001152}
1153
1154Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
1155 return Location::RegisterLocation(kMethodRegisterArgument);
1156}
1157
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001158Location InvokeDexCallingConventionVisitorX86::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001159 switch (type) {
1160 case Primitive::kPrimBoolean:
1161 case Primitive::kPrimByte:
1162 case Primitive::kPrimChar:
1163 case Primitive::kPrimShort:
1164 case Primitive::kPrimInt:
1165 case Primitive::kPrimNot: {
1166 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001167 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001168 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001169 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001170 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001171 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001172 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001173 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001174
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001175 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001176 uint32_t index = gp_index_;
1177 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001178 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001179 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001180 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
1181 calling_convention.GetRegisterPairAt(index));
1182 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001183 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001184 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1185 }
1186 }
1187
1188 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001189 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001190 stack_index_++;
1191 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1192 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1193 } else {
1194 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1195 }
1196 }
1197
1198 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001199 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001200 stack_index_ += 2;
1201 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1202 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1203 } else {
1204 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001205 }
1206 }
1207
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001208 case Primitive::kPrimVoid:
1209 LOG(FATAL) << "Unexpected parameter type " << type;
1210 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001211 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00001212 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001213}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001214
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001215void CodeGeneratorX86::Move32(Location destination, Location source) {
1216 if (source.Equals(destination)) {
1217 return;
1218 }
1219 if (destination.IsRegister()) {
1220 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001221 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001222 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001223 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001224 } else {
1225 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001226 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001227 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001228 } else if (destination.IsFpuRegister()) {
1229 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001230 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001231 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001232 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001233 } else {
1234 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001235 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001236 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001237 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001238 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001239 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001240 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001241 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001242 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05001243 } else if (source.IsConstant()) {
1244 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001245 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05001246 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001247 } else {
1248 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001249 __ pushl(Address(ESP, source.GetStackIndex()));
1250 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001251 }
1252 }
1253}
1254
1255void CodeGeneratorX86::Move64(Location destination, Location source) {
1256 if (source.Equals(destination)) {
1257 return;
1258 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001259 if (destination.IsRegisterPair()) {
1260 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001261 EmitParallelMoves(
1262 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1263 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001264 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001265 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001266 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1267 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001268 } else if (source.IsFpuRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001269 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
1270 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
1271 __ psrlq(src_reg, Immediate(32));
1272 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001273 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001274 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001275 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001276 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
1277 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001278 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
1279 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001280 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05001281 if (source.IsFpuRegister()) {
1282 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
1283 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001284 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001285 } else if (source.IsRegisterPair()) {
1286 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
1287 // Create stack space for 2 elements.
1288 __ subl(ESP, Immediate(2 * elem_size));
1289 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
1290 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
1291 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
1292 // And remove the temporary stack space we allocated.
1293 __ addl(ESP, Immediate(2 * elem_size));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001294 } else {
1295 LOG(FATAL) << "Unimplemented";
1296 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001297 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001298 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001299 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001300 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001301 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001302 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001303 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001304 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001305 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001306 } else if (source.IsConstant()) {
1307 HConstant* constant = source.GetConstant();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001308 DCHECK(constant->IsLongConstant() || constant->IsDoubleConstant());
1309 int64_t value = GetInt64ValueOf(constant);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001310 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001311 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
1312 Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001313 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001314 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001315 EmitParallelMoves(
1316 Location::StackSlot(source.GetStackIndex()),
1317 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001318 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001319 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001320 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
1321 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001322 }
1323 }
1324}
1325
Calin Juravle175dc732015-08-25 15:42:32 +01001326void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
1327 DCHECK(location.IsRegister());
1328 __ movl(location.AsRegister<Register>(), Immediate(value));
1329}
1330
Calin Juravlee460d1d2015-09-29 04:52:17 +01001331void CodeGeneratorX86::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001332 HParallelMove move(GetGraph()->GetArena());
1333 if (dst_type == Primitive::kPrimLong && !src.IsConstant() && !src.IsFpuRegister()) {
1334 move.AddMove(src.ToLow(), dst.ToLow(), Primitive::kPrimInt, nullptr);
1335 move.AddMove(src.ToHigh(), dst.ToHigh(), Primitive::kPrimInt, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001336 } else {
David Brazdil74eb1b22015-12-14 11:44:01 +00001337 move.AddMove(src, dst, dst_type, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001338 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001339 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001340}
1341
1342void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1343 if (location.IsRegister()) {
1344 locations->AddTemp(location);
1345 } else if (location.IsRegisterPair()) {
1346 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1347 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1348 } else {
1349 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1350 }
1351}
1352
David Brazdilfc6a86a2015-06-26 10:33:45 +00001353void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001354 DCHECK(!successor->IsExitBlock());
1355
1356 HBasicBlock* block = got->GetBlock();
1357 HInstruction* previous = got->GetPrevious();
1358
1359 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001360 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001361 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1362 return;
1363 }
1364
1365 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1366 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1367 }
1368 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001369 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001370 }
1371}
1372
David Brazdilfc6a86a2015-06-26 10:33:45 +00001373void LocationsBuilderX86::VisitGoto(HGoto* got) {
1374 got->SetLocations(nullptr);
1375}
1376
1377void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1378 HandleGoto(got, got->GetSuccessor());
1379}
1380
1381void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1382 try_boundary->SetLocations(nullptr);
1383}
1384
1385void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1386 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1387 if (!successor->IsExitBlock()) {
1388 HandleGoto(try_boundary, successor);
1389 }
1390}
1391
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001392void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001393 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001394}
1395
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001396void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001397}
1398
Mark Mendell152408f2015-12-31 12:28:50 -05001399template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001400void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001401 LabelType* true_label,
1402 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001403 if (cond->IsFPConditionTrueIfNaN()) {
1404 __ j(kUnordered, true_label);
1405 } else if (cond->IsFPConditionFalseIfNaN()) {
1406 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001407 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001408 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001409}
1410
Mark Mendell152408f2015-12-31 12:28:50 -05001411template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001412void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001413 LabelType* true_label,
1414 LabelType* false_label) {
Mark Mendellc4701932015-04-10 13:18:51 -04001415 LocationSummary* locations = cond->GetLocations();
1416 Location left = locations->InAt(0);
1417 Location right = locations->InAt(1);
1418 IfCondition if_cond = cond->GetCondition();
1419
Mark Mendellc4701932015-04-10 13:18:51 -04001420 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001421 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001422 IfCondition true_high_cond = if_cond;
1423 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001424 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001425
1426 // Set the conditions for the test, remembering that == needs to be
1427 // decided using the low words.
1428 switch (if_cond) {
1429 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001430 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001431 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001432 break;
1433 case kCondLT:
1434 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001435 break;
1436 case kCondLE:
1437 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001438 break;
1439 case kCondGT:
1440 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001441 break;
1442 case kCondGE:
1443 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001444 break;
Aart Bike9f37602015-10-09 11:15:55 -07001445 case kCondB:
1446 false_high_cond = kCondA;
1447 break;
1448 case kCondBE:
1449 true_high_cond = kCondB;
1450 break;
1451 case kCondA:
1452 false_high_cond = kCondB;
1453 break;
1454 case kCondAE:
1455 true_high_cond = kCondA;
1456 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001457 }
1458
1459 if (right.IsConstant()) {
1460 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001461 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001462 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001463
Aart Bika19616e2016-02-01 18:57:58 -08001464 codegen_->Compare32BitValue(left_high, val_high);
Mark Mendellc4701932015-04-10 13:18:51 -04001465 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001466 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001467 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001468 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001469 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001470 __ j(X86Condition(true_high_cond), true_label);
1471 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001472 }
1473 // Must be equal high, so compare the lows.
Aart Bika19616e2016-02-01 18:57:58 -08001474 codegen_->Compare32BitValue(left_low, val_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001475 } else if (right.IsRegisterPair()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001476 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001477 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001478
1479 __ cmpl(left_high, right_high);
1480 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001481 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001482 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001483 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001484 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001485 __ j(X86Condition(true_high_cond), true_label);
1486 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001487 }
1488 // Must be equal high, so compare the lows.
1489 __ cmpl(left_low, right_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001490 } else {
1491 DCHECK(right.IsDoubleStackSlot());
1492 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
1493 if (if_cond == kCondNE) {
1494 __ j(X86Condition(true_high_cond), true_label);
1495 } else if (if_cond == kCondEQ) {
1496 __ j(X86Condition(false_high_cond), false_label);
1497 } else {
1498 __ j(X86Condition(true_high_cond), true_label);
1499 __ j(X86Condition(false_high_cond), false_label);
1500 }
1501 // Must be equal high, so compare the lows.
1502 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Mark Mendellc4701932015-04-10 13:18:51 -04001503 }
1504 // The last comparison might be unsigned.
1505 __ j(final_condition, true_label);
1506}
1507
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001508void InstructionCodeGeneratorX86::GenerateFPCompare(Location lhs,
1509 Location rhs,
1510 HInstruction* insn,
1511 bool is_double) {
1512 HX86LoadFromConstantTable* const_area = insn->InputAt(1)->AsX86LoadFromConstantTable();
1513 if (is_double) {
1514 if (rhs.IsFpuRegister()) {
1515 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1516 } else if (const_area != nullptr) {
1517 DCHECK(const_area->IsEmittedAtUseSite());
1518 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(),
1519 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001520 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
1521 const_area->GetBaseMethodAddress(),
1522 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001523 } else {
1524 DCHECK(rhs.IsDoubleStackSlot());
1525 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1526 }
1527 } else {
1528 if (rhs.IsFpuRegister()) {
1529 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1530 } else if (const_area != nullptr) {
1531 DCHECK(const_area->IsEmittedAtUseSite());
1532 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(),
1533 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001534 const_area->GetConstant()->AsFloatConstant()->GetValue(),
1535 const_area->GetBaseMethodAddress(),
1536 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001537 } else {
1538 DCHECK(rhs.IsStackSlot());
1539 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1540 }
1541 }
1542}
1543
Mark Mendell152408f2015-12-31 12:28:50 -05001544template<class LabelType>
David Brazdil0debae72015-11-12 18:37:00 +00001545void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendell152408f2015-12-31 12:28:50 -05001546 LabelType* true_target_in,
1547 LabelType* false_target_in) {
David Brazdil0debae72015-11-12 18:37:00 +00001548 // Generated branching requires both targets to be explicit. If either of the
1549 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Mark Mendell152408f2015-12-31 12:28:50 -05001550 LabelType fallthrough_target;
1551 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1552 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
David Brazdil0debae72015-11-12 18:37:00 +00001553
Mark Mendellc4701932015-04-10 13:18:51 -04001554 LocationSummary* locations = condition->GetLocations();
1555 Location left = locations->InAt(0);
1556 Location right = locations->InAt(1);
1557
Mark Mendellc4701932015-04-10 13:18:51 -04001558 Primitive::Type type = condition->InputAt(0)->GetType();
1559 switch (type) {
1560 case Primitive::kPrimLong:
1561 GenerateLongComparesAndJumps(condition, true_target, false_target);
1562 break;
1563 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001564 GenerateFPCompare(left, right, condition, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001565 GenerateFPJumps(condition, true_target, false_target);
1566 break;
1567 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001568 GenerateFPCompare(left, right, condition, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001569 GenerateFPJumps(condition, true_target, false_target);
1570 break;
1571 default:
1572 LOG(FATAL) << "Unexpected compare type " << type;
1573 }
1574
David Brazdil0debae72015-11-12 18:37:00 +00001575 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001576 __ jmp(false_target);
1577 }
David Brazdil0debae72015-11-12 18:37:00 +00001578
1579 if (fallthrough_target.IsLinked()) {
1580 __ Bind(&fallthrough_target);
1581 }
Mark Mendellc4701932015-04-10 13:18:51 -04001582}
1583
David Brazdil0debae72015-11-12 18:37:00 +00001584static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1585 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1586 // are set only strictly before `branch`. We can't use the eflags on long/FP
1587 // conditions if they are materialized due to the complex branching.
1588 return cond->IsCondition() &&
1589 cond->GetNext() == branch &&
1590 cond->InputAt(0)->GetType() != Primitive::kPrimLong &&
1591 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1592}
1593
Mark Mendell152408f2015-12-31 12:28:50 -05001594template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001595void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001596 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001597 LabelType* true_target,
1598 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001599 HInstruction* cond = instruction->InputAt(condition_input_index);
1600
1601 if (true_target == nullptr && false_target == nullptr) {
1602 // Nothing to do. The code always falls through.
1603 return;
1604 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001605 // Constant condition, statically compared against "true" (integer value 1).
1606 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001607 if (true_target != nullptr) {
1608 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001609 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001610 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001611 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001612 if (false_target != nullptr) {
1613 __ jmp(false_target);
1614 }
1615 }
1616 return;
1617 }
1618
1619 // The following code generates these patterns:
1620 // (1) true_target == nullptr && false_target != nullptr
1621 // - opposite condition true => branch to false_target
1622 // (2) true_target != nullptr && false_target == nullptr
1623 // - condition true => branch to true_target
1624 // (3) true_target != nullptr && false_target != nullptr
1625 // - condition true => branch to true_target
1626 // - branch to false_target
1627 if (IsBooleanValueOrMaterializedCondition(cond)) {
1628 if (AreEflagsSetFrom(cond, instruction)) {
1629 if (true_target == nullptr) {
1630 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
1631 } else {
1632 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
1633 }
1634 } else {
1635 // Materialized condition, compare against 0.
1636 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1637 if (lhs.IsRegister()) {
1638 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1639 } else {
1640 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1641 }
1642 if (true_target == nullptr) {
1643 __ j(kEqual, false_target);
1644 } else {
1645 __ j(kNotEqual, true_target);
1646 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001647 }
1648 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001649 // Condition has not been materialized, use its inputs as the comparison and
1650 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001651 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001652
1653 // If this is a long or FP comparison that has been folded into
1654 // the HCondition, generate the comparison directly.
1655 Primitive::Type type = condition->InputAt(0)->GetType();
1656 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1657 GenerateCompareTestAndBranch(condition, true_target, false_target);
1658 return;
1659 }
1660
1661 Location lhs = condition->GetLocations()->InAt(0);
1662 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001663 // LHS is guaranteed to be in a register (see LocationsBuilderX86::HandleCondition).
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001664 codegen_->GenerateIntCompare(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00001665 if (true_target == nullptr) {
1666 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1667 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001668 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001669 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001670 }
David Brazdil0debae72015-11-12 18:37:00 +00001671
1672 // If neither branch falls through (case 3), the conditional branch to `true_target`
1673 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1674 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001675 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001676 }
1677}
1678
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001679void LocationsBuilderX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001680 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1681 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001682 locations->SetInAt(0, Location::Any());
1683 }
1684}
1685
1686void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001687 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1688 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1689 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1690 nullptr : codegen_->GetLabelOf(true_successor);
1691 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1692 nullptr : codegen_->GetLabelOf(false_successor);
1693 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001694}
1695
1696void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1697 LocationSummary* locations = new (GetGraph()->GetArena())
1698 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01001699 InvokeRuntimeCallingConvention calling_convention;
1700 RegisterSet caller_saves = RegisterSet::Empty();
1701 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1702 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00001703 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001704 locations->SetInAt(0, Location::Any());
1705 }
1706}
1707
1708void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001709 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001710 GenerateTestAndBranch<Label>(deoptimize,
1711 /* condition_input_index */ 0,
1712 slow_path->GetEntryLabel(),
1713 /* false_target */ nullptr);
1714}
1715
Mingyao Yang063fc772016-08-02 11:02:54 -07001716void LocationsBuilderX86::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
1717 LocationSummary* locations = new (GetGraph()->GetArena())
1718 LocationSummary(flag, LocationSummary::kNoCall);
1719 locations->SetOut(Location::RequiresRegister());
1720}
1721
1722void InstructionCodeGeneratorX86::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
1723 __ movl(flag->GetLocations()->Out().AsRegister<Register>(),
1724 Address(ESP, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
1725}
1726
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001727static bool SelectCanUseCMOV(HSelect* select) {
1728 // There are no conditional move instructions for XMMs.
1729 if (Primitive::IsFloatingPointType(select->GetType())) {
1730 return false;
1731 }
1732
1733 // A FP condition doesn't generate the single CC that we need.
1734 // In 32 bit mode, a long condition doesn't generate a single CC either.
1735 HInstruction* condition = select->GetCondition();
1736 if (condition->IsCondition()) {
1737 Primitive::Type compare_type = condition->InputAt(0)->GetType();
1738 if (compare_type == Primitive::kPrimLong ||
1739 Primitive::IsFloatingPointType(compare_type)) {
1740 return false;
1741 }
1742 }
1743
1744 // We can generate a CMOV for this Select.
1745 return true;
1746}
1747
David Brazdil74eb1b22015-12-14 11:44:01 +00001748void LocationsBuilderX86::VisitSelect(HSelect* select) {
1749 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001750 if (Primitive::IsFloatingPointType(select->GetType())) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001751 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001752 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001753 } else {
1754 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001755 if (SelectCanUseCMOV(select)) {
1756 if (select->InputAt(1)->IsConstant()) {
1757 // Cmov can't handle a constant value.
1758 locations->SetInAt(1, Location::RequiresRegister());
1759 } else {
1760 locations->SetInAt(1, Location::Any());
1761 }
1762 } else {
1763 locations->SetInAt(1, Location::Any());
1764 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001765 }
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001766 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1767 locations->SetInAt(2, Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00001768 }
1769 locations->SetOut(Location::SameAsFirstInput());
1770}
1771
1772void InstructionCodeGeneratorX86::VisitSelect(HSelect* select) {
1773 LocationSummary* locations = select->GetLocations();
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001774 DCHECK(locations->InAt(0).Equals(locations->Out()));
1775 if (SelectCanUseCMOV(select)) {
1776 // If both the condition and the source types are integer, we can generate
1777 // a CMOV to implement Select.
1778
1779 HInstruction* select_condition = select->GetCondition();
1780 Condition cond = kNotEqual;
1781
1782 // Figure out how to test the 'condition'.
1783 if (select_condition->IsCondition()) {
1784 HCondition* condition = select_condition->AsCondition();
1785 if (!condition->IsEmittedAtUseSite()) {
1786 // This was a previously materialized condition.
1787 // Can we use the existing condition code?
1788 if (AreEflagsSetFrom(condition, select)) {
1789 // Materialization was the previous instruction. Condition codes are right.
1790 cond = X86Condition(condition->GetCondition());
1791 } else {
1792 // No, we have to recreate the condition code.
1793 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1794 __ testl(cond_reg, cond_reg);
1795 }
1796 } else {
1797 // We can't handle FP or long here.
1798 DCHECK_NE(condition->InputAt(0)->GetType(), Primitive::kPrimLong);
1799 DCHECK(!Primitive::IsFloatingPointType(condition->InputAt(0)->GetType()));
1800 LocationSummary* cond_locations = condition->GetLocations();
Roland Levillain0b671c02016-08-19 12:02:34 +01001801 codegen_->GenerateIntCompare(cond_locations->InAt(0), cond_locations->InAt(1));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001802 cond = X86Condition(condition->GetCondition());
1803 }
1804 } else {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01001805 // Must be a Boolean condition, which needs to be compared to 0.
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001806 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1807 __ testl(cond_reg, cond_reg);
1808 }
1809
1810 // If the condition is true, overwrite the output, which already contains false.
1811 Location false_loc = locations->InAt(0);
1812 Location true_loc = locations->InAt(1);
1813 if (select->GetType() == Primitive::kPrimLong) {
1814 // 64 bit conditional move.
1815 Register false_high = false_loc.AsRegisterPairHigh<Register>();
1816 Register false_low = false_loc.AsRegisterPairLow<Register>();
1817 if (true_loc.IsRegisterPair()) {
1818 __ cmovl(cond, false_high, true_loc.AsRegisterPairHigh<Register>());
1819 __ cmovl(cond, false_low, true_loc.AsRegisterPairLow<Register>());
1820 } else {
1821 __ cmovl(cond, false_high, Address(ESP, true_loc.GetHighStackIndex(kX86WordSize)));
1822 __ cmovl(cond, false_low, Address(ESP, true_loc.GetStackIndex()));
1823 }
1824 } else {
1825 // 32 bit conditional move.
1826 Register false_reg = false_loc.AsRegister<Register>();
1827 if (true_loc.IsRegister()) {
1828 __ cmovl(cond, false_reg, true_loc.AsRegister<Register>());
1829 } else {
1830 __ cmovl(cond, false_reg, Address(ESP, true_loc.GetStackIndex()));
1831 }
1832 }
1833 } else {
1834 NearLabel false_target;
1835 GenerateTestAndBranch<NearLabel>(
1836 select, /* condition_input_index */ 2, /* true_target */ nullptr, &false_target);
1837 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1838 __ Bind(&false_target);
1839 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001840}
1841
David Srbecky0cf44932015-12-09 14:09:59 +00001842void LocationsBuilderX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1843 new (GetGraph()->GetArena()) LocationSummary(info);
1844}
1845
David Srbeckyd28f4a02016-03-14 17:14:24 +00001846void InstructionCodeGeneratorX86::VisitNativeDebugInfo(HNativeDebugInfo*) {
1847 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001848}
1849
1850void CodeGeneratorX86::GenerateNop() {
1851 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001852}
1853
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001854void LocationsBuilderX86::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001855 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001856 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001857 // Handle the long/FP comparisons made in instruction simplification.
1858 switch (cond->InputAt(0)->GetType()) {
1859 case Primitive::kPrimLong: {
1860 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell8659e842016-02-16 10:41:46 -05001861 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001862 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001863 locations->SetOut(Location::RequiresRegister());
1864 }
1865 break;
1866 }
1867 case Primitive::kPrimFloat:
1868 case Primitive::kPrimDouble: {
1869 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001870 if (cond->InputAt(1)->IsX86LoadFromConstantTable()) {
1871 DCHECK(cond->InputAt(1)->IsEmittedAtUseSite());
1872 } else if (cond->InputAt(1)->IsConstant()) {
1873 locations->SetInAt(1, Location::RequiresFpuRegister());
1874 } else {
1875 locations->SetInAt(1, Location::Any());
1876 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001877 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001878 locations->SetOut(Location::RequiresRegister());
1879 }
1880 break;
1881 }
1882 default:
1883 locations->SetInAt(0, Location::RequiresRegister());
1884 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001885 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001886 // We need a byte register.
1887 locations->SetOut(Location::RegisterLocation(ECX));
1888 }
1889 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001890 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001891}
1892
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001893void InstructionCodeGeneratorX86::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001894 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001895 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001896 }
Mark Mendellc4701932015-04-10 13:18:51 -04001897
1898 LocationSummary* locations = cond->GetLocations();
1899 Location lhs = locations->InAt(0);
1900 Location rhs = locations->InAt(1);
1901 Register reg = locations->Out().AsRegister<Register>();
Mark Mendell152408f2015-12-31 12:28:50 -05001902 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001903
1904 switch (cond->InputAt(0)->GetType()) {
1905 default: {
1906 // Integer case.
1907
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01001908 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04001909 __ xorl(reg, reg);
Roland Levillain0b671c02016-08-19 12:02:34 +01001910 codegen_->GenerateIntCompare(lhs, rhs);
Aart Bike9f37602015-10-09 11:15:55 -07001911 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001912 return;
1913 }
1914 case Primitive::kPrimLong:
1915 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1916 break;
1917 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001918 GenerateFPCompare(lhs, rhs, cond, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001919 GenerateFPJumps(cond, &true_label, &false_label);
1920 break;
1921 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001922 GenerateFPCompare(lhs, rhs, cond, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001923 GenerateFPJumps(cond, &true_label, &false_label);
1924 break;
1925 }
1926
1927 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001928 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001929
Roland Levillain4fa13f62015-07-06 18:11:54 +01001930 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001931 __ Bind(&false_label);
1932 __ xorl(reg, reg);
1933 __ jmp(&done_label);
1934
Roland Levillain4fa13f62015-07-06 18:11:54 +01001935 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001936 __ Bind(&true_label);
1937 __ movl(reg, Immediate(1));
1938 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001939}
1940
1941void LocationsBuilderX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001942 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001943}
1944
1945void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001946 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001947}
1948
1949void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001950 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001951}
1952
1953void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001954 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001955}
1956
1957void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001958 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001959}
1960
1961void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001962 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001963}
1964
1965void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001966 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001967}
1968
1969void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001970 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001971}
1972
1973void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001974 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001975}
1976
1977void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001978 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001979}
1980
1981void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001982 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001983}
1984
1985void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001986 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001987}
1988
Aart Bike9f37602015-10-09 11:15:55 -07001989void LocationsBuilderX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001990 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001991}
1992
1993void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001994 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001995}
1996
1997void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001998 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001999}
2000
2001void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002002 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002003}
2004
2005void LocationsBuilderX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002006 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002007}
2008
2009void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002010 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002011}
2012
2013void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002014 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002015}
2016
2017void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002018 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002019}
2020
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002021void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002022 LocationSummary* locations =
2023 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002024 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002025}
2026
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002027void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002028 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002029}
2030
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002031void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
2032 LocationSummary* locations =
2033 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2034 locations->SetOut(Location::ConstantLocation(constant));
2035}
2036
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002037void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002038 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002039}
2040
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002041void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002042 LocationSummary* locations =
2043 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002044 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002045}
2046
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002047void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002048 // Will be generated at use site.
2049}
2050
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002051void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
2052 LocationSummary* locations =
2053 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2054 locations->SetOut(Location::ConstantLocation(constant));
2055}
2056
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002057void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002058 // Will be generated at use site.
2059}
2060
2061void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
2062 LocationSummary* locations =
2063 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2064 locations->SetOut(Location::ConstantLocation(constant));
2065}
2066
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002067void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002068 // Will be generated at use site.
2069}
2070
Igor Murashkind01745e2017-04-05 16:40:31 -07002071void LocationsBuilderX86::VisitConstructorFence(HConstructorFence* constructor_fence) {
2072 constructor_fence->SetLocations(nullptr);
2073}
2074
2075void InstructionCodeGeneratorX86::VisitConstructorFence(
2076 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
2077 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
2078}
2079
Calin Juravle27df7582015-04-17 19:12:31 +01002080void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2081 memory_barrier->SetLocations(nullptr);
2082}
2083
2084void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00002085 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002086}
2087
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002088void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002089 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002090}
2091
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002092void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002093 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002094}
2095
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002096void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002097 LocationSummary* locations =
2098 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002099 switch (ret->InputAt(0)->GetType()) {
2100 case Primitive::kPrimBoolean:
2101 case Primitive::kPrimByte:
2102 case Primitive::kPrimChar:
2103 case Primitive::kPrimShort:
2104 case Primitive::kPrimInt:
2105 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002106 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002107 break;
2108
2109 case Primitive::kPrimLong:
2110 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002111 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002112 break;
2113
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002114 case Primitive::kPrimFloat:
2115 case Primitive::kPrimDouble:
2116 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002117 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002118 break;
2119
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002120 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002121 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002122 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002123}
2124
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002125void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002126 if (kIsDebugBuild) {
2127 switch (ret->InputAt(0)->GetType()) {
2128 case Primitive::kPrimBoolean:
2129 case Primitive::kPrimByte:
2130 case Primitive::kPrimChar:
2131 case Primitive::kPrimShort:
2132 case Primitive::kPrimInt:
2133 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002134 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002135 break;
2136
2137 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002138 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
2139 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002140 break;
2141
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002142 case Primitive::kPrimFloat:
2143 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002144 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002145 break;
2146
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002147 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002148 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002149 }
2150 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002151 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002152}
2153
Calin Juravle175dc732015-08-25 15:42:32 +01002154void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2155 // The trampoline uses the same calling convention as dex calling conventions,
2156 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2157 // the method_idx.
2158 HandleInvoke(invoke);
2159}
2160
2161void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2162 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2163}
2164
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002165void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002166 // Explicit clinit checks triggered by static invokes must have been pruned by
2167 // art::PrepareForRegisterAllocation.
2168 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002169
Mark Mendellfb8d2792015-03-31 22:16:59 -04002170 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002171 if (intrinsic.TryDispatch(invoke)) {
Vladimir Marko65979462017-05-19 17:25:12 +01002172 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeMethodLoadKind()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00002173 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002174 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04002175 return;
2176 }
2177
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002178 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002179
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002180 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
Vladimir Marko65979462017-05-19 17:25:12 +01002181 if (invoke->HasPcRelativeMethodLoadKind()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00002182 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002183 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002184}
2185
Mark Mendell09ed1a32015-03-25 08:30:06 -04002186static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
2187 if (invoke->GetLocations()->Intrinsified()) {
2188 IntrinsicCodeGeneratorX86 intrinsic(codegen);
2189 intrinsic.Dispatch(invoke);
2190 return true;
2191 }
2192 return false;
2193}
2194
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002195void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002196 // Explicit clinit checks triggered by static invokes must have been pruned by
2197 // art::PrepareForRegisterAllocation.
2198 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002199
Mark Mendell09ed1a32015-03-25 08:30:06 -04002200 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2201 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002202 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002203
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002204 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04002205 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002206 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Mingyao Yang8693fe12015-04-17 16:51:08 -07002207 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002208}
2209
2210void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00002211 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
2212 if (intrinsic.TryDispatch(invoke)) {
2213 return;
2214 }
2215
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002216 HandleInvoke(invoke);
2217}
2218
2219void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002220 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002221 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002222}
2223
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002224void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002225 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2226 return;
2227 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002228
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002229 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002230 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002231 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002232}
2233
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002234void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002235 // This call to HandleInvoke allocates a temporary (core) register
2236 // which is also used to transfer the hidden argument from FP to
2237 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002238 HandleInvoke(invoke);
2239 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002240 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002241}
2242
2243void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
2244 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002245 LocationSummary* locations = invoke->GetLocations();
2246 Register temp = locations->GetTemp(0).AsRegister<Register>();
2247 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002248 Location receiver = locations->InAt(0);
2249 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2250
Roland Levillain0d5a2812015-11-13 10:07:31 +00002251 // Set the hidden argument. This is safe to do this here, as XMM7
2252 // won't be modified thereafter, before the `call` instruction.
2253 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002254 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002255 __ movd(hidden_reg, temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002256
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002257 if (receiver.IsStackSlot()) {
2258 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002259 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002260 __ movl(temp, Address(temp, class_offset));
2261 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002262 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002263 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002264 }
Roland Levillain4d027112015-07-01 15:41:14 +01002265 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002266 // Instead of simply (possibly) unpoisoning `temp` here, we should
2267 // emit a read barrier for the previous class reference load.
2268 // However this is not required in practice, as this is an
2269 // intermediate/temporary reference and because the current
2270 // concurrent copying collector keeps the from-space memory
2271 // intact/accessible until the end of the marking phase (the
2272 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002273 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002274 // temp = temp->GetAddressOfIMT()
2275 __ movl(temp,
2276 Address(temp, mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002277 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002278 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002279 invoke->GetImtIndex(), kX86PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002280 __ movl(temp, Address(temp, method_offset));
2281 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002282 __ call(Address(temp,
Andreas Gampe542451c2016-07-26 09:02:02 -07002283 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002284
2285 DCHECK(!codegen_->IsLeafMethod());
2286 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2287}
2288
Orion Hodsonac141392017-01-13 11:53:47 +00002289void LocationsBuilderX86::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2290 HandleInvoke(invoke);
2291}
2292
2293void InstructionCodeGeneratorX86::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2294 codegen_->GenerateInvokePolymorphicCall(invoke);
2295}
2296
Roland Levillain88cb1752014-10-20 16:36:47 +01002297void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2298 LocationSummary* locations =
2299 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2300 switch (neg->GetResultType()) {
2301 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002302 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002303 locations->SetInAt(0, Location::RequiresRegister());
2304 locations->SetOut(Location::SameAsFirstInput());
2305 break;
2306
Roland Levillain88cb1752014-10-20 16:36:47 +01002307 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00002308 locations->SetInAt(0, Location::RequiresFpuRegister());
2309 locations->SetOut(Location::SameAsFirstInput());
2310 locations->AddTemp(Location::RequiresRegister());
2311 locations->AddTemp(Location::RequiresFpuRegister());
2312 break;
2313
Roland Levillain88cb1752014-10-20 16:36:47 +01002314 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002315 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002316 locations->SetOut(Location::SameAsFirstInput());
2317 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002318 break;
2319
2320 default:
2321 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2322 }
2323}
2324
2325void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2326 LocationSummary* locations = neg->GetLocations();
2327 Location out = locations->Out();
2328 Location in = locations->InAt(0);
2329 switch (neg->GetResultType()) {
2330 case Primitive::kPrimInt:
2331 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002332 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002333 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002334 break;
2335
2336 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002337 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002338 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002339 __ negl(out.AsRegisterPairLow<Register>());
2340 // Negation is similar to subtraction from zero. The least
2341 // significant byte triggers a borrow when it is different from
2342 // zero; to take it into account, add 1 to the most significant
2343 // byte if the carry flag (CF) is set to 1 after the first NEGL
2344 // operation.
2345 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2346 __ negl(out.AsRegisterPairHigh<Register>());
2347 break;
2348
Roland Levillain5368c212014-11-27 15:03:41 +00002349 case Primitive::kPrimFloat: {
2350 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002351 Register constant = locations->GetTemp(0).AsRegister<Register>();
2352 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002353 // Implement float negation with an exclusive or with value
2354 // 0x80000000 (mask for bit 31, representing the sign of a
2355 // single-precision floating-point number).
2356 __ movl(constant, Immediate(INT32_C(0x80000000)));
2357 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002358 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002359 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002360 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002361
Roland Levillain5368c212014-11-27 15:03:41 +00002362 case Primitive::kPrimDouble: {
2363 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002364 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002365 // Implement double negation with an exclusive or with value
2366 // 0x8000000000000000 (mask for bit 63, representing the sign of
2367 // a double-precision floating-point number).
2368 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002369 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002370 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002371 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002372
2373 default:
2374 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2375 }
2376}
2377
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002378void LocationsBuilderX86::VisitX86FPNeg(HX86FPNeg* neg) {
2379 LocationSummary* locations =
2380 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2381 DCHECK(Primitive::IsFloatingPointType(neg->GetType()));
2382 locations->SetInAt(0, Location::RequiresFpuRegister());
2383 locations->SetInAt(1, Location::RequiresRegister());
2384 locations->SetOut(Location::SameAsFirstInput());
2385 locations->AddTemp(Location::RequiresFpuRegister());
2386}
2387
2388void InstructionCodeGeneratorX86::VisitX86FPNeg(HX86FPNeg* neg) {
2389 LocationSummary* locations = neg->GetLocations();
2390 Location out = locations->Out();
2391 DCHECK(locations->InAt(0).Equals(out));
2392
2393 Register constant_area = locations->InAt(1).AsRegister<Register>();
2394 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2395 if (neg->GetType() == Primitive::kPrimFloat) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00002396 __ movss(mask, codegen_->LiteralInt32Address(INT32_C(0x80000000),
2397 neg->GetBaseMethodAddress(),
2398 constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002399 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
2400 } else {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00002401 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000),
2402 neg->GetBaseMethodAddress(),
2403 constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002404 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
2405 }
2406}
2407
Roland Levillaindff1f282014-11-05 14:15:05 +00002408void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002409 Primitive::Type result_type = conversion->GetResultType();
2410 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002411 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002412
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002413 // The float-to-long and double-to-long type conversions rely on a
2414 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002415 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002416 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2417 && result_type == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002418 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002419 : LocationSummary::kNoCall;
2420 LocationSummary* locations =
2421 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2422
David Brazdilb2bd1c52015-03-25 11:17:37 +00002423 // The Java language does not allow treating boolean as an integral type but
2424 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002425
Roland Levillaindff1f282014-11-05 14:15:05 +00002426 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002427 case Primitive::kPrimByte:
2428 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002429 case Primitive::kPrimLong: {
2430 // Type conversion from long to byte is a result of code transformations.
2431 HInstruction* input = conversion->InputAt(0);
2432 Location input_location = input->IsConstant()
2433 ? Location::ConstantLocation(input->AsConstant())
2434 : Location::RegisterPairLocation(EAX, EDX);
2435 locations->SetInAt(0, input_location);
2436 // Make the output overlap to please the register allocator. This greatly simplifies
2437 // the validation of the linear scan implementation
2438 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2439 break;
2440 }
David Brazdil46e2a392015-03-16 17:31:52 +00002441 case Primitive::kPrimBoolean:
2442 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002443 case Primitive::kPrimShort:
2444 case Primitive::kPrimInt:
2445 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002446 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05002447 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2448 // Make the output overlap to please the register allocator. This greatly simplifies
2449 // the validation of the linear scan implementation
2450 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002451 break;
2452
2453 default:
2454 LOG(FATAL) << "Unexpected type conversion from " << input_type
2455 << " to " << result_type;
2456 }
2457 break;
2458
Roland Levillain01a8d712014-11-14 16:27:39 +00002459 case Primitive::kPrimShort:
2460 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002461 case Primitive::kPrimLong:
2462 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002463 case Primitive::kPrimBoolean:
2464 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002465 case Primitive::kPrimByte:
2466 case Primitive::kPrimInt:
2467 case Primitive::kPrimChar:
2468 // Processing a Dex `int-to-short' instruction.
2469 locations->SetInAt(0, Location::Any());
2470 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2471 break;
2472
2473 default:
2474 LOG(FATAL) << "Unexpected type conversion from " << input_type
2475 << " to " << result_type;
2476 }
2477 break;
2478
Roland Levillain946e1432014-11-11 17:35:19 +00002479 case Primitive::kPrimInt:
2480 switch (input_type) {
2481 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002482 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002483 locations->SetInAt(0, Location::Any());
2484 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2485 break;
2486
2487 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002488 // Processing a Dex `float-to-int' instruction.
2489 locations->SetInAt(0, Location::RequiresFpuRegister());
2490 locations->SetOut(Location::RequiresRegister());
2491 locations->AddTemp(Location::RequiresFpuRegister());
2492 break;
2493
Roland Levillain946e1432014-11-11 17:35:19 +00002494 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002495 // Processing a Dex `double-to-int' instruction.
2496 locations->SetInAt(0, Location::RequiresFpuRegister());
2497 locations->SetOut(Location::RequiresRegister());
2498 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002499 break;
2500
2501 default:
2502 LOG(FATAL) << "Unexpected type conversion from " << input_type
2503 << " to " << result_type;
2504 }
2505 break;
2506
Roland Levillaindff1f282014-11-05 14:15:05 +00002507 case Primitive::kPrimLong:
2508 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002509 case Primitive::kPrimBoolean:
2510 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002511 case Primitive::kPrimByte:
2512 case Primitive::kPrimShort:
2513 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002514 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002515 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002516 locations->SetInAt(0, Location::RegisterLocation(EAX));
2517 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2518 break;
2519
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002520 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00002521 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002522 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00002523 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002524 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2525 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2526
Vladimir Marko949c91f2015-01-27 10:48:44 +00002527 // The runtime helper puts the result in EAX, EDX.
2528 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002529 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002530 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002531
2532 default:
2533 LOG(FATAL) << "Unexpected type conversion from " << input_type
2534 << " to " << result_type;
2535 }
2536 break;
2537
Roland Levillain981e4542014-11-14 11:47:14 +00002538 case Primitive::kPrimChar:
2539 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002540 case Primitive::kPrimLong:
2541 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002542 case Primitive::kPrimBoolean:
2543 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002544 case Primitive::kPrimByte:
2545 case Primitive::kPrimShort:
2546 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002547 // Processing a Dex `int-to-char' instruction.
2548 locations->SetInAt(0, Location::Any());
2549 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2550 break;
2551
2552 default:
2553 LOG(FATAL) << "Unexpected type conversion from " << input_type
2554 << " to " << result_type;
2555 }
2556 break;
2557
Roland Levillaindff1f282014-11-05 14:15:05 +00002558 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002559 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002560 case Primitive::kPrimBoolean:
2561 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002562 case Primitive::kPrimByte:
2563 case Primitive::kPrimShort:
2564 case Primitive::kPrimInt:
2565 case Primitive::kPrimChar:
2566 // Processing a Dex `int-to-float' instruction.
2567 locations->SetInAt(0, Location::RequiresRegister());
2568 locations->SetOut(Location::RequiresFpuRegister());
2569 break;
2570
2571 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002572 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002573 locations->SetInAt(0, Location::Any());
2574 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002575 break;
2576
Roland Levillaincff13742014-11-17 14:32:17 +00002577 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002578 // Processing a Dex `double-to-float' instruction.
2579 locations->SetInAt(0, Location::RequiresFpuRegister());
2580 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002581 break;
2582
2583 default:
2584 LOG(FATAL) << "Unexpected type conversion from " << input_type
2585 << " to " << result_type;
2586 };
2587 break;
2588
Roland Levillaindff1f282014-11-05 14:15:05 +00002589 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002590 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002591 case Primitive::kPrimBoolean:
2592 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002593 case Primitive::kPrimByte:
2594 case Primitive::kPrimShort:
2595 case Primitive::kPrimInt:
2596 case Primitive::kPrimChar:
2597 // Processing a Dex `int-to-double' instruction.
2598 locations->SetInAt(0, Location::RequiresRegister());
2599 locations->SetOut(Location::RequiresFpuRegister());
2600 break;
2601
2602 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002603 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002604 locations->SetInAt(0, Location::Any());
2605 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002606 break;
2607
Roland Levillaincff13742014-11-17 14:32:17 +00002608 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002609 // Processing a Dex `float-to-double' instruction.
2610 locations->SetInAt(0, Location::RequiresFpuRegister());
2611 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002612 break;
2613
2614 default:
2615 LOG(FATAL) << "Unexpected type conversion from " << input_type
2616 << " to " << result_type;
2617 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002618 break;
2619
2620 default:
2621 LOG(FATAL) << "Unexpected type conversion from " << input_type
2622 << " to " << result_type;
2623 }
2624}
2625
2626void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2627 LocationSummary* locations = conversion->GetLocations();
2628 Location out = locations->Out();
2629 Location in = locations->InAt(0);
2630 Primitive::Type result_type = conversion->GetResultType();
2631 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002632 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002633 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002634 case Primitive::kPrimByte:
2635 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002636 case Primitive::kPrimLong:
2637 // Type conversion from long to byte is a result of code transformations.
2638 if (in.IsRegisterPair()) {
2639 __ movsxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2640 } else {
2641 DCHECK(in.GetConstant()->IsLongConstant());
2642 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2643 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2644 }
2645 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002646 case Primitive::kPrimBoolean:
2647 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002648 case Primitive::kPrimShort:
2649 case Primitive::kPrimInt:
2650 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002651 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002652 if (in.IsRegister()) {
2653 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002654 } else {
2655 DCHECK(in.GetConstant()->IsIntConstant());
2656 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2657 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2658 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002659 break;
2660
2661 default:
2662 LOG(FATAL) << "Unexpected type conversion from " << input_type
2663 << " to " << result_type;
2664 }
2665 break;
2666
Roland Levillain01a8d712014-11-14 16:27:39 +00002667 case Primitive::kPrimShort:
2668 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002669 case Primitive::kPrimLong:
2670 // Type conversion from long to short is a result of code transformations.
2671 if (in.IsRegisterPair()) {
2672 __ movsxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2673 } else if (in.IsDoubleStackSlot()) {
2674 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2675 } else {
2676 DCHECK(in.GetConstant()->IsLongConstant());
2677 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2678 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
2679 }
2680 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002681 case Primitive::kPrimBoolean:
2682 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002683 case Primitive::kPrimByte:
2684 case Primitive::kPrimInt:
2685 case Primitive::kPrimChar:
2686 // Processing a Dex `int-to-short' instruction.
2687 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002688 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002689 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002690 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002691 } else {
2692 DCHECK(in.GetConstant()->IsIntConstant());
2693 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002694 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002695 }
2696 break;
2697
2698 default:
2699 LOG(FATAL) << "Unexpected type conversion from " << input_type
2700 << " to " << result_type;
2701 }
2702 break;
2703
Roland Levillain946e1432014-11-11 17:35:19 +00002704 case Primitive::kPrimInt:
2705 switch (input_type) {
2706 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002707 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002708 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002709 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002710 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002711 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002712 } else {
2713 DCHECK(in.IsConstant());
2714 DCHECK(in.GetConstant()->IsLongConstant());
2715 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002716 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002717 }
2718 break;
2719
Roland Levillain3f8f9362014-12-02 17:45:01 +00002720 case Primitive::kPrimFloat: {
2721 // Processing a Dex `float-to-int' instruction.
2722 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2723 Register output = out.AsRegister<Register>();
2724 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002725 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002726
2727 __ movl(output, Immediate(kPrimIntMax));
2728 // temp = int-to-float(output)
2729 __ cvtsi2ss(temp, output);
2730 // if input >= temp goto done
2731 __ comiss(input, temp);
2732 __ j(kAboveEqual, &done);
2733 // if input == NaN goto nan
2734 __ j(kUnordered, &nan);
2735 // output = float-to-int-truncate(input)
2736 __ cvttss2si(output, input);
2737 __ jmp(&done);
2738 __ Bind(&nan);
2739 // output = 0
2740 __ xorl(output, output);
2741 __ Bind(&done);
2742 break;
2743 }
2744
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002745 case Primitive::kPrimDouble: {
2746 // Processing a Dex `double-to-int' instruction.
2747 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2748 Register output = out.AsRegister<Register>();
2749 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002750 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002751
2752 __ movl(output, Immediate(kPrimIntMax));
2753 // temp = int-to-double(output)
2754 __ cvtsi2sd(temp, output);
2755 // if input >= temp goto done
2756 __ comisd(input, temp);
2757 __ j(kAboveEqual, &done);
2758 // if input == NaN goto nan
2759 __ j(kUnordered, &nan);
2760 // output = double-to-int-truncate(input)
2761 __ cvttsd2si(output, input);
2762 __ jmp(&done);
2763 __ Bind(&nan);
2764 // output = 0
2765 __ xorl(output, output);
2766 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002767 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002768 }
Roland Levillain946e1432014-11-11 17:35:19 +00002769
2770 default:
2771 LOG(FATAL) << "Unexpected type conversion from " << input_type
2772 << " to " << result_type;
2773 }
2774 break;
2775
Roland Levillaindff1f282014-11-05 14:15:05 +00002776 case Primitive::kPrimLong:
2777 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002778 case Primitive::kPrimBoolean:
2779 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002780 case Primitive::kPrimByte:
2781 case Primitive::kPrimShort:
2782 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002783 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002784 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002785 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2786 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002787 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002788 __ cdq();
2789 break;
2790
2791 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002792 // Processing a Dex `float-to-long' instruction.
Serban Constantinescuba45db02016-07-12 22:53:02 +01002793 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002794 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002795 break;
2796
Roland Levillaindff1f282014-11-05 14:15:05 +00002797 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002798 // Processing a Dex `double-to-long' instruction.
Serban Constantinescuba45db02016-07-12 22:53:02 +01002799 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002800 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002801 break;
2802
2803 default:
2804 LOG(FATAL) << "Unexpected type conversion from " << input_type
2805 << " to " << result_type;
2806 }
2807 break;
2808
Roland Levillain981e4542014-11-14 11:47:14 +00002809 case Primitive::kPrimChar:
2810 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002811 case Primitive::kPrimLong:
2812 // Type conversion from long to short is a result of code transformations.
2813 if (in.IsRegisterPair()) {
2814 __ movzxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2815 } else if (in.IsDoubleStackSlot()) {
2816 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2817 } else {
2818 DCHECK(in.GetConstant()->IsLongConstant());
2819 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2820 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
2821 }
2822 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002823 case Primitive::kPrimBoolean:
2824 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002825 case Primitive::kPrimByte:
2826 case Primitive::kPrimShort:
2827 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002828 // Processing a Dex `Process a Dex `int-to-char'' instruction.
2829 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002830 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00002831 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002832 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00002833 } else {
2834 DCHECK(in.GetConstant()->IsIntConstant());
2835 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002836 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00002837 }
2838 break;
2839
2840 default:
2841 LOG(FATAL) << "Unexpected type conversion from " << input_type
2842 << " to " << result_type;
2843 }
2844 break;
2845
Roland Levillaindff1f282014-11-05 14:15:05 +00002846 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002847 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002848 case Primitive::kPrimBoolean:
2849 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002850 case Primitive::kPrimByte:
2851 case Primitive::kPrimShort:
2852 case Primitive::kPrimInt:
2853 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002854 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002855 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002856 break;
2857
Roland Levillain6d0e4832014-11-27 18:31:21 +00002858 case Primitive::kPrimLong: {
2859 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002860 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002861
Roland Levillain232ade02015-04-20 15:14:36 +01002862 // Create stack space for the call to
2863 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2864 // TODO: enhance register allocator to ask for stack temporaries.
2865 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
2866 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2867 __ subl(ESP, Immediate(adjustment));
2868 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002869
Roland Levillain232ade02015-04-20 15:14:36 +01002870 // Load the value to the FP stack, using temporaries if needed.
2871 PushOntoFPStack(in, 0, adjustment, false, true);
2872
2873 if (out.IsStackSlot()) {
2874 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2875 } else {
2876 __ fstps(Address(ESP, 0));
2877 Location stack_temp = Location::StackSlot(0);
2878 codegen_->Move32(out, stack_temp);
2879 }
2880
2881 // Remove the temporary stack space we allocated.
2882 if (adjustment != 0) {
2883 __ addl(ESP, Immediate(adjustment));
2884 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002885 break;
2886 }
2887
Roland Levillaincff13742014-11-17 14:32:17 +00002888 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002889 // Processing a Dex `double-to-float' instruction.
2890 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002891 break;
2892
2893 default:
2894 LOG(FATAL) << "Unexpected type conversion from " << input_type
2895 << " to " << result_type;
2896 };
2897 break;
2898
Roland Levillaindff1f282014-11-05 14:15:05 +00002899 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002900 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002901 case Primitive::kPrimBoolean:
2902 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002903 case Primitive::kPrimByte:
2904 case Primitive::kPrimShort:
2905 case Primitive::kPrimInt:
2906 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002907 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002908 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002909 break;
2910
Roland Levillain647b9ed2014-11-27 12:06:00 +00002911 case Primitive::kPrimLong: {
2912 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002913 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002914
Roland Levillain232ade02015-04-20 15:14:36 +01002915 // Create stack space for the call to
2916 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2917 // TODO: enhance register allocator to ask for stack temporaries.
2918 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
2919 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2920 __ subl(ESP, Immediate(adjustment));
2921 }
2922
2923 // Load the value to the FP stack, using temporaries if needed.
2924 PushOntoFPStack(in, 0, adjustment, false, true);
2925
2926 if (out.IsDoubleStackSlot()) {
2927 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2928 } else {
2929 __ fstpl(Address(ESP, 0));
2930 Location stack_temp = Location::DoubleStackSlot(0);
2931 codegen_->Move64(out, stack_temp);
2932 }
2933
2934 // Remove the temporary stack space we allocated.
2935 if (adjustment != 0) {
2936 __ addl(ESP, Immediate(adjustment));
2937 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002938 break;
2939 }
2940
Roland Levillaincff13742014-11-17 14:32:17 +00002941 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002942 // Processing a Dex `float-to-double' instruction.
2943 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002944 break;
2945
2946 default:
2947 LOG(FATAL) << "Unexpected type conversion from " << input_type
2948 << " to " << result_type;
2949 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002950 break;
2951
2952 default:
2953 LOG(FATAL) << "Unexpected type conversion from " << input_type
2954 << " to " << result_type;
2955 }
2956}
2957
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002958void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002959 LocationSummary* locations =
2960 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002961 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002962 case Primitive::kPrimInt: {
2963 locations->SetInAt(0, Location::RequiresRegister());
2964 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2965 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2966 break;
2967 }
2968
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002969 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002970 locations->SetInAt(0, Location::RequiresRegister());
2971 locations->SetInAt(1, Location::Any());
2972 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002973 break;
2974 }
2975
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002976 case Primitive::kPrimFloat:
2977 case Primitive::kPrimDouble: {
2978 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002979 if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2980 DCHECK(add->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002981 } else if (add->InputAt(1)->IsConstant()) {
2982 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002983 } else {
2984 locations->SetInAt(1, Location::Any());
2985 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002986 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002987 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002988 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002989
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002990 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002991 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2992 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002993 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002994}
2995
2996void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2997 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002998 Location first = locations->InAt(0);
2999 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05003000 Location out = locations->Out();
3001
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003002 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003003 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003004 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05003005 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3006 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04003007 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
3008 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05003009 } else {
3010 __ leal(out.AsRegister<Register>(), Address(
3011 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
3012 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003013 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05003014 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
3015 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3016 __ addl(out.AsRegister<Register>(), Immediate(value));
3017 } else {
3018 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
3019 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003020 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05003021 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003022 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003023 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003024 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003025 }
3026
3027 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003028 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003029 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3030 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003031 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003032 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3033 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003034 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003035 } else {
3036 DCHECK(second.IsConstant()) << second;
3037 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3038 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3039 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003040 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003041 break;
3042 }
3043
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003044 case Primitive::kPrimFloat: {
3045 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003046 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04003047 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3048 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003049 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003050 __ addss(first.AsFpuRegister<XmmRegister>(),
3051 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003052 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3053 const_area->GetBaseMethodAddress(),
3054 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003055 } else {
3056 DCHECK(second.IsStackSlot());
3057 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003058 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003059 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003060 }
3061
3062 case Primitive::kPrimDouble: {
3063 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003064 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04003065 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3066 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003067 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003068 __ addsd(first.AsFpuRegister<XmmRegister>(),
3069 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003070 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3071 const_area->GetBaseMethodAddress(),
3072 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003073 } else {
3074 DCHECK(second.IsDoubleStackSlot());
3075 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003076 }
3077 break;
3078 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003079
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003080 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003081 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003082 }
3083}
3084
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003085void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003086 LocationSummary* locations =
3087 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003088 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003089 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003090 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003091 locations->SetInAt(0, Location::RequiresRegister());
3092 locations->SetInAt(1, Location::Any());
3093 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003094 break;
3095 }
Calin Juravle11351682014-10-23 15:38:15 +01003096 case Primitive::kPrimFloat:
3097 case Primitive::kPrimDouble: {
3098 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003099 if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3100 DCHECK(sub->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003101 } else if (sub->InputAt(1)->IsConstant()) {
3102 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003103 } else {
3104 locations->SetInAt(1, Location::Any());
3105 }
Calin Juravle11351682014-10-23 15:38:15 +01003106 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003107 break;
Calin Juravle11351682014-10-23 15:38:15 +01003108 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003109
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003110 default:
Calin Juravle11351682014-10-23 15:38:15 +01003111 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003112 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003113}
3114
3115void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
3116 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003117 Location first = locations->InAt(0);
3118 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01003119 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003120 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003121 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003122 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003123 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003124 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003125 __ subl(first.AsRegister<Register>(),
3126 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003127 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003128 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003129 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003130 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003131 }
3132
3133 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003134 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01003135 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3136 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003137 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01003138 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003139 __ sbbl(first.AsRegisterPairHigh<Register>(),
3140 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003141 } else {
3142 DCHECK(second.IsConstant()) << second;
3143 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3144 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3145 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003146 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003147 break;
3148 }
3149
Calin Juravle11351682014-10-23 15:38:15 +01003150 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003151 if (second.IsFpuRegister()) {
3152 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3153 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3154 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003155 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003156 __ subss(first.AsFpuRegister<XmmRegister>(),
3157 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003158 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3159 const_area->GetBaseMethodAddress(),
3160 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003161 } else {
3162 DCHECK(second.IsStackSlot());
3163 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3164 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003165 break;
Calin Juravle11351682014-10-23 15:38:15 +01003166 }
3167
3168 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003169 if (second.IsFpuRegister()) {
3170 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3171 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3172 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003173 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003174 __ subsd(first.AsFpuRegister<XmmRegister>(),
3175 codegen_->LiteralDoubleAddress(
3176 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003177 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003178 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3179 } else {
3180 DCHECK(second.IsDoubleStackSlot());
3181 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3182 }
Calin Juravle11351682014-10-23 15:38:15 +01003183 break;
3184 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003185
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003186 default:
Calin Juravle11351682014-10-23 15:38:15 +01003187 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003188 }
3189}
3190
Calin Juravle34bacdf2014-10-07 20:23:36 +01003191void LocationsBuilderX86::VisitMul(HMul* mul) {
3192 LocationSummary* locations =
3193 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3194 switch (mul->GetResultType()) {
3195 case Primitive::kPrimInt:
3196 locations->SetInAt(0, Location::RequiresRegister());
3197 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003198 if (mul->InputAt(1)->IsIntConstant()) {
3199 // Can use 3 operand multiply.
3200 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3201 } else {
3202 locations->SetOut(Location::SameAsFirstInput());
3203 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003204 break;
3205 case Primitive::kPrimLong: {
3206 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003207 locations->SetInAt(1, Location::Any());
3208 locations->SetOut(Location::SameAsFirstInput());
3209 // Needed for imul on 32bits with 64bits output.
3210 locations->AddTemp(Location::RegisterLocation(EAX));
3211 locations->AddTemp(Location::RegisterLocation(EDX));
3212 break;
3213 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003214 case Primitive::kPrimFloat:
3215 case Primitive::kPrimDouble: {
3216 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003217 if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3218 DCHECK(mul->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003219 } else if (mul->InputAt(1)->IsConstant()) {
3220 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003221 } else {
3222 locations->SetInAt(1, Location::Any());
3223 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003224 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003225 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003226 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003227
3228 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003229 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003230 }
3231}
3232
3233void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
3234 LocationSummary* locations = mul->GetLocations();
3235 Location first = locations->InAt(0);
3236 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003237 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003238
3239 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003240 case Primitive::kPrimInt:
3241 // The constant may have ended up in a register, so test explicitly to avoid
3242 // problems where the output may not be the same as the first operand.
3243 if (mul->InputAt(1)->IsIntConstant()) {
3244 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3245 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
3246 } else if (second.IsRegister()) {
3247 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003248 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003249 } else {
3250 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003251 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003252 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003253 }
3254 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003255
3256 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003257 Register in1_hi = first.AsRegisterPairHigh<Register>();
3258 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003259 Register eax = locations->GetTemp(0).AsRegister<Register>();
3260 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003261
3262 DCHECK_EQ(EAX, eax);
3263 DCHECK_EQ(EDX, edx);
3264
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003265 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01003266 // output: in1
3267 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3268 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3269 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003270 if (second.IsConstant()) {
3271 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003272
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003273 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3274 int32_t low_value = Low32Bits(value);
3275 int32_t high_value = High32Bits(value);
3276 Immediate low(low_value);
3277 Immediate high(high_value);
3278
3279 __ movl(eax, high);
3280 // eax <- in1.lo * in2.hi
3281 __ imull(eax, in1_lo);
3282 // in1.hi <- in1.hi * in2.lo
3283 __ imull(in1_hi, low);
3284 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3285 __ addl(in1_hi, eax);
3286 // move in2_lo to eax to prepare for double precision
3287 __ movl(eax, low);
3288 // edx:eax <- in1.lo * in2.lo
3289 __ mull(in1_lo);
3290 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3291 __ addl(in1_hi, edx);
3292 // in1.lo <- (in1.lo * in2.lo)[31:0];
3293 __ movl(in1_lo, eax);
3294 } else if (second.IsRegisterPair()) {
3295 Register in2_hi = second.AsRegisterPairHigh<Register>();
3296 Register in2_lo = second.AsRegisterPairLow<Register>();
3297
3298 __ movl(eax, in2_hi);
3299 // eax <- in1.lo * in2.hi
3300 __ imull(eax, in1_lo);
3301 // in1.hi <- in1.hi * in2.lo
3302 __ imull(in1_hi, in2_lo);
3303 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3304 __ addl(in1_hi, eax);
3305 // move in1_lo to eax to prepare for double precision
3306 __ movl(eax, in1_lo);
3307 // edx:eax <- in1.lo * in2.lo
3308 __ mull(in2_lo);
3309 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3310 __ addl(in1_hi, edx);
3311 // in1.lo <- (in1.lo * in2.lo)[31:0];
3312 __ movl(in1_lo, eax);
3313 } else {
3314 DCHECK(second.IsDoubleStackSlot()) << second;
3315 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
3316 Address in2_lo(ESP, second.GetStackIndex());
3317
3318 __ movl(eax, in2_hi);
3319 // eax <- in1.lo * in2.hi
3320 __ imull(eax, in1_lo);
3321 // in1.hi <- in1.hi * in2.lo
3322 __ imull(in1_hi, in2_lo);
3323 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3324 __ addl(in1_hi, eax);
3325 // move in1_lo to eax to prepare for double precision
3326 __ movl(eax, in1_lo);
3327 // edx:eax <- in1.lo * in2.lo
3328 __ mull(in2_lo);
3329 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3330 __ addl(in1_hi, edx);
3331 // in1.lo <- (in1.lo * in2.lo)[31:0];
3332 __ movl(in1_lo, eax);
3333 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003334
3335 break;
3336 }
3337
Calin Juravleb5bfa962014-10-21 18:02:24 +01003338 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003339 DCHECK(first.Equals(locations->Out()));
3340 if (second.IsFpuRegister()) {
3341 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3342 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3343 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003344 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003345 __ mulss(first.AsFpuRegister<XmmRegister>(),
3346 codegen_->LiteralFloatAddress(
3347 const_area->GetConstant()->AsFloatConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003348 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003349 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3350 } else {
3351 DCHECK(second.IsStackSlot());
3352 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3353 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003354 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003355 }
3356
3357 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003358 DCHECK(first.Equals(locations->Out()));
3359 if (second.IsFpuRegister()) {
3360 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3361 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3362 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003363 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003364 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3365 codegen_->LiteralDoubleAddress(
3366 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003367 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003368 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3369 } else {
3370 DCHECK(second.IsDoubleStackSlot());
3371 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3372 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003373 break;
3374 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003375
3376 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003377 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003378 }
3379}
3380
Roland Levillain232ade02015-04-20 15:14:36 +01003381void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3382 uint32_t temp_offset,
3383 uint32_t stack_adjustment,
3384 bool is_fp,
3385 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003386 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003387 DCHECK(!is_wide);
3388 if (is_fp) {
3389 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3390 } else {
3391 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3392 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003393 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003394 DCHECK(is_wide);
3395 if (is_fp) {
3396 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3397 } else {
3398 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3399 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003400 } else {
3401 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003402 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003403 Location stack_temp = Location::StackSlot(temp_offset);
3404 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003405 if (is_fp) {
3406 __ flds(Address(ESP, temp_offset));
3407 } else {
3408 __ filds(Address(ESP, temp_offset));
3409 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003410 } else {
3411 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3412 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003413 if (is_fp) {
3414 __ fldl(Address(ESP, temp_offset));
3415 } else {
3416 __ fildl(Address(ESP, temp_offset));
3417 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003418 }
3419 }
3420}
3421
3422void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
3423 Primitive::Type type = rem->GetResultType();
3424 bool is_float = type == Primitive::kPrimFloat;
3425 size_t elem_size = Primitive::ComponentSize(type);
3426 LocationSummary* locations = rem->GetLocations();
3427 Location first = locations->InAt(0);
3428 Location second = locations->InAt(1);
3429 Location out = locations->Out();
3430
3431 // Create stack space for 2 elements.
3432 // TODO: enhance register allocator to ask for stack temporaries.
3433 __ subl(ESP, Immediate(2 * elem_size));
3434
3435 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003436 const bool is_wide = !is_float;
3437 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
3438 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003439
3440 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003441 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003442 __ Bind(&retry);
3443 __ fprem();
3444
3445 // Move FP status to AX.
3446 __ fstsw();
3447
3448 // And see if the argument reduction is complete. This is signaled by the
3449 // C2 FPU flag bit set to 0.
3450 __ andl(EAX, Immediate(kC2ConditionMask));
3451 __ j(kNotEqual, &retry);
3452
3453 // We have settled on the final value. Retrieve it into an XMM register.
3454 // Store FP top of stack to real stack.
3455 if (is_float) {
3456 __ fsts(Address(ESP, 0));
3457 } else {
3458 __ fstl(Address(ESP, 0));
3459 }
3460
3461 // Pop the 2 items from the FP stack.
3462 __ fucompp();
3463
3464 // Load the value from the stack into an XMM register.
3465 DCHECK(out.IsFpuRegister()) << out;
3466 if (is_float) {
3467 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3468 } else {
3469 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3470 }
3471
3472 // And remove the temporary stack space we allocated.
3473 __ addl(ESP, Immediate(2 * elem_size));
3474}
3475
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003476
3477void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3478 DCHECK(instruction->IsDiv() || instruction->IsRem());
3479
3480 LocationSummary* locations = instruction->GetLocations();
3481 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003482 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003483
3484 Register out_register = locations->Out().AsRegister<Register>();
3485 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003486 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003487
3488 DCHECK(imm == 1 || imm == -1);
3489
3490 if (instruction->IsRem()) {
3491 __ xorl(out_register, out_register);
3492 } else {
3493 __ movl(out_register, input_register);
3494 if (imm == -1) {
3495 __ negl(out_register);
3496 }
3497 }
3498}
3499
3500
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003501void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003502 LocationSummary* locations = instruction->GetLocations();
3503
3504 Register out_register = locations->Out().AsRegister<Register>();
3505 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003506 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003507 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3508 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003509
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003510 Register num = locations->GetTemp(0).AsRegister<Register>();
3511
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003512 __ leal(num, Address(input_register, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003513 __ testl(input_register, input_register);
3514 __ cmovl(kGreaterEqual, num, input_register);
3515 int shift = CTZ(imm);
3516 __ sarl(num, Immediate(shift));
3517
3518 if (imm < 0) {
3519 __ negl(num);
3520 }
3521
3522 __ movl(out_register, num);
3523}
3524
3525void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3526 DCHECK(instruction->IsDiv() || instruction->IsRem());
3527
3528 LocationSummary* locations = instruction->GetLocations();
3529 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3530
3531 Register eax = locations->InAt(0).AsRegister<Register>();
3532 Register out = locations->Out().AsRegister<Register>();
3533 Register num;
3534 Register edx;
3535
3536 if (instruction->IsDiv()) {
3537 edx = locations->GetTemp(0).AsRegister<Register>();
3538 num = locations->GetTemp(1).AsRegister<Register>();
3539 } else {
3540 edx = locations->Out().AsRegister<Register>();
3541 num = locations->GetTemp(0).AsRegister<Register>();
3542 }
3543
3544 DCHECK_EQ(EAX, eax);
3545 DCHECK_EQ(EDX, edx);
3546 if (instruction->IsDiv()) {
3547 DCHECK_EQ(EAX, out);
3548 } else {
3549 DCHECK_EQ(EDX, out);
3550 }
3551
3552 int64_t magic;
3553 int shift;
3554 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3555
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003556 // Save the numerator.
3557 __ movl(num, eax);
3558
3559 // EAX = magic
3560 __ movl(eax, Immediate(magic));
3561
3562 // EDX:EAX = magic * numerator
3563 __ imull(num);
3564
3565 if (imm > 0 && magic < 0) {
3566 // EDX += num
3567 __ addl(edx, num);
3568 } else if (imm < 0 && magic > 0) {
3569 __ subl(edx, num);
3570 }
3571
3572 // Shift if needed.
3573 if (shift != 0) {
3574 __ sarl(edx, Immediate(shift));
3575 }
3576
3577 // EDX += 1 if EDX < 0
3578 __ movl(eax, edx);
3579 __ shrl(edx, Immediate(31));
3580 __ addl(edx, eax);
3581
3582 if (instruction->IsRem()) {
3583 __ movl(eax, num);
3584 __ imull(edx, Immediate(imm));
3585 __ subl(eax, edx);
3586 __ movl(edx, eax);
3587 } else {
3588 __ movl(eax, edx);
3589 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003590}
3591
Calin Juravlebacfec32014-11-14 15:54:36 +00003592void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3593 DCHECK(instruction->IsDiv() || instruction->IsRem());
3594
3595 LocationSummary* locations = instruction->GetLocations();
3596 Location out = locations->Out();
3597 Location first = locations->InAt(0);
3598 Location second = locations->InAt(1);
3599 bool is_div = instruction->IsDiv();
3600
3601 switch (instruction->GetResultType()) {
3602 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003603 DCHECK_EQ(EAX, first.AsRegister<Register>());
3604 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003605
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003606 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003607 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003608
3609 if (imm == 0) {
3610 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3611 } else if (imm == 1 || imm == -1) {
3612 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003613 } else if (is_div && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003614 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003615 } else {
3616 DCHECK(imm <= -2 || imm >= 2);
3617 GenerateDivRemWithAnyConstant(instruction);
3618 }
3619 } else {
David Srbecky9cd6d372016-02-09 15:24:47 +00003620 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(
3621 instruction, out.AsRegister<Register>(), is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003622 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003623
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003624 Register second_reg = second.AsRegister<Register>();
3625 // 0x80000000/-1 triggers an arithmetic exception!
3626 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3627 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003628
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003629 __ cmpl(second_reg, Immediate(-1));
3630 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003631
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003632 // edx:eax <- sign-extended of eax
3633 __ cdq();
3634 // eax = quotient, edx = remainder
3635 __ idivl(second_reg);
3636 __ Bind(slow_path->GetExitLabel());
3637 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003638 break;
3639 }
3640
3641 case Primitive::kPrimLong: {
3642 InvokeRuntimeCallingConvention calling_convention;
3643 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3644 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3645 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3646 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3647 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3648 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3649
3650 if (is_div) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003651 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003652 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003653 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003654 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003655 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003656 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003657 break;
3658 }
3659
3660 default:
3661 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3662 }
3663}
3664
Calin Juravle7c4954d2014-10-28 16:57:40 +00003665void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003666 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003667 ? LocationSummary::kCallOnMainOnly
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003668 : LocationSummary::kNoCall;
3669 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3670
Calin Juravle7c4954d2014-10-28 16:57:40 +00003671 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003672 case Primitive::kPrimInt: {
3673 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003674 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003675 locations->SetOut(Location::SameAsFirstInput());
3676 // Intel uses edx:eax as the dividend.
3677 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003678 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3679 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3680 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003681 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003682 locations->AddTemp(Location::RequiresRegister());
3683 }
Calin Juravled0d48522014-11-04 16:40:20 +00003684 break;
3685 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003686 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003687 InvokeRuntimeCallingConvention calling_convention;
3688 locations->SetInAt(0, Location::RegisterPairLocation(
3689 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3690 locations->SetInAt(1, Location::RegisterPairLocation(
3691 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3692 // Runtime helper puts the result in EAX, EDX.
3693 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003694 break;
3695 }
3696 case Primitive::kPrimFloat:
3697 case Primitive::kPrimDouble: {
3698 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003699 if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3700 DCHECK(div->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003701 } else if (div->InputAt(1)->IsConstant()) {
3702 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003703 } else {
3704 locations->SetInAt(1, Location::Any());
3705 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003706 locations->SetOut(Location::SameAsFirstInput());
3707 break;
3708 }
3709
3710 default:
3711 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3712 }
3713}
3714
3715void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3716 LocationSummary* locations = div->GetLocations();
3717 Location first = locations->InAt(0);
3718 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003719
3720 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003721 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00003722 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003723 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003724 break;
3725 }
3726
3727 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003728 if (second.IsFpuRegister()) {
3729 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3730 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3731 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003732 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003733 __ divss(first.AsFpuRegister<XmmRegister>(),
3734 codegen_->LiteralFloatAddress(
3735 const_area->GetConstant()->AsFloatConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003736 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003737 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3738 } else {
3739 DCHECK(second.IsStackSlot());
3740 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3741 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003742 break;
3743 }
3744
3745 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003746 if (second.IsFpuRegister()) {
3747 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3748 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3749 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003750 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003751 __ divsd(first.AsFpuRegister<XmmRegister>(),
3752 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003753 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3754 const_area->GetBaseMethodAddress(),
3755 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003756 } else {
3757 DCHECK(second.IsDoubleStackSlot());
3758 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3759 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003760 break;
3761 }
3762
3763 default:
3764 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3765 }
3766}
3767
Calin Juravlebacfec32014-11-14 15:54:36 +00003768void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003769 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003770
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003771 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003772 ? LocationSummary::kCallOnMainOnly
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003773 : LocationSummary::kNoCall;
3774 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003775
Calin Juravled2ec87d2014-12-08 14:24:46 +00003776 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003777 case Primitive::kPrimInt: {
3778 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003779 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003780 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003781 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3782 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3783 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003784 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003785 locations->AddTemp(Location::RequiresRegister());
3786 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003787 break;
3788 }
3789 case Primitive::kPrimLong: {
3790 InvokeRuntimeCallingConvention calling_convention;
3791 locations->SetInAt(0, Location::RegisterPairLocation(
3792 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3793 locations->SetInAt(1, Location::RegisterPairLocation(
3794 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3795 // Runtime helper puts the result in EAX, EDX.
3796 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3797 break;
3798 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003799 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003800 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003801 locations->SetInAt(0, Location::Any());
3802 locations->SetInAt(1, Location::Any());
3803 locations->SetOut(Location::RequiresFpuRegister());
3804 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003805 break;
3806 }
3807
3808 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003809 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003810 }
3811}
3812
3813void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
3814 Primitive::Type type = rem->GetResultType();
3815 switch (type) {
3816 case Primitive::kPrimInt:
3817 case Primitive::kPrimLong: {
3818 GenerateDivRemIntegral(rem);
3819 break;
3820 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003821 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00003822 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003823 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003824 break;
3825 }
3826 default:
3827 LOG(FATAL) << "Unexpected rem type " << type;
3828 }
3829}
3830
Calin Juravled0d48522014-11-04 16:40:20 +00003831void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003832 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003833 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003834 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003835 case Primitive::kPrimByte:
3836 case Primitive::kPrimChar:
3837 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003838 case Primitive::kPrimInt: {
3839 locations->SetInAt(0, Location::Any());
3840 break;
3841 }
3842 case Primitive::kPrimLong: {
3843 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
3844 if (!instruction->IsConstant()) {
3845 locations->AddTemp(Location::RequiresRegister());
3846 }
3847 break;
3848 }
3849 default:
3850 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3851 }
Calin Juravled0d48522014-11-04 16:40:20 +00003852}
3853
3854void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003855 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003856 codegen_->AddSlowPath(slow_path);
3857
3858 LocationSummary* locations = instruction->GetLocations();
3859 Location value = locations->InAt(0);
3860
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003861 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003862 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003863 case Primitive::kPrimByte:
3864 case Primitive::kPrimChar:
3865 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003866 case Primitive::kPrimInt: {
3867 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003868 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003869 __ j(kEqual, slow_path->GetEntryLabel());
3870 } else if (value.IsStackSlot()) {
3871 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
3872 __ j(kEqual, slow_path->GetEntryLabel());
3873 } else {
3874 DCHECK(value.IsConstant()) << value;
3875 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01003876 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003877 }
3878 }
3879 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003880 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003881 case Primitive::kPrimLong: {
3882 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003883 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003884 __ movl(temp, value.AsRegisterPairLow<Register>());
3885 __ orl(temp, value.AsRegisterPairHigh<Register>());
3886 __ j(kEqual, slow_path->GetEntryLabel());
3887 } else {
3888 DCHECK(value.IsConstant()) << value;
3889 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3890 __ jmp(slow_path->GetEntryLabel());
3891 }
3892 }
3893 break;
3894 }
3895 default:
3896 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003897 }
Calin Juravled0d48522014-11-04 16:40:20 +00003898}
3899
Calin Juravle9aec02f2014-11-18 23:06:35 +00003900void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3901 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3902
3903 LocationSummary* locations =
3904 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3905
3906 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00003907 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00003908 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003909 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003910 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003911 // The shift count needs to be in CL or a constant.
3912 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003913 locations->SetOut(Location::SameAsFirstInput());
3914 break;
3915 }
3916 default:
3917 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3918 }
3919}
3920
3921void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3922 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3923
3924 LocationSummary* locations = op->GetLocations();
3925 Location first = locations->InAt(0);
3926 Location second = locations->InAt(1);
3927 DCHECK(first.Equals(locations->Out()));
3928
3929 switch (op->GetResultType()) {
3930 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00003931 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003932 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003933 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003934 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003935 DCHECK_EQ(ECX, second_reg);
3936 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003937 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003938 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003939 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003940 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003941 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003942 }
3943 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003944 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003945 if (shift == 0) {
3946 return;
3947 }
3948 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003949 if (op->IsShl()) {
3950 __ shll(first_reg, imm);
3951 } else if (op->IsShr()) {
3952 __ sarl(first_reg, imm);
3953 } else {
3954 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003955 }
3956 }
3957 break;
3958 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003959 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003960 if (second.IsRegister()) {
3961 Register second_reg = second.AsRegister<Register>();
3962 DCHECK_EQ(ECX, second_reg);
3963 if (op->IsShl()) {
3964 GenerateShlLong(first, second_reg);
3965 } else if (op->IsShr()) {
3966 GenerateShrLong(first, second_reg);
3967 } else {
3968 GenerateUShrLong(first, second_reg);
3969 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003970 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003971 // Shift by a constant.
Roland Levillain5b5b9312016-03-22 14:57:31 +00003972 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003973 // Nothing to do if the shift is 0, as the input is already the output.
3974 if (shift != 0) {
3975 if (op->IsShl()) {
3976 GenerateShlLong(first, shift);
3977 } else if (op->IsShr()) {
3978 GenerateShrLong(first, shift);
3979 } else {
3980 GenerateUShrLong(first, shift);
3981 }
3982 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003983 }
3984 break;
3985 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003986 default:
3987 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3988 }
3989}
3990
Mark P Mendell73945692015-04-29 14:56:17 +00003991void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3992 Register low = loc.AsRegisterPairLow<Register>();
3993 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003994 if (shift == 1) {
3995 // This is just an addition.
3996 __ addl(low, low);
3997 __ adcl(high, high);
3998 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003999 // Shift by 32 is easy. High gets low, and low gets 0.
4000 codegen_->EmitParallelMoves(
4001 loc.ToLow(),
4002 loc.ToHigh(),
4003 Primitive::kPrimInt,
4004 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
4005 loc.ToLow(),
4006 Primitive::kPrimInt);
4007 } else if (shift > 32) {
4008 // Low part becomes 0. High part is low part << (shift-32).
4009 __ movl(high, low);
4010 __ shll(high, Immediate(shift - 32));
4011 __ xorl(low, low);
4012 } else {
4013 // Between 1 and 31.
4014 __ shld(high, low, Immediate(shift));
4015 __ shll(low, Immediate(shift));
4016 }
4017}
4018
Calin Juravle9aec02f2014-11-18 23:06:35 +00004019void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004020 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004021 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
4022 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
4023 __ testl(shifter, Immediate(32));
4024 __ j(kEqual, &done);
4025 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
4026 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
4027 __ Bind(&done);
4028}
4029
Mark P Mendell73945692015-04-29 14:56:17 +00004030void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
4031 Register low = loc.AsRegisterPairLow<Register>();
4032 Register high = loc.AsRegisterPairHigh<Register>();
4033 if (shift == 32) {
4034 // Need to copy the sign.
4035 DCHECK_NE(low, high);
4036 __ movl(low, high);
4037 __ sarl(high, Immediate(31));
4038 } else if (shift > 32) {
4039 DCHECK_NE(low, high);
4040 // High part becomes sign. Low part is shifted by shift - 32.
4041 __ movl(low, high);
4042 __ sarl(high, Immediate(31));
4043 __ sarl(low, Immediate(shift - 32));
4044 } else {
4045 // Between 1 and 31.
4046 __ shrd(low, high, Immediate(shift));
4047 __ sarl(high, Immediate(shift));
4048 }
4049}
4050
Calin Juravle9aec02f2014-11-18 23:06:35 +00004051void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004052 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004053 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4054 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
4055 __ testl(shifter, Immediate(32));
4056 __ j(kEqual, &done);
4057 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4058 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
4059 __ Bind(&done);
4060}
4061
Mark P Mendell73945692015-04-29 14:56:17 +00004062void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
4063 Register low = loc.AsRegisterPairLow<Register>();
4064 Register high = loc.AsRegisterPairHigh<Register>();
4065 if (shift == 32) {
4066 // Shift by 32 is easy. Low gets high, and high gets 0.
4067 codegen_->EmitParallelMoves(
4068 loc.ToHigh(),
4069 loc.ToLow(),
4070 Primitive::kPrimInt,
4071 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
4072 loc.ToHigh(),
4073 Primitive::kPrimInt);
4074 } else if (shift > 32) {
4075 // Low part is high >> (shift - 32). High part becomes 0.
4076 __ movl(low, high);
4077 __ shrl(low, Immediate(shift - 32));
4078 __ xorl(high, high);
4079 } else {
4080 // Between 1 and 31.
4081 __ shrd(low, high, Immediate(shift));
4082 __ shrl(high, Immediate(shift));
4083 }
4084}
4085
Calin Juravle9aec02f2014-11-18 23:06:35 +00004086void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004087 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004088 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4089 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
4090 __ testl(shifter, Immediate(32));
4091 __ j(kEqual, &done);
4092 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4093 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
4094 __ Bind(&done);
4095}
4096
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004097void LocationsBuilderX86::VisitRor(HRor* ror) {
4098 LocationSummary* locations =
4099 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
4100
4101 switch (ror->GetResultType()) {
4102 case Primitive::kPrimLong:
4103 // Add the temporary needed.
4104 locations->AddTemp(Location::RequiresRegister());
4105 FALLTHROUGH_INTENDED;
4106 case Primitive::kPrimInt:
4107 locations->SetInAt(0, Location::RequiresRegister());
4108 // The shift count needs to be in CL (unless it is a constant).
4109 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
4110 locations->SetOut(Location::SameAsFirstInput());
4111 break;
4112 default:
4113 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4114 UNREACHABLE();
4115 }
4116}
4117
4118void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
4119 LocationSummary* locations = ror->GetLocations();
4120 Location first = locations->InAt(0);
4121 Location second = locations->InAt(1);
4122
4123 if (ror->GetResultType() == Primitive::kPrimInt) {
4124 Register first_reg = first.AsRegister<Register>();
4125 if (second.IsRegister()) {
4126 Register second_reg = second.AsRegister<Register>();
4127 __ rorl(first_reg, second_reg);
4128 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004129 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004130 __ rorl(first_reg, imm);
4131 }
4132 return;
4133 }
4134
4135 DCHECK_EQ(ror->GetResultType(), Primitive::kPrimLong);
4136 Register first_reg_lo = first.AsRegisterPairLow<Register>();
4137 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
4138 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
4139 if (second.IsRegister()) {
4140 Register second_reg = second.AsRegister<Register>();
4141 DCHECK_EQ(second_reg, ECX);
4142 __ movl(temp_reg, first_reg_hi);
4143 __ shrd(first_reg_hi, first_reg_lo, second_reg);
4144 __ shrd(first_reg_lo, temp_reg, second_reg);
4145 __ movl(temp_reg, first_reg_hi);
4146 __ testl(second_reg, Immediate(32));
4147 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
4148 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
4149 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004150 int32_t shift_amt = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004151 if (shift_amt == 0) {
4152 // Already fine.
4153 return;
4154 }
4155 if (shift_amt == 32) {
4156 // Just swap.
4157 __ movl(temp_reg, first_reg_lo);
4158 __ movl(first_reg_lo, first_reg_hi);
4159 __ movl(first_reg_hi, temp_reg);
4160 return;
4161 }
4162
4163 Immediate imm(shift_amt);
4164 // Save the constents of the low value.
4165 __ movl(temp_reg, first_reg_lo);
4166
4167 // Shift right into low, feeding bits from high.
4168 __ shrd(first_reg_lo, first_reg_hi, imm);
4169
4170 // Shift right into high, feeding bits from the original low.
4171 __ shrd(first_reg_hi, temp_reg, imm);
4172
4173 // Swap if needed.
4174 if (shift_amt > 32) {
4175 __ movl(temp_reg, first_reg_lo);
4176 __ movl(first_reg_lo, first_reg_hi);
4177 __ movl(first_reg_hi, temp_reg);
4178 }
4179 }
4180}
4181
Calin Juravle9aec02f2014-11-18 23:06:35 +00004182void LocationsBuilderX86::VisitShl(HShl* shl) {
4183 HandleShift(shl);
4184}
4185
4186void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
4187 HandleShift(shl);
4188}
4189
4190void LocationsBuilderX86::VisitShr(HShr* shr) {
4191 HandleShift(shr);
4192}
4193
4194void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
4195 HandleShift(shr);
4196}
4197
4198void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
4199 HandleShift(ushr);
4200}
4201
4202void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
4203 HandleShift(ushr);
4204}
4205
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004206void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004207 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004208 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004209 locations->SetOut(Location::RegisterLocation(EAX));
David Brazdil6de19382016-01-08 17:37:10 +00004210 if (instruction->IsStringAlloc()) {
4211 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4212 } else {
4213 InvokeRuntimeCallingConvention calling_convention;
4214 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00004215 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004216}
4217
4218void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004219 // Note: if heap poisoning is enabled, the entry point takes cares
4220 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004221 if (instruction->IsStringAlloc()) {
4222 // String is allocated through StringFactory. Call NewEmptyString entry point.
4223 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07004224 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004225 __ fs()->movl(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString)));
4226 __ call(Address(temp, code_offset.Int32Value()));
4227 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4228 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01004229 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00004230 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00004231 DCHECK(!codegen_->IsLeafMethod());
4232 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004233}
4234
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004235void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
4236 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004237 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004238 locations->SetOut(Location::RegisterLocation(EAX));
4239 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004240 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4241 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004242}
4243
4244void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004245 // Note: if heap poisoning is enabled, the entry point takes cares
4246 // of poisoning the reference.
Nicolas Geoffrayd0958442017-01-30 14:57:16 +00004247 QuickEntrypointEnum entrypoint =
4248 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
4249 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004250 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004251 DCHECK(!codegen_->IsLeafMethod());
4252}
4253
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004254void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004255 LocationSummary* locations =
4256 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004257 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4258 if (location.IsStackSlot()) {
4259 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4260 } else if (location.IsDoubleStackSlot()) {
4261 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004262 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004263 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004264}
4265
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004266void InstructionCodeGeneratorX86::VisitParameterValue(
4267 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4268}
4269
4270void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
4271 LocationSummary* locations =
4272 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4273 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4274}
4275
4276void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004277}
4278
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004279void LocationsBuilderX86::VisitClassTableGet(HClassTableGet* instruction) {
4280 LocationSummary* locations =
4281 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4282 locations->SetInAt(0, Location::RequiresRegister());
4283 locations->SetOut(Location::RequiresRegister());
4284}
4285
4286void InstructionCodeGeneratorX86::VisitClassTableGet(HClassTableGet* instruction) {
4287 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004288 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004289 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004290 instruction->GetIndex(), kX86PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004291 __ movl(locations->Out().AsRegister<Register>(),
4292 Address(locations->InAt(0).AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004293 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004294 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004295 instruction->GetIndex(), kX86PointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004296 __ movl(locations->Out().AsRegister<Register>(),
4297 Address(locations->InAt(0).AsRegister<Register>(),
4298 mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
4299 // temp = temp->GetImtEntryAt(method_offset);
4300 __ movl(locations->Out().AsRegister<Register>(),
4301 Address(locations->Out().AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004302 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004303}
4304
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004305void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004306 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004307 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004308 locations->SetInAt(0, Location::RequiresRegister());
4309 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004310}
4311
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004312void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
4313 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01004314 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004315 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01004316 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004317 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004318 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004319 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004320 break;
4321
4322 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01004323 __ notl(out.AsRegisterPairLow<Register>());
4324 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004325 break;
4326
4327 default:
4328 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4329 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004330}
4331
David Brazdil66d126e2015-04-03 16:02:44 +01004332void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
4333 LocationSummary* locations =
4334 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4335 locations->SetInAt(0, Location::RequiresRegister());
4336 locations->SetOut(Location::SameAsFirstInput());
4337}
4338
4339void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004340 LocationSummary* locations = bool_not->GetLocations();
4341 Location in = locations->InAt(0);
4342 Location out = locations->Out();
4343 DCHECK(in.Equals(out));
4344 __ xorl(out.AsRegister<Register>(), Immediate(1));
4345}
4346
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004347void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004348 LocationSummary* locations =
4349 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004350 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004351 case Primitive::kPrimBoolean:
4352 case Primitive::kPrimByte:
4353 case Primitive::kPrimShort:
4354 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004355 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00004356 case Primitive::kPrimLong: {
4357 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00004358 locations->SetInAt(1, Location::Any());
4359 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4360 break;
4361 }
4362 case Primitive::kPrimFloat:
4363 case Primitive::kPrimDouble: {
4364 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004365 if (compare->InputAt(1)->IsX86LoadFromConstantTable()) {
4366 DCHECK(compare->InputAt(1)->IsEmittedAtUseSite());
4367 } else if (compare->InputAt(1)->IsConstant()) {
4368 locations->SetInAt(1, Location::RequiresFpuRegister());
4369 } else {
4370 locations->SetInAt(1, Location::Any());
4371 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004372 locations->SetOut(Location::RequiresRegister());
4373 break;
4374 }
4375 default:
4376 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4377 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004378}
4379
4380void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004381 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004382 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004383 Location left = locations->InAt(0);
4384 Location right = locations->InAt(1);
4385
Mark Mendell0c9497d2015-08-21 09:30:05 -04004386 NearLabel less, greater, done;
Aart Bika19616e2016-02-01 18:57:58 -08004387 Condition less_cond = kLess;
4388
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004389 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004390 case Primitive::kPrimBoolean:
4391 case Primitive::kPrimByte:
4392 case Primitive::kPrimShort:
4393 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004394 case Primitive::kPrimInt: {
Roland Levillain0b671c02016-08-19 12:02:34 +01004395 codegen_->GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08004396 break;
4397 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004398 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004399 Register left_low = left.AsRegisterPairLow<Register>();
4400 Register left_high = left.AsRegisterPairHigh<Register>();
4401 int32_t val_low = 0;
4402 int32_t val_high = 0;
4403 bool right_is_const = false;
4404
4405 if (right.IsConstant()) {
4406 DCHECK(right.GetConstant()->IsLongConstant());
4407 right_is_const = true;
4408 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
4409 val_low = Low32Bits(val);
4410 val_high = High32Bits(val);
4411 }
4412
Calin Juravleddb7df22014-11-25 20:56:51 +00004413 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004414 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004415 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004416 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004417 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004418 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004419 codegen_->Compare32BitValue(left_high, val_high);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004420 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004421 __ j(kLess, &less); // Signed compare.
4422 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004423 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004424 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004425 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004426 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004427 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004428 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004429 codegen_->Compare32BitValue(left_low, val_low);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004430 }
Aart Bika19616e2016-02-01 18:57:58 -08004431 less_cond = kBelow; // for CF (unsigned).
Calin Juravleddb7df22014-11-25 20:56:51 +00004432 break;
4433 }
4434 case Primitive::kPrimFloat: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004435 GenerateFPCompare(left, right, compare, false);
Calin Juravleddb7df22014-11-25 20:56:51 +00004436 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004437 less_cond = kBelow; // for CF (floats).
Calin Juravleddb7df22014-11-25 20:56:51 +00004438 break;
4439 }
4440 case Primitive::kPrimDouble: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004441 GenerateFPCompare(left, right, compare, true);
Calin Juravleddb7df22014-11-25 20:56:51 +00004442 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004443 less_cond = kBelow; // for CF (floats).
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004444 break;
4445 }
4446 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004447 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004448 }
Aart Bika19616e2016-02-01 18:57:58 -08004449
Calin Juravleddb7df22014-11-25 20:56:51 +00004450 __ movl(out, Immediate(0));
4451 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08004452 __ j(less_cond, &less);
Calin Juravleddb7df22014-11-25 20:56:51 +00004453
4454 __ Bind(&greater);
4455 __ movl(out, Immediate(1));
4456 __ jmp(&done);
4457
4458 __ Bind(&less);
4459 __ movl(out, Immediate(-1));
4460
4461 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004462}
4463
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004464void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004465 LocationSummary* locations =
4466 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004467 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004468 locations->SetInAt(i, Location::Any());
4469 }
4470 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004471}
4472
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004473void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004474 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004475}
4476
Roland Levillain7c1559a2015-12-15 10:55:36 +00004477void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004478 /*
4479 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
4480 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
4481 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4482 */
4483 switch (kind) {
4484 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004485 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004486 break;
4487 }
4488 case MemBarrierKind::kAnyStore:
4489 case MemBarrierKind::kLoadAny:
4490 case MemBarrierKind::kStoreStore: {
4491 // nop
4492 break;
4493 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004494 case MemBarrierKind::kNTStoreStore:
4495 // Non-Temporal Store/Store needs an explicit fence.
4496 MemoryFence(/* non-temporal */ true);
4497 break;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004498 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004499}
4500
Vladimir Markodc151b22015-10-15 18:02:30 +01004501HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
4502 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004503 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004504 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004505}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004506
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004507Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4508 Register temp) {
4509 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Markoc53c0792015-11-19 15:48:33 +00004510 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004511 if (!invoke->GetLocations()->Intrinsified()) {
4512 return location.AsRegister<Register>();
4513 }
4514 // For intrinsics we allow any location, so it may be on the stack.
4515 if (!location.IsRegister()) {
4516 __ movl(temp, Address(ESP, location.GetStackIndex()));
4517 return temp;
4518 }
4519 // For register locations, check if the register was saved. If so, get it from the stack.
4520 // Note: There is a chance that the register was saved but not overwritten, so we could
4521 // save one load. However, since this is just an intrinsic slow path we prefer this
4522 // simple and more robust approach rather that trying to determine if that's the case.
4523 SlowPathCode* slow_path = GetCurrentSlowPath();
Serguei Katkov288c7a82016-05-16 11:53:15 +06004524 if (slow_path != nullptr) {
4525 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4526 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4527 __ movl(temp, Address(ESP, stack_offset));
4528 return temp;
4529 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004530 }
4531 return location.AsRegister<Register>();
4532}
4533
Serguei Katkov288c7a82016-05-16 11:53:15 +06004534Location CodeGeneratorX86::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
4535 Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00004536 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4537 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004538 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00004539 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004540 uint32_t offset =
4541 GetThreadOffset<kX86PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
4542 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004543 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004544 }
Vladimir Marko58155012015-08-19 12:49:41 +00004545 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004546 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004547 break;
Vladimir Marko65979462017-05-19 17:25:12 +01004548 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
4549 DCHECK(GetCompilerOptions().IsBootImage());
4550 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4551 temp.AsRegister<Register>());
4552 __ leal(temp.AsRegister<Register>(), Address(base_reg, CodeGeneratorX86::kDummy32BitOffset));
4553 RecordBootMethodPatch(invoke);
4554 break;
4555 }
Vladimir Marko58155012015-08-19 12:49:41 +00004556 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4557 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
4558 break;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004559 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4560 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4561 temp.AsRegister<Register>());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004562 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004563 // Bind a new fixup label at the end of the "movl" insn.
4564 uint32_t offset = invoke->GetDexCacheArrayOffset();
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004565 __ Bind(NewPcRelativeDexCacheArrayPatch(
4566 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress(),
4567 invoke->GetDexFileForPcRelativeDexCache(),
4568 offset));
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004569 break;
4570 }
Vladimir Marko58155012015-08-19 12:49:41 +00004571 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004572 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004573 Register method_reg;
4574 Register reg = temp.AsRegister<Register>();
4575 if (current_method.IsRegister()) {
4576 method_reg = current_method.AsRegister<Register>();
4577 } else {
David Brazdil58282f42016-01-14 12:45:10 +00004578 DCHECK(invoke->GetLocations()->Intrinsified());
Vladimir Marko58155012015-08-19 12:49:41 +00004579 DCHECK(!current_method.IsValid());
4580 method_reg = reg;
4581 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
4582 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004583 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004584 __ movl(reg, Address(method_reg,
4585 ArtMethod::DexCacheResolvedMethodsOffset(kX86PointerSize).Int32Value()));
Vladimir Marko40ecb122016-04-06 17:33:41 +01004586 // temp = temp[index_in_cache];
4587 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
4588 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00004589 __ movl(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
4590 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004591 }
Vladimir Marko58155012015-08-19 12:49:41 +00004592 }
Serguei Katkov288c7a82016-05-16 11:53:15 +06004593 return callee_method;
4594}
4595
4596void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4597 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +00004598
4599 switch (invoke->GetCodePtrLocation()) {
4600 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4601 __ call(GetFrameEntryLabel());
4602 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004603 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4604 // (callee_method + offset_of_quick_compiled_code)()
4605 __ call(Address(callee_method.AsRegister<Register>(),
4606 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07004607 kX86PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004608 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04004609 }
4610
4611 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04004612}
4613
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004614void CodeGeneratorX86::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
4615 Register temp = temp_in.AsRegister<Register>();
4616 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4617 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004618
4619 // Use the calling convention instead of the location of the receiver, as
4620 // intrinsics may have put the receiver in a different register. In the intrinsics
4621 // slow path, the arguments have been moved to the right place, so here we are
4622 // guaranteed that the receiver is the first register of the calling convention.
4623 InvokeDexCallingConvention calling_convention;
4624 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004625 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004626 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004627 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004628 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004629 // Instead of simply (possibly) unpoisoning `temp` here, we should
4630 // emit a read barrier for the previous class reference load.
4631 // However this is not required in practice, as this is an
4632 // intermediate/temporary reference and because the current
4633 // concurrent copying collector keeps the from-space memory
4634 // intact/accessible until the end of the marking phase (the
4635 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004636 __ MaybeUnpoisonHeapReference(temp);
4637 // temp = temp->GetMethodAt(method_offset);
4638 __ movl(temp, Address(temp, method_offset));
4639 // call temp->GetEntryPoint();
4640 __ call(Address(
Andreas Gampe542451c2016-07-26 09:02:02 -07004641 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004642}
4643
Vladimir Marko65979462017-05-19 17:25:12 +01004644void CodeGeneratorX86::RecordBootMethodPatch(HInvokeStaticOrDirect* invoke) {
4645 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
4646 HX86ComputeBaseMethodAddress* address =
4647 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
4648 boot_image_method_patches_.emplace_back(address,
4649 *invoke->GetTargetMethod().dex_file,
4650 invoke->GetTargetMethod().dex_method_index);
4651 __ Bind(&boot_image_method_patches_.back().label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004652}
4653
Vladimir Marko1998cd02017-01-13 13:02:58 +00004654void CodeGeneratorX86::RecordBootTypePatch(HLoadClass* load_class) {
Vladimir Marko00916b92017-05-17 17:45:45 +01004655 HX86ComputeBaseMethodAddress* address = load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004656 boot_image_type_patches_.emplace_back(address,
4657 load_class->GetDexFile(),
Vladimir Marko1998cd02017-01-13 13:02:58 +00004658 load_class->GetTypeIndex().index_);
4659 __ Bind(&boot_image_type_patches_.back().label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004660}
4661
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004662Label* CodeGeneratorX86::NewTypeBssEntryPatch(HLoadClass* load_class) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004663 HX86ComputeBaseMethodAddress* address =
4664 load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
4665 type_bss_entry_patches_.emplace_back(
4666 address, load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00004667 return &type_bss_entry_patches_.back().label;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004668}
4669
Vladimir Marko65979462017-05-19 17:25:12 +01004670void CodeGeneratorX86::RecordBootStringPatch(HLoadString* load_string) {
4671 DCHECK(GetCompilerOptions().IsBootImage());
4672 HX86ComputeBaseMethodAddress* address = load_string->InputAt(0)->AsX86ComputeBaseMethodAddress();
4673 string_patches_.emplace_back(address,
4674 load_string->GetDexFile(),
4675 load_string->GetStringIndex().index_);
4676 __ Bind(&string_patches_.back().label);
4677}
4678
Vladimir Markoaad75c62016-10-03 08:46:48 +00004679Label* CodeGeneratorX86::NewStringBssEntryPatch(HLoadString* load_string) {
4680 DCHECK(!GetCompilerOptions().IsBootImage());
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004681 HX86ComputeBaseMethodAddress* address =
4682 load_string->InputAt(0)->AsX86ComputeBaseMethodAddress();
4683 string_patches_.emplace_back(
4684 address, load_string->GetDexFile(), load_string->GetStringIndex().index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004685 return &string_patches_.back().label;
4686}
4687
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004688Label* CodeGeneratorX86::NewPcRelativeDexCacheArrayPatch(
4689 HX86ComputeBaseMethodAddress* method_address,
4690 const DexFile& dex_file,
4691 uint32_t element_offset) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004692 // Add the patch entry and bind its label at the end of the instruction.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004693 pc_relative_dex_cache_patches_.emplace_back(method_address, dex_file, element_offset);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004694 return &pc_relative_dex_cache_patches_.back().label;
4695}
4696
Vladimir Markoaad75c62016-10-03 08:46:48 +00004697// The label points to the end of the "movl" or another instruction but the literal offset
4698// for method patch needs to point to the embedded constant which occupies the last 4 bytes.
4699constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
4700
4701template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
4702inline void CodeGeneratorX86::EmitPcRelativeLinkerPatches(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004703 const ArenaDeque<X86PcRelativePatchInfo>& infos,
Vladimir Markoaad75c62016-10-03 08:46:48 +00004704 ArenaVector<LinkerPatch>* linker_patches) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004705 for (const X86PcRelativePatchInfo& info : infos) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00004706 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004707 linker_patches->push_back(Factory(
4708 literal_offset, &info.dex_file, GetMethodAddressOffset(info.method_address), info.index));
Vladimir Markoaad75c62016-10-03 08:46:48 +00004709 }
4710}
4711
Vladimir Marko58155012015-08-19 12:49:41 +00004712void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4713 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004714 size_t size =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004715 pc_relative_dex_cache_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01004716 boot_image_method_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +00004717 boot_image_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01004718 type_bss_entry_patches_.size() +
4719 string_patches_.size();
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004720 linker_patches->reserve(size);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004721 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
4722 linker_patches);
Vladimir Marko764d4542017-05-16 10:31:41 +01004723 if (GetCompilerOptions().IsBootImage()) {
Vladimir Marko65979462017-05-19 17:25:12 +01004724 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeMethodPatch>(boot_image_method_patches_,
4725 linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00004726 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(boot_image_type_patches_,
4727 linker_patches);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004728 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(string_patches_, linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004729 } else {
Vladimir Marko65979462017-05-19 17:25:12 +01004730 DCHECK(boot_image_method_patches_.empty());
Vladimir Marko764d4542017-05-16 10:31:41 +01004731 DCHECK(boot_image_type_patches_.empty());
4732 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(string_patches_, linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004733 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00004734 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
4735 linker_patches);
4736 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00004737}
4738
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004739void CodeGeneratorX86::MarkGCCard(Register temp,
4740 Register card,
4741 Register object,
4742 Register value,
4743 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004744 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004745 if (value_can_be_null) {
4746 __ testl(value, value);
4747 __ j(kEqual, &is_null);
4748 }
Andreas Gampe542451c2016-07-26 09:02:02 -07004749 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86PointerSize>().Int32Value()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004750 __ movl(temp, object);
4751 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004752 __ movb(Address(temp, card, TIMES_1, 0),
4753 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004754 if (value_can_be_null) {
4755 __ Bind(&is_null);
4756 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004757}
4758
Calin Juravle52c48962014-12-16 17:02:57 +00004759void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4760 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00004761
4762 bool object_field_get_with_read_barrier =
4763 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004764 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004765 new (GetGraph()->GetArena()) LocationSummary(instruction,
4766 kEmitCompilerReadBarrier ?
4767 LocationSummary::kCallOnSlowPath :
4768 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004769 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004770 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004771 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004772 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004773
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004774 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4775 locations->SetOut(Location::RequiresFpuRegister());
4776 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004777 // The output overlaps in case of long: we don't want the low move
4778 // to overwrite the object's location. Likewise, in the case of
4779 // an object field get with read barriers enabled, we do not want
4780 // the move to overwrite the object's location, as we need it to emit
4781 // the read barrier.
4782 locations->SetOut(
4783 Location::RequiresRegister(),
4784 (object_field_get_with_read_barrier || instruction->GetType() == Primitive::kPrimLong) ?
4785 Location::kOutputOverlap :
4786 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004787 }
Calin Juravle52c48962014-12-16 17:02:57 +00004788
4789 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
4790 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00004791 // So we use an XMM register as a temp to achieve atomicity (first
4792 // load the temp into the XMM and then copy the XMM into the
4793 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00004794 locations->AddTemp(Location::RequiresFpuRegister());
4795 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004796}
4797
Calin Juravle52c48962014-12-16 17:02:57 +00004798void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
4799 const FieldInfo& field_info) {
4800 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004801
Calin Juravle52c48962014-12-16 17:02:57 +00004802 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004803 Location base_loc = locations->InAt(0);
4804 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004805 Location out = locations->Out();
4806 bool is_volatile = field_info.IsVolatile();
4807 Primitive::Type field_type = field_info.GetFieldType();
4808 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4809
4810 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004811 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00004812 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004813 break;
4814 }
4815
4816 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00004817 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004818 break;
4819 }
4820
4821 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00004822 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004823 break;
4824 }
4825
4826 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00004827 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004828 break;
4829 }
4830
4831 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004832 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004833 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00004834
4835 case Primitive::kPrimNot: {
4836 // /* HeapReference<Object> */ out = *(base + offset)
4837 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004838 // Note that a potential implicit null check is handled in this
4839 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4840 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00004841 instruction, out, base, offset, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00004842 if (is_volatile) {
4843 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4844 }
4845 } else {
4846 __ movl(out.AsRegister<Register>(), Address(base, offset));
4847 codegen_->MaybeRecordImplicitNullCheck(instruction);
4848 if (is_volatile) {
4849 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4850 }
4851 // If read barriers are enabled, emit read barriers other than
4852 // Baker's using a slow path (and also unpoison the loaded
4853 // reference, if heap poisoning is enabled).
4854 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4855 }
4856 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004857 }
4858
4859 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00004860 if (is_volatile) {
4861 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4862 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004863 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004864 __ movd(out.AsRegisterPairLow<Register>(), temp);
4865 __ psrlq(temp, Immediate(32));
4866 __ movd(out.AsRegisterPairHigh<Register>(), temp);
4867 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004868 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00004869 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004870 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004871 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
4872 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004873 break;
4874 }
4875
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004876 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004877 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004878 break;
4879 }
4880
4881 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004882 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004883 break;
4884 }
4885
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004886 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004887 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004888 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004889 }
Calin Juravle52c48962014-12-16 17:02:57 +00004890
Roland Levillain7c1559a2015-12-15 10:55:36 +00004891 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimLong) {
4892 // Potential implicit null checks, in the case of reference or
4893 // long fields, are handled in the previous switch statement.
4894 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004895 codegen_->MaybeRecordImplicitNullCheck(instruction);
4896 }
4897
Calin Juravle52c48962014-12-16 17:02:57 +00004898 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004899 if (field_type == Primitive::kPrimNot) {
4900 // Memory barriers, in the case of references, are also handled
4901 // in the previous switch statement.
4902 } else {
4903 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4904 }
Roland Levillain4d027112015-07-01 15:41:14 +01004905 }
Calin Juravle52c48962014-12-16 17:02:57 +00004906}
4907
4908void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4909 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4910
4911 LocationSummary* locations =
4912 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4913 locations->SetInAt(0, Location::RequiresRegister());
4914 bool is_volatile = field_info.IsVolatile();
4915 Primitive::Type field_type = field_info.GetFieldType();
4916 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
4917 || (field_type == Primitive::kPrimByte);
4918
4919 // The register allocator does not support multiple
4920 // inputs that die at entry with one in a specific register.
4921 if (is_byte_type) {
4922 // Ensure the value is in a byte register.
4923 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004924 } else if (Primitive::IsFloatingPointType(field_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05004925 if (is_volatile && field_type == Primitive::kPrimDouble) {
4926 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4927 locations->SetInAt(1, Location::RequiresFpuRegister());
4928 } else {
4929 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4930 }
4931 } else if (is_volatile && field_type == Primitive::kPrimLong) {
4932 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00004933 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004934
Calin Juravle52c48962014-12-16 17:02:57 +00004935 // 64bits value can be atomically written to an address with movsd and an XMM register.
4936 // We need two XMM registers because there's no easier way to (bit) copy a register pair
4937 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
4938 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
4939 // isolated cases when we need this it isn't worth adding the extra complexity.
4940 locations->AddTemp(Location::RequiresFpuRegister());
4941 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004942 } else {
4943 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4944
4945 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4946 // Temporary registers for the write barrier.
4947 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
4948 // Ensure the card is in a byte register.
4949 locations->AddTemp(Location::RegisterLocation(ECX));
4950 }
Calin Juravle52c48962014-12-16 17:02:57 +00004951 }
4952}
4953
4954void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004955 const FieldInfo& field_info,
4956 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004957 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4958
4959 LocationSummary* locations = instruction->GetLocations();
4960 Register base = locations->InAt(0).AsRegister<Register>();
4961 Location value = locations->InAt(1);
4962 bool is_volatile = field_info.IsVolatile();
4963 Primitive::Type field_type = field_info.GetFieldType();
4964 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004965 bool needs_write_barrier =
4966 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004967
4968 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004969 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004970 }
4971
Mark Mendell81489372015-11-04 11:30:41 -05004972 bool maybe_record_implicit_null_check_done = false;
4973
Calin Juravle52c48962014-12-16 17:02:57 +00004974 switch (field_type) {
4975 case Primitive::kPrimBoolean:
4976 case Primitive::kPrimByte: {
4977 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
4978 break;
4979 }
4980
4981 case Primitive::kPrimShort:
4982 case Primitive::kPrimChar: {
Mark Mendell81489372015-11-04 11:30:41 -05004983 if (value.IsConstant()) {
4984 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4985 __ movw(Address(base, offset), Immediate(v));
4986 } else {
4987 __ movw(Address(base, offset), value.AsRegister<Register>());
4988 }
Calin Juravle52c48962014-12-16 17:02:57 +00004989 break;
4990 }
4991
4992 case Primitive::kPrimInt:
4993 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01004994 if (kPoisonHeapReferences && needs_write_barrier) {
4995 // Note that in the case where `value` is a null reference,
4996 // we do not enter this block, as the reference does not
4997 // need poisoning.
4998 DCHECK_EQ(field_type, Primitive::kPrimNot);
4999 Register temp = locations->GetTemp(0).AsRegister<Register>();
5000 __ movl(temp, value.AsRegister<Register>());
5001 __ PoisonHeapReference(temp);
5002 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05005003 } else if (value.IsConstant()) {
5004 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5005 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01005006 } else {
Nicolas Geoffray03971632016-03-17 10:44:24 +00005007 DCHECK(value.IsRegister()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01005008 __ movl(Address(base, offset), value.AsRegister<Register>());
5009 }
Calin Juravle52c48962014-12-16 17:02:57 +00005010 break;
5011 }
5012
5013 case Primitive::kPrimLong: {
5014 if (is_volatile) {
5015 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
5016 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
5017 __ movd(temp1, value.AsRegisterPairLow<Register>());
5018 __ movd(temp2, value.AsRegisterPairHigh<Register>());
5019 __ punpckldq(temp1, temp2);
5020 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00005021 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05005022 } else if (value.IsConstant()) {
5023 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
5024 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
5025 codegen_->MaybeRecordImplicitNullCheck(instruction);
5026 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00005027 } else {
5028 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005029 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005030 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
5031 }
Mark Mendell81489372015-11-04 11:30:41 -05005032 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00005033 break;
5034 }
5035
5036 case Primitive::kPrimFloat: {
Mark Mendell81489372015-11-04 11:30:41 -05005037 if (value.IsConstant()) {
5038 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5039 __ movl(Address(base, offset), Immediate(v));
5040 } else {
5041 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
5042 }
Calin Juravle52c48962014-12-16 17:02:57 +00005043 break;
5044 }
5045
5046 case Primitive::kPrimDouble: {
Mark Mendell81489372015-11-04 11:30:41 -05005047 if (value.IsConstant()) {
5048 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
5049 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
5050 codegen_->MaybeRecordImplicitNullCheck(instruction);
5051 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
5052 maybe_record_implicit_null_check_done = true;
5053 } else {
5054 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
5055 }
Calin Juravle52c48962014-12-16 17:02:57 +00005056 break;
5057 }
5058
5059 case Primitive::kPrimVoid:
5060 LOG(FATAL) << "Unreachable type " << field_type;
5061 UNREACHABLE();
5062 }
5063
Mark Mendell81489372015-11-04 11:30:41 -05005064 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005065 codegen_->MaybeRecordImplicitNullCheck(instruction);
5066 }
5067
Roland Levillain4d027112015-07-01 15:41:14 +01005068 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005069 Register temp = locations->GetTemp(0).AsRegister<Register>();
5070 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005071 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00005072 }
5073
Calin Juravle52c48962014-12-16 17:02:57 +00005074 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005075 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00005076 }
5077}
5078
5079void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5080 HandleFieldGet(instruction, instruction->GetFieldInfo());
5081}
5082
5083void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5084 HandleFieldGet(instruction, instruction->GetFieldInfo());
5085}
5086
5087void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5088 HandleFieldSet(instruction, instruction->GetFieldInfo());
5089}
5090
5091void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005092 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005093}
5094
5095void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5096 HandleFieldSet(instruction, instruction->GetFieldInfo());
5097}
5098
5099void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005100 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005101}
5102
5103void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5104 HandleFieldGet(instruction, instruction->GetFieldInfo());
5105}
5106
5107void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5108 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005109}
5110
Calin Juravlee460d1d2015-09-29 04:52:17 +01005111void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
5112 HUnresolvedInstanceFieldGet* instruction) {
5113 FieldAccessCallingConventionX86 calling_convention;
5114 codegen_->CreateUnresolvedFieldLocationSummary(
5115 instruction, instruction->GetFieldType(), calling_convention);
5116}
5117
5118void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
5119 HUnresolvedInstanceFieldGet* instruction) {
5120 FieldAccessCallingConventionX86 calling_convention;
5121 codegen_->GenerateUnresolvedFieldAccess(instruction,
5122 instruction->GetFieldType(),
5123 instruction->GetFieldIndex(),
5124 instruction->GetDexPc(),
5125 calling_convention);
5126}
5127
5128void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
5129 HUnresolvedInstanceFieldSet* instruction) {
5130 FieldAccessCallingConventionX86 calling_convention;
5131 codegen_->CreateUnresolvedFieldLocationSummary(
5132 instruction, instruction->GetFieldType(), calling_convention);
5133}
5134
5135void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
5136 HUnresolvedInstanceFieldSet* instruction) {
5137 FieldAccessCallingConventionX86 calling_convention;
5138 codegen_->GenerateUnresolvedFieldAccess(instruction,
5139 instruction->GetFieldType(),
5140 instruction->GetFieldIndex(),
5141 instruction->GetDexPc(),
5142 calling_convention);
5143}
5144
5145void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
5146 HUnresolvedStaticFieldGet* instruction) {
5147 FieldAccessCallingConventionX86 calling_convention;
5148 codegen_->CreateUnresolvedFieldLocationSummary(
5149 instruction, instruction->GetFieldType(), calling_convention);
5150}
5151
5152void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
5153 HUnresolvedStaticFieldGet* instruction) {
5154 FieldAccessCallingConventionX86 calling_convention;
5155 codegen_->GenerateUnresolvedFieldAccess(instruction,
5156 instruction->GetFieldType(),
5157 instruction->GetFieldIndex(),
5158 instruction->GetDexPc(),
5159 calling_convention);
5160}
5161
5162void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
5163 HUnresolvedStaticFieldSet* instruction) {
5164 FieldAccessCallingConventionX86 calling_convention;
5165 codegen_->CreateUnresolvedFieldLocationSummary(
5166 instruction, instruction->GetFieldType(), calling_convention);
5167}
5168
5169void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
5170 HUnresolvedStaticFieldSet* instruction) {
5171 FieldAccessCallingConventionX86 calling_convention;
5172 codegen_->GenerateUnresolvedFieldAccess(instruction,
5173 instruction->GetFieldType(),
5174 instruction->GetFieldIndex(),
5175 instruction->GetDexPc(),
5176 calling_convention);
5177}
5178
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005179void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005180 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5181 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
5182 ? Location::RequiresRegister()
5183 : Location::Any();
5184 locations->SetInAt(0, loc);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005185}
5186
Calin Juravle2ae48182016-03-16 14:05:09 +00005187void CodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
5188 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005189 return;
5190 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005191 LocationSummary* locations = instruction->GetLocations();
5192 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005193
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005194 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00005195 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005196}
5197
Calin Juravle2ae48182016-03-16 14:05:09 +00005198void CodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07005199 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005200 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005201
5202 LocationSummary* locations = instruction->GetLocations();
5203 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005204
5205 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04005206 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005207 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005208 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005209 } else {
5210 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00005211 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005212 __ jmp(slow_path->GetEntryLabel());
5213 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005214 }
5215 __ j(kEqual, slow_path->GetEntryLabel());
5216}
5217
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005218void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005219 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005220}
5221
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005222void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005223 bool object_array_get_with_read_barrier =
5224 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005225 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00005226 new (GetGraph()->GetArena()) LocationSummary(instruction,
5227 object_array_get_with_read_barrier ?
5228 LocationSummary::kCallOnSlowPath :
5229 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005230 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005231 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005232 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005233 locations->SetInAt(0, Location::RequiresRegister());
5234 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005235 if (Primitive::IsFloatingPointType(instruction->GetType())) {
5236 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5237 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005238 // The output overlaps in case of long: we don't want the low move
5239 // to overwrite the array's location. Likewise, in the case of an
5240 // object array get with read barriers enabled, we do not want the
5241 // move to overwrite the array's location, as we need it to emit
5242 // the read barrier.
5243 locations->SetOut(
5244 Location::RequiresRegister(),
5245 (instruction->GetType() == Primitive::kPrimLong || object_array_get_with_read_barrier) ?
5246 Location::kOutputOverlap :
5247 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005248 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005249}
5250
5251void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
5252 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005253 Location obj_loc = locations->InAt(0);
5254 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005255 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005256 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005257 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005258
Calin Juravle77520bc2015-01-12 18:45:46 +00005259 Primitive::Type type = instruction->GetType();
5260 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005261 case Primitive::kPrimBoolean: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005262 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005263 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005264 break;
5265 }
5266
5267 case Primitive::kPrimByte: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005268 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005269 __ movsxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005270 break;
5271 }
5272
5273 case Primitive::kPrimShort: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005274 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005275 __ movsxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005276 break;
5277 }
5278
5279 case Primitive::kPrimChar: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005280 Register out = out_loc.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07005281 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5282 // Branch cases into compressed and uncompressed for each index's type.
5283 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
5284 NearLabel done, not_compressed;
Vladimir Marko3c89d422017-02-17 11:30:23 +00005285 __ testb(Address(obj, count_offset), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005286 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005287 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
5288 "Expecting 0=compressed, 1=uncompressed");
5289 __ j(kNotZero, &not_compressed);
jessicahandojo4877b792016-09-08 19:49:13 -07005290 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
5291 __ jmp(&done);
5292 __ Bind(&not_compressed);
5293 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5294 __ Bind(&done);
5295 } else {
5296 // Common case for charAt of array of char or when string compression's
5297 // feature is turned off.
5298 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5299 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005300 break;
5301 }
5302
Roland Levillain7c1559a2015-12-15 10:55:36 +00005303 case Primitive::kPrimInt: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005304 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005305 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005306 break;
5307 }
5308
Roland Levillain7c1559a2015-12-15 10:55:36 +00005309 case Primitive::kPrimNot: {
5310 static_assert(
5311 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5312 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00005313 // /* HeapReference<Object> */ out =
5314 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5315 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005316 // Note that a potential implicit null check is handled in this
5317 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
5318 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00005319 instruction, out_loc, obj, data_offset, index, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005320 } else {
5321 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005322 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
5323 codegen_->MaybeRecordImplicitNullCheck(instruction);
5324 // If read barriers are enabled, emit read barriers other than
5325 // Baker's using a slow path (and also unpoison the loaded
5326 // reference, if heap poisoning is enabled).
Roland Levillain7c1559a2015-12-15 10:55:36 +00005327 if (index.IsConstant()) {
5328 uint32_t offset =
5329 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005330 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5331 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005332 codegen_->MaybeGenerateReadBarrierSlow(
5333 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5334 }
5335 }
5336 break;
5337 }
5338
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005339 case Primitive::kPrimLong: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005340 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005341 __ movl(out_loc.AsRegisterPairLow<Register>(),
5342 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
5343 codegen_->MaybeRecordImplicitNullCheck(instruction);
5344 __ movl(out_loc.AsRegisterPairHigh<Register>(),
5345 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005346 break;
5347 }
5348
Mark Mendell7c8d0092015-01-26 11:21:33 -05005349 case Primitive::kPrimFloat: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005350 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005351 __ movss(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005352 break;
5353 }
5354
5355 case Primitive::kPrimDouble: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005356 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005357 __ movsd(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005358 break;
5359 }
5360
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005361 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00005362 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005363 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005364 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005365
Roland Levillain7c1559a2015-12-15 10:55:36 +00005366 if (type == Primitive::kPrimNot || type == Primitive::kPrimLong) {
5367 // Potential implicit null checks, in the case of reference or
5368 // long arrays, are handled in the previous switch statement.
5369 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005370 codegen_->MaybeRecordImplicitNullCheck(instruction);
5371 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005372}
5373
5374void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005375 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005376
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005377 bool needs_write_barrier =
5378 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005379 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005380
Nicolas Geoffray39468442014-09-02 15:17:15 +01005381 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
5382 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01005383 may_need_runtime_call_for_type_check ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00005384 LocationSummary::kCallOnSlowPath :
5385 LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005386
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005387 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
5388 || (value_type == Primitive::kPrimByte);
5389 // We need the inputs to be different than the output in case of long operation.
5390 // In case of a byte operation, the register allocator does not support multiple
5391 // inputs that die at entry with one in a specific register.
5392 locations->SetInAt(0, Location::RequiresRegister());
5393 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5394 if (is_byte_type) {
5395 // Ensure the value is in a byte register.
5396 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
5397 } else if (Primitive::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05005398 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005399 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005400 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
5401 }
5402 if (needs_write_barrier) {
5403 // Temporary registers for the write barrier.
5404 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5405 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005406 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005407 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005408}
5409
5410void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
5411 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005412 Location array_loc = locations->InAt(0);
5413 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005414 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005415 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005416 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005417 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5418 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5419 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005420 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005421 bool needs_write_barrier =
5422 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005423
5424 switch (value_type) {
5425 case Primitive::kPrimBoolean:
5426 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005427 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005428 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_1, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005429 if (value.IsRegister()) {
5430 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005431 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005432 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005433 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005434 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005435 break;
5436 }
5437
5438 case Primitive::kPrimShort:
5439 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005440 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005441 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_2, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005442 if (value.IsRegister()) {
5443 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005444 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005445 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005446 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005447 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005448 break;
5449 }
5450
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005451 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005452 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005453 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005454
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005455 if (!value.IsRegister()) {
5456 // Just setting null.
5457 DCHECK(instruction->InputAt(2)->IsNullConstant());
5458 DCHECK(value.IsConstant()) << value;
5459 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00005460 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005461 DCHECK(!needs_write_barrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005462 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005463 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005464 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005465
5466 DCHECK(needs_write_barrier);
5467 Register register_value = value.AsRegister<Register>();
Roland Levillain16d9f942016-08-25 17:27:56 +01005468 // We cannot use a NearLabel for `done`, as its range may be too
5469 // short when Baker read barriers are enabled.
5470 Label done;
5471 NearLabel not_null, do_put;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005472 SlowPathCode* slow_path = nullptr;
Roland Levillain16d9f942016-08-25 17:27:56 +01005473 Location temp_loc = locations->GetTemp(0);
5474 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005475 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005476 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86(instruction);
5477 codegen_->AddSlowPath(slow_path);
5478 if (instruction->GetValueCanBeNull()) {
5479 __ testl(register_value, register_value);
5480 __ j(kNotEqual, &not_null);
5481 __ movl(address, Immediate(0));
5482 codegen_->MaybeRecordImplicitNullCheck(instruction);
5483 __ jmp(&done);
5484 __ Bind(&not_null);
5485 }
5486
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005487 // Note that when Baker read barriers are enabled, the type
5488 // checks are performed without read barriers. This is fine,
5489 // even in the case where a class object is in the from-space
5490 // after the flip, as a comparison involving such a type would
5491 // not produce a false positive; it may of course produce a
5492 // false negative, in which case we would take the ArraySet
5493 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01005494
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005495 // /* HeapReference<Class> */ temp = array->klass_
5496 __ movl(temp, Address(array, class_offset));
5497 codegen_->MaybeRecordImplicitNullCheck(instruction);
5498 __ MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01005499
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005500 // /* HeapReference<Class> */ temp = temp->component_type_
5501 __ movl(temp, Address(temp, component_offset));
5502 // If heap poisoning is enabled, no need to unpoison `temp`
5503 // nor the object reference in `register_value->klass`, as
5504 // we are comparing two poisoned references.
5505 __ cmpl(temp, Address(register_value, class_offset));
Roland Levillain16d9f942016-08-25 17:27:56 +01005506
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005507 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5508 __ j(kEqual, &do_put);
5509 // If heap poisoning is enabled, the `temp` reference has
5510 // not been unpoisoned yet; unpoison it now.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005511 __ MaybeUnpoisonHeapReference(temp);
5512
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005513 // If heap poisoning is enabled, no need to unpoison the
5514 // heap reference loaded below, as it is only used for a
5515 // comparison with null.
5516 __ cmpl(Address(temp, super_offset), Immediate(0));
5517 __ j(kNotEqual, slow_path->GetEntryLabel());
5518 __ Bind(&do_put);
5519 } else {
5520 __ j(kNotEqual, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005521 }
5522 }
5523
5524 if (kPoisonHeapReferences) {
5525 __ movl(temp, register_value);
5526 __ PoisonHeapReference(temp);
5527 __ movl(address, temp);
5528 } else {
5529 __ movl(address, register_value);
5530 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005531 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005532 codegen_->MaybeRecordImplicitNullCheck(instruction);
5533 }
5534
5535 Register card = locations->GetTemp(1).AsRegister<Register>();
5536 codegen_->MarkGCCard(
5537 temp, card, array, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
5538 __ Bind(&done);
5539
5540 if (slow_path != nullptr) {
5541 __ Bind(slow_path->GetExitLabel());
5542 }
5543
5544 break;
5545 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005546
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005547 case Primitive::kPrimInt: {
5548 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005549 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005550 if (value.IsRegister()) {
5551 __ movl(address, value.AsRegister<Register>());
5552 } else {
5553 DCHECK(value.IsConstant()) << value;
5554 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5555 __ movl(address, Immediate(v));
5556 }
5557 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005558 break;
5559 }
5560
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005561 case Primitive::kPrimLong: {
5562 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005563 if (value.IsRegisterPair()) {
5564 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
5565 value.AsRegisterPairLow<Register>());
5566 codegen_->MaybeRecordImplicitNullCheck(instruction);
5567 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
5568 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005569 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005570 DCHECK(value.IsConstant());
5571 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
5572 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
5573 Immediate(Low32Bits(val)));
5574 codegen_->MaybeRecordImplicitNullCheck(instruction);
5575 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
5576 Immediate(High32Bits(val)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005577 }
5578 break;
5579 }
5580
Mark Mendell7c8d0092015-01-26 11:21:33 -05005581 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005582 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005583 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005584 if (value.IsFpuRegister()) {
5585 __ movss(address, value.AsFpuRegister<XmmRegister>());
5586 } else {
5587 DCHECK(value.IsConstant());
5588 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
5589 __ movl(address, Immediate(v));
5590 }
5591 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005592 break;
5593 }
5594
5595 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005596 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005597 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005598 if (value.IsFpuRegister()) {
5599 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5600 } else {
5601 DCHECK(value.IsConstant());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005602 Address address_hi =
5603 CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset + kX86WordSize);
Mark Mendell81489372015-11-04 11:30:41 -05005604 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5605 __ movl(address, Immediate(Low32Bits(v)));
5606 codegen_->MaybeRecordImplicitNullCheck(instruction);
5607 __ movl(address_hi, Immediate(High32Bits(v)));
5608 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005609 break;
5610 }
5611
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005612 case Primitive::kPrimVoid:
5613 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005614 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005615 }
5616}
5617
5618void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
5619 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005620 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04005621 if (!instruction->IsEmittedAtUseSite()) {
5622 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5623 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005624}
5625
5626void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04005627 if (instruction->IsEmittedAtUseSite()) {
5628 return;
5629 }
5630
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005631 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005632 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005633 Register obj = locations->InAt(0).AsRegister<Register>();
5634 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005635 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005636 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo4877b792016-09-08 19:49:13 -07005637 // Mask out most significant bit in case the array is String's array of char.
5638 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005639 __ shrl(out, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005640 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005641}
5642
5643void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005644 RegisterSet caller_saves = RegisterSet::Empty();
5645 InvokeRuntimeCallingConvention calling_convention;
5646 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5647 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5648 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005649 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04005650 HInstruction* length = instruction->InputAt(1);
5651 if (!length->IsEmittedAtUseSite()) {
5652 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5653 }
jessicahandojo4877b792016-09-08 19:49:13 -07005654 // Need register to see array's length.
5655 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5656 locations->AddTemp(Location::RequiresRegister());
5657 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005658}
5659
5660void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
jessicahandojo4877b792016-09-08 19:49:13 -07005661 const bool is_string_compressed_char_at =
5662 mirror::kUseStringCompression && instruction->IsStringCharAt();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005663 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005664 Location index_loc = locations->InAt(0);
5665 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07005666 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005667 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005668
Mark Mendell99dbd682015-04-22 16:18:52 -04005669 if (length_loc.IsConstant()) {
5670 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5671 if (index_loc.IsConstant()) {
5672 // BCE will remove the bounds check if we are guarenteed to pass.
5673 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5674 if (index < 0 || index >= length) {
5675 codegen_->AddSlowPath(slow_path);
5676 __ jmp(slow_path->GetEntryLabel());
5677 } else {
5678 // Some optimization after BCE may have generated this, and we should not
5679 // generate a bounds check if it is a valid range.
5680 }
5681 return;
5682 }
5683
5684 // We have to reverse the jump condition because the length is the constant.
5685 Register index_reg = index_loc.AsRegister<Register>();
5686 __ cmpl(index_reg, Immediate(length));
5687 codegen_->AddSlowPath(slow_path);
5688 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005689 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04005690 HInstruction* array_length = instruction->InputAt(1);
5691 if (array_length->IsEmittedAtUseSite()) {
5692 // Address the length field in the array.
5693 DCHECK(array_length->IsArrayLength());
5694 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
5695 Location array_loc = array_length->GetLocations()->InAt(0);
5696 Address array_len(array_loc.AsRegister<Register>(), len_offset);
jessicahandojo4877b792016-09-08 19:49:13 -07005697 if (is_string_compressed_char_at) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005698 // TODO: if index_loc.IsConstant(), compare twice the index (to compensate for
5699 // the string compression flag) with the in-memory length and avoid the temporary.
jessicahandojo4877b792016-09-08 19:49:13 -07005700 Register length_reg = locations->GetTemp(0).AsRegister<Register>();
5701 __ movl(length_reg, array_len);
5702 codegen_->MaybeRecordImplicitNullCheck(array_length);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005703 __ shrl(length_reg, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005704 codegen_->GenerateIntCompare(length_reg, index_loc);
Mark Mendellee8d9712016-07-12 11:13:15 -04005705 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07005706 // Checking bounds for general case:
5707 // Array of char or string's array with feature compression off.
5708 if (index_loc.IsConstant()) {
5709 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5710 __ cmpl(array_len, Immediate(value));
5711 } else {
5712 __ cmpl(array_len, index_loc.AsRegister<Register>());
5713 }
5714 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendellee8d9712016-07-12 11:13:15 -04005715 }
Mark Mendell99dbd682015-04-22 16:18:52 -04005716 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005717 codegen_->GenerateIntCompare(length_loc, index_loc);
Mark Mendell99dbd682015-04-22 16:18:52 -04005718 }
5719 codegen_->AddSlowPath(slow_path);
5720 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005721 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005722}
5723
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005724void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005725 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005726}
5727
5728void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005729 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5730}
5731
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005732void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005733 LocationSummary* locations =
5734 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Aart Bikb13c65b2017-03-21 20:14:07 -07005735 // In suspend check slow path, usually there are no caller-save registers at all.
5736 // If SIMD instructions are present, however, we force spilling all live SIMD
5737 // registers in full width (since the runtime only saves/restores lower part).
Aart Bik5576f372017-03-23 16:17:37 -07005738 locations->SetCustomSlowPathCallerSaves(
5739 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005740}
5741
5742void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005743 HBasicBlock* block = instruction->GetBlock();
5744 if (block->GetLoopInformation() != nullptr) {
5745 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5746 // The back edge will generate the suspend check.
5747 return;
5748 }
5749 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5750 // The goto will generate the suspend check.
5751 return;
5752 }
5753 GenerateSuspendCheck(instruction, nullptr);
5754}
5755
5756void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
5757 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005758 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005759 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
5760 if (slow_path == nullptr) {
5761 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
5762 instruction->SetSlowPath(slow_path);
5763 codegen_->AddSlowPath(slow_path);
5764 if (successor != nullptr) {
5765 DCHECK(successor->IsLoopHeader());
5766 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5767 }
5768 } else {
5769 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5770 }
5771
Andreas Gampe542451c2016-07-26 09:02:02 -07005772 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86PointerSize>().Int32Value()),
Roland Levillain7c1559a2015-12-15 10:55:36 +00005773 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005774 if (successor == nullptr) {
5775 __ j(kNotEqual, slow_path->GetEntryLabel());
5776 __ Bind(slow_path->GetReturnLabel());
5777 } else {
5778 __ j(kEqual, codegen_->GetLabelOf(successor));
5779 __ jmp(slow_path->GetEntryLabel());
5780 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005781}
5782
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005783X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
5784 return codegen_->GetAssembler();
5785}
5786
Mark Mendell7c8d0092015-01-26 11:21:33 -05005787void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005788 ScratchRegisterScope ensure_scratch(
5789 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5790 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5791 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5792 __ movl(temp_reg, Address(ESP, src + stack_offset));
5793 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005794}
5795
5796void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005797 ScratchRegisterScope ensure_scratch(
5798 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5799 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5800 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5801 __ movl(temp_reg, Address(ESP, src + stack_offset));
5802 __ movl(Address(ESP, dst + stack_offset), temp_reg);
5803 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
5804 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005805}
5806
5807void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005808 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005809 Location source = move->GetSource();
5810 Location destination = move->GetDestination();
5811
5812 if (source.IsRegister()) {
5813 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005814 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005815 } else if (destination.IsFpuRegister()) {
5816 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005817 } else {
5818 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005819 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005820 }
David Brazdil74eb1b22015-12-14 11:44:01 +00005821 } else if (source.IsRegisterPair()) {
5822 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
5823 // Create stack space for 2 elements.
5824 __ subl(ESP, Immediate(2 * elem_size));
5825 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
5826 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
5827 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
5828 // And remove the temporary stack space we allocated.
5829 __ addl(ESP, Immediate(2 * elem_size));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005830 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005831 if (destination.IsRegister()) {
5832 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
5833 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005834 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005835 } else if (destination.IsRegisterPair()) {
5836 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
5837 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
5838 __ psrlq(src_reg, Immediate(32));
5839 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005840 } else if (destination.IsStackSlot()) {
5841 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07005842 } else if (destination.IsDoubleStackSlot()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005843 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07005844 } else {
5845 DCHECK(destination.IsSIMDStackSlot());
5846 __ movups(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005847 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005848 } else if (source.IsStackSlot()) {
5849 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005850 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005851 } else if (destination.IsFpuRegister()) {
5852 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005853 } else {
5854 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005855 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
5856 }
5857 } else if (source.IsDoubleStackSlot()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005858 if (destination.IsRegisterPair()) {
5859 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
5860 __ movl(destination.AsRegisterPairHigh<Register>(),
5861 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
5862 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005863 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
5864 } else {
5865 DCHECK(destination.IsDoubleStackSlot()) << destination;
5866 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005867 }
Aart Bik5576f372017-03-23 16:17:37 -07005868 } else if (source.IsSIMDStackSlot()) {
5869 DCHECK(destination.IsFpuRegister());
5870 __ movups(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005871 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005872 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005873 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005874 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005875 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005876 if (value == 0) {
5877 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
5878 } else {
5879 __ movl(destination.AsRegister<Register>(), Immediate(value));
5880 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005881 } else {
5882 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05005883 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005884 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005885 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005886 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005887 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005888 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005889 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005890 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5891 if (value == 0) {
5892 // Easy handling of 0.0.
5893 __ xorps(dest, dest);
5894 } else {
5895 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005896 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5897 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
5898 __ movl(temp, Immediate(value));
5899 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005900 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005901 } else {
5902 DCHECK(destination.IsStackSlot()) << destination;
5903 __ movl(Address(ESP, destination.GetStackIndex()), imm);
5904 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005905 } else if (constant->IsLongConstant()) {
5906 int64_t value = constant->AsLongConstant()->GetValue();
5907 int32_t low_value = Low32Bits(value);
5908 int32_t high_value = High32Bits(value);
5909 Immediate low(low_value);
5910 Immediate high(high_value);
5911 if (destination.IsDoubleStackSlot()) {
5912 __ movl(Address(ESP, destination.GetStackIndex()), low);
5913 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5914 } else {
5915 __ movl(destination.AsRegisterPairLow<Register>(), low);
5916 __ movl(destination.AsRegisterPairHigh<Register>(), high);
5917 }
5918 } else {
5919 DCHECK(constant->IsDoubleConstant());
5920 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005921 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005922 int32_t low_value = Low32Bits(value);
5923 int32_t high_value = High32Bits(value);
5924 Immediate low(low_value);
5925 Immediate high(high_value);
5926 if (destination.IsFpuRegister()) {
5927 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5928 if (value == 0) {
5929 // Easy handling of 0.0.
5930 __ xorpd(dest, dest);
5931 } else {
5932 __ pushl(high);
5933 __ pushl(low);
5934 __ movsd(dest, Address(ESP, 0));
5935 __ addl(ESP, Immediate(8));
5936 }
5937 } else {
5938 DCHECK(destination.IsDoubleStackSlot()) << destination;
5939 __ movl(Address(ESP, destination.GetStackIndex()), low);
5940 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5941 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005942 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005943 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005944 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005945 }
5946}
5947
Mark Mendella5c19ce2015-04-01 12:51:05 -04005948void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005949 Register suggested_scratch = reg == EAX ? EBX : EAX;
5950 ScratchRegisterScope ensure_scratch(
5951 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
5952
5953 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5954 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
5955 __ movl(Address(ESP, mem + stack_offset), reg);
5956 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005957}
5958
Mark Mendell7c8d0092015-01-26 11:21:33 -05005959void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005960 ScratchRegisterScope ensure_scratch(
5961 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5962
5963 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5964 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5965 __ movl(temp_reg, Address(ESP, mem + stack_offset));
5966 __ movss(Address(ESP, mem + stack_offset), reg);
5967 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005968}
5969
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005970void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005971 ScratchRegisterScope ensure_scratch1(
5972 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005973
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005974 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
5975 ScratchRegisterScope ensure_scratch2(
5976 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005977
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005978 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
5979 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
5980 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
5981 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
5982 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
5983 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005984}
5985
5986void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005987 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005988 Location source = move->GetSource();
5989 Location destination = move->GetDestination();
5990
5991 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04005992 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
5993 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
5994 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
5995 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
5996 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005997 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005998 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005999 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006000 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006001 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
6002 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05006003 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
6004 // Use XOR Swap algorithm to avoid a temporary.
6005 DCHECK_NE(source.reg(), destination.reg());
6006 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
6007 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
6008 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
6009 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
6010 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
6011 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
6012 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006013 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
6014 // Take advantage of the 16 bytes in the XMM register.
6015 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
6016 Address stack(ESP, destination.GetStackIndex());
6017 // Load the double into the high doubleword.
6018 __ movhpd(reg, stack);
6019
6020 // Store the low double into the destination.
6021 __ movsd(stack, reg);
6022
6023 // Move the high double to the low double.
6024 __ psrldq(reg, Immediate(8));
6025 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
6026 // Take advantage of the 16 bytes in the XMM register.
6027 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
6028 Address stack(ESP, source.GetStackIndex());
6029 // Load the double into the high doubleword.
6030 __ movhpd(reg, stack);
6031
6032 // Store the low double into the destination.
6033 __ movsd(stack, reg);
6034
6035 // Move the high double to the low double.
6036 __ psrldq(reg, Immediate(8));
6037 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
6038 Exchange(destination.GetStackIndex(), source.GetStackIndex());
6039 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006040 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006041 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006042 }
6043}
6044
6045void ParallelMoveResolverX86::SpillScratch(int reg) {
6046 __ pushl(static_cast<Register>(reg));
6047}
6048
6049void ParallelMoveResolverX86::RestoreScratch(int reg) {
6050 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006051}
6052
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006053HLoadClass::LoadKind CodeGeneratorX86::GetSupportedLoadClassKind(
6054 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006055 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006056 case HLoadClass::LoadKind::kInvalid:
6057 LOG(FATAL) << "UNREACHABLE";
6058 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006059 case HLoadClass::LoadKind::kReferrersClass:
6060 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006061 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006062 case HLoadClass::LoadKind::kBssEntry:
Vladimir Marko764d4542017-05-16 10:31:41 +01006063 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006064 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006065 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006066 DCHECK(Runtime::Current()->UseJitCompilation());
6067 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01006068 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006069 case HLoadClass::LoadKind::kRuntimeCall:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006070 break;
6071 }
6072 return desired_class_load_kind;
6073}
6074
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006075void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00006076 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006077 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006078 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00006079 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006080 cls,
6081 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00006082 Location::RegisterLocation(EAX));
Vladimir Markoea4c1262017-02-06 19:59:33 +00006083 DCHECK_EQ(calling_convention.GetRegisterAt(0), EAX);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006084 return;
6085 }
Vladimir Marko41559982017-01-06 14:04:23 +00006086 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006087
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006088 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
6089 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006090 ? LocationSummary::kCallOnSlowPath
6091 : LocationSummary::kNoCall;
6092 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006093 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006094 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006095 }
6096
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006097 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006098 load_kind == HLoadClass::LoadKind::kBootImageLinkTimePcRelative ||
6099 load_kind == HLoadClass::LoadKind::kBssEntry) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006100 locations->SetInAt(0, Location::RequiresRegister());
6101 }
6102 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006103 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
6104 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6105 // Rely on the type resolution and/or initialization to save everything.
6106 RegisterSet caller_saves = RegisterSet::Empty();
6107 InvokeRuntimeCallingConvention calling_convention;
6108 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6109 locations->SetCustomSlowPathCallerSaves(caller_saves);
6110 } else {
6111 // For non-Baker read barrier we have a temp-clobbering call.
6112 }
6113 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006114}
6115
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006116Label* CodeGeneratorX86::NewJitRootClassPatch(const DexFile& dex_file,
6117 dex::TypeIndex dex_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006118 Handle<mirror::Class> handle) {
6119 jit_class_roots_.Overwrite(TypeReference(&dex_file, dex_index),
6120 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006121 // Add a patch entry and return the label.
6122 jit_class_patches_.emplace_back(dex_file, dex_index.index_);
6123 PatchInfo<Label>* info = &jit_class_patches_.back();
6124 return &info->label;
6125}
6126
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006127// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6128// move.
6129void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00006130 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006131 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00006132 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01006133 return;
6134 }
Vladimir Marko41559982017-01-06 14:04:23 +00006135 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01006136
Vladimir Marko41559982017-01-06 14:04:23 +00006137 LocationSummary* locations = cls->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006138 Location out_loc = locations->Out();
6139 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006140
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006141 bool generate_null_check = false;
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006142 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
6143 ? kWithoutReadBarrier
6144 : kCompilerReadBarrierOption;
Vladimir Marko41559982017-01-06 14:04:23 +00006145 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006146 case HLoadClass::LoadKind::kReferrersClass: {
6147 DCHECK(!cls->CanCallRuntime());
6148 DCHECK(!cls->MustGenerateClinitCheck());
6149 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6150 Register current_method = locations->InAt(0).AsRegister<Register>();
6151 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006152 cls,
6153 out_loc,
6154 Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()),
Roland Levillain00468f32016-10-27 18:02:48 +01006155 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006156 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006157 break;
6158 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006159 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006160 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006161 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006162 Register method_address = locations->InAt(0).AsRegister<Register>();
6163 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Marko1998cd02017-01-13 13:02:58 +00006164 codegen_->RecordBootTypePatch(cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006165 break;
6166 }
6167 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006168 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006169 uint32_t address = dchecked_integral_cast<uint32_t>(
6170 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
6171 DCHECK_NE(address, 0u);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006172 __ movl(out, Immediate(address));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006173 break;
6174 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006175 case HLoadClass::LoadKind::kBssEntry: {
6176 Register method_address = locations->InAt(0).AsRegister<Register>();
6177 Address address(method_address, CodeGeneratorX86::kDummy32BitOffset);
6178 Label* fixup_label = codegen_->NewTypeBssEntryPatch(cls);
6179 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
6180 generate_null_check = true;
6181 break;
6182 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006183 case HLoadClass::LoadKind::kJitTableAddress: {
6184 Address address = Address::Absolute(CodeGeneratorX86::kDummy32BitOffset);
6185 Label* fixup_label = codegen_->NewJitRootClassPatch(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006186 cls->GetDexFile(), cls->GetTypeIndex(), cls->GetClass());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006187 // /* GcRoot<mirror::Class> */ out = *address
Vladimir Markoea4c1262017-02-06 19:59:33 +00006188 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006189 break;
6190 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006191 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006192 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00006193 LOG(FATAL) << "UNREACHABLE";
6194 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006195 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006196
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006197 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6198 DCHECK(cls->CanCallRuntime());
6199 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
6200 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
6201 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006202
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006203 if (generate_null_check) {
6204 __ testl(out, out);
6205 __ j(kEqual, slow_path->GetEntryLabel());
6206 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006207
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006208 if (cls->MustGenerateClinitCheck()) {
6209 GenerateClassInitializationCheck(slow_path, out);
6210 } else {
6211 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006212 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006213 }
6214}
6215
6216void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
6217 LocationSummary* locations =
6218 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
6219 locations->SetInAt(0, Location::RequiresRegister());
6220 if (check->HasUses()) {
6221 locations->SetOut(Location::SameAsFirstInput());
6222 }
6223}
6224
6225void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006226 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07006227 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006228 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006229 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00006230 GenerateClassInitializationCheck(slow_path,
6231 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006232}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006233
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006234void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07006235 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006236 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
6237 Immediate(mirror::Class::kStatusInitialized));
6238 __ j(kLess, slow_path->GetEntryLabel());
6239 __ Bind(slow_path->GetExitLabel());
6240 // No need for memory fence, thanks to the X86 memory model.
6241}
6242
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006243HLoadString::LoadKind CodeGeneratorX86::GetSupportedLoadStringKind(
6244 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006245 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006246 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoaad75c62016-10-03 08:46:48 +00006247 case HLoadString::LoadKind::kBssEntry:
Vladimir Marko764d4542017-05-16 10:31:41 +01006248 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006249 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006250 case HLoadString::LoadKind::kJitTableAddress:
6251 DCHECK(Runtime::Current()->UseJitCompilation());
6252 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01006253 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006254 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006255 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006256 }
6257 return desired_string_load_kind;
6258}
6259
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006260void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006261 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006262 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006263 HLoadString::LoadKind load_kind = load->GetLoadKind();
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006264 if (load_kind == HLoadString::LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00006265 load_kind == HLoadString::LoadKind::kBssEntry) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006266 locations->SetInAt(0, Location::RequiresRegister());
6267 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006268 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006269 locations->SetOut(Location::RegisterLocation(EAX));
6270 } else {
6271 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006272 if (load_kind == HLoadString::LoadKind::kBssEntry) {
6273 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00006274 // Rely on the pResolveString to save everything.
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006275 RegisterSet caller_saves = RegisterSet::Empty();
6276 InvokeRuntimeCallingConvention calling_convention;
6277 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6278 locations->SetCustomSlowPathCallerSaves(caller_saves);
6279 } else {
6280 // For non-Baker read barrier we have a temp-clobbering call.
6281 }
6282 }
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006283 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006284}
6285
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006286Label* CodeGeneratorX86::NewJitRootStringPatch(const DexFile& dex_file,
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006287 dex::StringIndex dex_index,
6288 Handle<mirror::String> handle) {
6289 jit_string_roots_.Overwrite(
6290 StringReference(&dex_file, dex_index), reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006291 // Add a patch entry and return the label.
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006292 jit_string_patches_.emplace_back(dex_file, dex_index.index_);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006293 PatchInfo<Label>* info = &jit_string_patches_.back();
6294 return &info->label;
6295}
6296
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006297// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6298// move.
6299void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006300 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006301 Location out_loc = locations->Out();
6302 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006303
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006304 switch (load->GetLoadKind()) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006305 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006306 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006307 Register method_address = locations->InAt(0).AsRegister<Register>();
6308 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Markoaad75c62016-10-03 08:46:48 +00006309 codegen_->RecordBootStringPatch(load);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006310 return; // No dex cache slow path.
6311 }
6312 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006313 uint32_t address = dchecked_integral_cast<uint32_t>(
6314 reinterpret_cast<uintptr_t>(load->GetString().Get()));
6315 DCHECK_NE(address, 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006316 __ movl(out, Immediate(address));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006317 return; // No dex cache slow path.
6318 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00006319 case HLoadString::LoadKind::kBssEntry: {
6320 Register method_address = locations->InAt(0).AsRegister<Register>();
6321 Address address = Address(method_address, CodeGeneratorX86::kDummy32BitOffset);
6322 Label* fixup_label = codegen_->NewStringBssEntryPatch(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006323 // /* GcRoot<mirror::String> */ out = *address /* PC-relative */
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006324 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
Vladimir Markoaad75c62016-10-03 08:46:48 +00006325 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
6326 codegen_->AddSlowPath(slow_path);
6327 __ testl(out, out);
6328 __ j(kEqual, slow_path->GetEntryLabel());
6329 __ Bind(slow_path->GetExitLabel());
6330 return;
6331 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006332 case HLoadString::LoadKind::kJitTableAddress: {
6333 Address address = Address::Absolute(CodeGeneratorX86::kDummy32BitOffset);
6334 Label* fixup_label = codegen_->NewJitRootStringPatch(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006335 load->GetDexFile(), load->GetStringIndex(), load->GetString());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006336 // /* GcRoot<mirror::String> */ out = *address
6337 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
6338 return;
6339 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006340 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006341 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006342 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006343
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006344 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006345 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006346 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006347 __ movl(calling_convention.GetRegisterAt(0), Immediate(load->GetStringIndex().index_));
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006348 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6349 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006350}
6351
David Brazdilcb1c0552015-08-04 16:22:25 +01006352static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006353 return Address::Absolute(Thread::ExceptionOffset<kX86PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01006354}
6355
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006356void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
6357 LocationSummary* locations =
6358 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
6359 locations->SetOut(Location::RequiresRegister());
6360}
6361
6362void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01006363 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
6364}
6365
6366void LocationsBuilderX86::VisitClearException(HClearException* clear) {
6367 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
6368}
6369
6370void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6371 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006372}
6373
6374void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
6375 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006376 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006377 InvokeRuntimeCallingConvention calling_convention;
6378 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6379}
6380
6381void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006382 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006383 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006384}
6385
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006386// Temp is used for read barrier.
6387static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
6388 if (kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00006389 !kUseBakerReadBarrier &&
6390 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain7c1559a2015-12-15 10:55:36 +00006391 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006392 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
6393 return 1;
6394 }
6395 return 0;
6396}
6397
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006398// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006399// interface pointer, one for loading the current interface.
6400// The other checks have one temp for loading the object's class.
6401static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
6402 if (type_check_kind == TypeCheckKind::kInterfaceCheck && !kPoisonHeapReferences) {
6403 return 2;
6404 }
6405 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006406}
6407
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006408void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006409 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006410 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01006411 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006412 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006413 case TypeCheckKind::kExactCheck:
6414 case TypeCheckKind::kAbstractClassCheck:
6415 case TypeCheckKind::kClassHierarchyCheck:
6416 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006417 call_kind =
6418 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01006419 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006420 break;
6421 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006422 case TypeCheckKind::kUnresolvedCheck:
6423 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006424 call_kind = LocationSummary::kCallOnSlowPath;
6425 break;
6426 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006427
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006428 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01006429 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006430 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006431 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006432 locations->SetInAt(0, Location::RequiresRegister());
6433 locations->SetInAt(1, Location::Any());
6434 // Note that TypeCheckSlowPathX86 uses this "out" register too.
6435 locations->SetOut(Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006436 // When read barriers are enabled, we need a temporary register for some cases.
6437 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006438}
6439
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006440void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006441 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006442 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006443 Location obj_loc = locations->InAt(0);
6444 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006445 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006446 Location out_loc = locations->Out();
6447 Register out = out_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006448 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
6449 DCHECK_LE(num_temps, 1u);
6450 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006451 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006452 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6453 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6454 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07006455 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006456 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006457
6458 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006459 // Avoid null check if we know obj is not null.
6460 if (instruction->MustDoNullCheck()) {
6461 __ testl(obj, obj);
6462 __ j(kEqual, &zero);
6463 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006464
Roland Levillain7c1559a2015-12-15 10:55:36 +00006465 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006466 case TypeCheckKind::kExactCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006467 // /* HeapReference<Class> */ out = obj->klass_
6468 GenerateReferenceLoadTwoRegisters(instruction,
6469 out_loc,
6470 obj_loc,
6471 class_offset,
6472 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006473 if (cls.IsRegister()) {
6474 __ cmpl(out, cls.AsRegister<Register>());
6475 } else {
6476 DCHECK(cls.IsStackSlot()) << cls;
6477 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6478 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006479
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006480 // Classes must be equal for the instanceof to succeed.
6481 __ j(kNotEqual, &zero);
6482 __ movl(out, Immediate(1));
6483 __ jmp(&done);
6484 break;
6485 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006486
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006487 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006488 // /* HeapReference<Class> */ out = obj->klass_
6489 GenerateReferenceLoadTwoRegisters(instruction,
6490 out_loc,
6491 obj_loc,
6492 class_offset,
6493 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006494 // If the class is abstract, we eagerly fetch the super class of the
6495 // object to avoid doing a comparison we know will fail.
6496 NearLabel loop;
6497 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006498 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006499 GenerateReferenceLoadOneRegister(instruction,
6500 out_loc,
6501 super_offset,
6502 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006503 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006504 __ testl(out, out);
6505 // If `out` is null, we use it for the result, and jump to `done`.
6506 __ j(kEqual, &done);
6507 if (cls.IsRegister()) {
6508 __ cmpl(out, cls.AsRegister<Register>());
6509 } else {
6510 DCHECK(cls.IsStackSlot()) << cls;
6511 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6512 }
6513 __ j(kNotEqual, &loop);
6514 __ movl(out, Immediate(1));
6515 if (zero.IsLinked()) {
6516 __ jmp(&done);
6517 }
6518 break;
6519 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006520
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006521 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006522 // /* HeapReference<Class> */ out = obj->klass_
6523 GenerateReferenceLoadTwoRegisters(instruction,
6524 out_loc,
6525 obj_loc,
6526 class_offset,
6527 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006528 // Walk over the class hierarchy to find a match.
6529 NearLabel loop, success;
6530 __ Bind(&loop);
6531 if (cls.IsRegister()) {
6532 __ cmpl(out, cls.AsRegister<Register>());
6533 } else {
6534 DCHECK(cls.IsStackSlot()) << cls;
6535 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6536 }
6537 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006538 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006539 GenerateReferenceLoadOneRegister(instruction,
6540 out_loc,
6541 super_offset,
6542 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006543 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006544 __ testl(out, out);
6545 __ j(kNotEqual, &loop);
6546 // If `out` is null, we use it for the result, and jump to `done`.
6547 __ jmp(&done);
6548 __ Bind(&success);
6549 __ movl(out, Immediate(1));
6550 if (zero.IsLinked()) {
6551 __ jmp(&done);
6552 }
6553 break;
6554 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006555
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006556 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006557 // /* HeapReference<Class> */ out = obj->klass_
6558 GenerateReferenceLoadTwoRegisters(instruction,
6559 out_loc,
6560 obj_loc,
6561 class_offset,
6562 kCompilerReadBarrierOption);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006563 // Do an exact check.
6564 NearLabel exact_check;
6565 if (cls.IsRegister()) {
6566 __ cmpl(out, cls.AsRegister<Register>());
6567 } else {
6568 DCHECK(cls.IsStackSlot()) << cls;
6569 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6570 }
6571 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006572 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006573 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006574 GenerateReferenceLoadOneRegister(instruction,
6575 out_loc,
6576 component_offset,
6577 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006578 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006579 __ testl(out, out);
6580 // If `out` is null, we use it for the result, and jump to `done`.
6581 __ j(kEqual, &done);
6582 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
6583 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006584 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006585 __ movl(out, Immediate(1));
6586 __ jmp(&done);
6587 break;
6588 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006589
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006590 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006591 // No read barrier since the slow path will retry upon failure.
6592 // /* HeapReference<Class> */ out = obj->klass_
6593 GenerateReferenceLoadTwoRegisters(instruction,
6594 out_loc,
6595 obj_loc,
6596 class_offset,
6597 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006598 if (cls.IsRegister()) {
6599 __ cmpl(out, cls.AsRegister<Register>());
6600 } else {
6601 DCHECK(cls.IsStackSlot()) << cls;
6602 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6603 }
6604 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006605 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6606 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006607 codegen_->AddSlowPath(slow_path);
6608 __ j(kNotEqual, slow_path->GetEntryLabel());
6609 __ movl(out, Immediate(1));
6610 if (zero.IsLinked()) {
6611 __ jmp(&done);
6612 }
6613 break;
6614 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006615
Calin Juravle98893e12015-10-02 21:05:03 +01006616 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006617 case TypeCheckKind::kInterfaceCheck: {
6618 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006619 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00006620 // cases.
6621 //
6622 // We cannot directly call the InstanceofNonTrivial runtime
6623 // entry point without resorting to a type checking slow path
6624 // here (i.e. by calling InvokeRuntime directly), as it would
6625 // require to assign fixed registers for the inputs of this
6626 // HInstanceOf instruction (following the runtime calling
6627 // convention), which might be cluttered by the potential first
6628 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006629 //
6630 // TODO: Introduce a new runtime entry point taking the object
6631 // to test (instead of its class) as argument, and let it deal
6632 // with the read barrier issues. This will let us refactor this
6633 // case of the `switch` code as it was previously (with a direct
6634 // call to the runtime not using a type checking slow path).
6635 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006636 DCHECK(locations->OnlyCallsOnSlowPath());
6637 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6638 /* is_fatal */ false);
6639 codegen_->AddSlowPath(slow_path);
6640 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006641 if (zero.IsLinked()) {
6642 __ jmp(&done);
6643 }
6644 break;
6645 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006646 }
6647
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006648 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006649 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006650 __ xorl(out, out);
6651 }
6652
6653 if (done.IsLinked()) {
6654 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006655 }
6656
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006657 if (slow_path != nullptr) {
6658 __ Bind(slow_path->GetExitLabel());
6659 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006660}
6661
Mathieu Chartier5ac321b2016-11-09 16:33:54 -08006662static bool IsTypeCheckSlowPathFatal(TypeCheckKind type_check_kind, bool throws_into_catch) {
6663 switch (type_check_kind) {
6664 case TypeCheckKind::kExactCheck:
6665 case TypeCheckKind::kAbstractClassCheck:
6666 case TypeCheckKind::kClassHierarchyCheck:
6667 case TypeCheckKind::kArrayObjectCheck:
6668 return !throws_into_catch && !kEmitCompilerReadBarrier;
6669 case TypeCheckKind::kInterfaceCheck:
6670 return !throws_into_catch && !kEmitCompilerReadBarrier && !kPoisonHeapReferences;
6671 case TypeCheckKind::kArrayCheck:
6672 case TypeCheckKind::kUnresolvedCheck:
6673 return false;
6674 }
6675 LOG(FATAL) << "Unreachable";
6676 UNREACHABLE();
6677}
6678
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006679void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006680 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006681 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Mathieu Chartier5ac321b2016-11-09 16:33:54 -08006682 LocationSummary::CallKind call_kind =
6683 IsTypeCheckSlowPathFatal(type_check_kind, throws_into_catch)
6684 ? LocationSummary::kNoCall
6685 : LocationSummary::kCallOnSlowPath;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006686 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6687 locations->SetInAt(0, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006688 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
6689 // Require a register for the interface check since there is a loop that compares the class to
6690 // a memory address.
6691 locations->SetInAt(1, Location::RequiresRegister());
6692 } else {
6693 locations->SetInAt(1, Location::Any());
6694 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006695 // Note that TypeCheckSlowPathX86 uses this "temp" register too.
6696 locations->AddTemp(Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006697 // When read barriers are enabled, we need an additional temporary register for some cases.
6698 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
6699}
6700
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006701void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006702 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006703 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006704 Location obj_loc = locations->InAt(0);
6705 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006706 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006707 Location temp_loc = locations->GetTemp(0);
6708 Register temp = temp_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006709 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
6710 DCHECK_GE(num_temps, 1u);
6711 DCHECK_LE(num_temps, 2u);
6712 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
6713 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6714 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6715 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6716 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
6717 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
6718 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
6719 const uint32_t object_array_data_offset =
6720 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006721
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006722 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
6723 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
6724 // read barriers is done for performance and code size reasons.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006725 bool is_type_check_slow_path_fatal =
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006726 IsTypeCheckSlowPathFatal(type_check_kind, instruction->CanThrowIntoCatchBlock());
6727
Roland Levillain0d5a2812015-11-13 10:07:31 +00006728 SlowPathCode* type_check_slow_path =
6729 new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6730 is_type_check_slow_path_fatal);
6731 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006732
Roland Levillain0d5a2812015-11-13 10:07:31 +00006733 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006734 // Avoid null check if we know obj is not null.
6735 if (instruction->MustDoNullCheck()) {
6736 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006737 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006738 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006739
Roland Levillain0d5a2812015-11-13 10:07:31 +00006740 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006741 case TypeCheckKind::kExactCheck:
6742 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006743 // /* HeapReference<Class> */ temp = obj->klass_
6744 GenerateReferenceLoadTwoRegisters(instruction,
6745 temp_loc,
6746 obj_loc,
6747 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006748 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006749
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006750 if (cls.IsRegister()) {
6751 __ cmpl(temp, cls.AsRegister<Register>());
6752 } else {
6753 DCHECK(cls.IsStackSlot()) << cls;
6754 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6755 }
6756 // Jump to slow path for throwing the exception or doing a
6757 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006758 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006759 break;
6760 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006761
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006762 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006763 // /* HeapReference<Class> */ temp = obj->klass_
6764 GenerateReferenceLoadTwoRegisters(instruction,
6765 temp_loc,
6766 obj_loc,
6767 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006768 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006769
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006770 // If the class is abstract, we eagerly fetch the super class of the
6771 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006772 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006773 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006774 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006775 GenerateReferenceLoadOneRegister(instruction,
6776 temp_loc,
6777 super_offset,
6778 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006779 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006780
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006781 // If the class reference currently in `temp` is null, jump to the slow path to throw the
6782 // exception.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006783 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006784 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006785
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006786 // Otherwise, compare the classes
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006787 if (cls.IsRegister()) {
6788 __ cmpl(temp, cls.AsRegister<Register>());
6789 } else {
6790 DCHECK(cls.IsStackSlot()) << cls;
6791 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6792 }
6793 __ j(kNotEqual, &loop);
6794 break;
6795 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006796
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006797 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006798 // /* HeapReference<Class> */ temp = obj->klass_
6799 GenerateReferenceLoadTwoRegisters(instruction,
6800 temp_loc,
6801 obj_loc,
6802 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006803 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006804
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006805 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006806 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006807 __ Bind(&loop);
6808 if (cls.IsRegister()) {
6809 __ cmpl(temp, cls.AsRegister<Register>());
6810 } else {
6811 DCHECK(cls.IsStackSlot()) << cls;
6812 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6813 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006814 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006815
Roland Levillain0d5a2812015-11-13 10:07:31 +00006816 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006817 GenerateReferenceLoadOneRegister(instruction,
6818 temp_loc,
6819 super_offset,
6820 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006821 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006822
6823 // If the class reference currently in `temp` is not null, jump
6824 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006825 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006826 __ j(kNotZero, &loop);
6827 // Otherwise, jump to the slow path to throw the exception.;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006828 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006829 break;
6830 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006831
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006832 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006833 // /* HeapReference<Class> */ temp = obj->klass_
6834 GenerateReferenceLoadTwoRegisters(instruction,
6835 temp_loc,
6836 obj_loc,
6837 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006838 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006839
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006840 // Do an exact check.
6841 if (cls.IsRegister()) {
6842 __ cmpl(temp, cls.AsRegister<Register>());
6843 } else {
6844 DCHECK(cls.IsStackSlot()) << cls;
6845 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6846 }
6847 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006848
6849 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006850 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006851 GenerateReferenceLoadOneRegister(instruction,
6852 temp_loc,
6853 component_offset,
6854 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006855 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006856
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006857 // If the component type is null (i.e. the object not an array), jump to the slow path to
6858 // throw the exception. Otherwise proceed with the check.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006859 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006860 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006861
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006862 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006863 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006864 break;
6865 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006866
Calin Juravle98893e12015-10-02 21:05:03 +01006867 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006868 // We always go into the type check slow path for the unresolved check case.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006869 // We cannot directly call the CheckCast runtime entry point
6870 // without resorting to a type checking slow path here (i.e. by
6871 // calling InvokeRuntime directly), as it would require to
6872 // assign fixed registers for the inputs of this HInstanceOf
6873 // instruction (following the runtime calling convention), which
6874 // might be cluttered by the potential first read barrier
6875 // emission at the beginning of this method.
6876 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006877 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006878
6879 case TypeCheckKind::kInterfaceCheck: {
6880 // Fast path for the interface check. Since we compare with a memory location in the inner
6881 // loop we would need to have cls poisoned. However unpoisoning cls would reset the
6882 // conditional flags and cause the conditional jump to be incorrect. Therefore we just jump
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006883 // to the slow path if we are running under poisoning.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006884 if (!kPoisonHeapReferences) {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006885 // Try to avoid read barriers to improve the fast path. We can not get false positives by
6886 // doing this.
6887 // /* HeapReference<Class> */ temp = obj->klass_
6888 GenerateReferenceLoadTwoRegisters(instruction,
6889 temp_loc,
6890 obj_loc,
6891 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006892 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006893
6894 // /* HeapReference<Class> */ temp = temp->iftable_
6895 GenerateReferenceLoadTwoRegisters(instruction,
6896 temp_loc,
6897 temp_loc,
6898 iftable_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006899 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08006900 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006901 __ movl(maybe_temp2_loc.AsRegister<Register>(), Address(temp, array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08006902 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006903 NearLabel start_loop;
6904 __ Bind(&start_loop);
Mathieu Chartier6beced42016-11-15 15:51:31 -08006905 // Need to subtract first to handle the empty array case.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006906 __ subl(maybe_temp2_loc.AsRegister<Register>(), Immediate(2));
Mathieu Chartier6beced42016-11-15 15:51:31 -08006907 __ j(kNegative, type_check_slow_path->GetEntryLabel());
6908 // Go to next interface if the classes do not match.
6909 __ cmpl(cls.AsRegister<Register>(),
6910 CodeGeneratorX86::ArrayAddress(temp,
6911 maybe_temp2_loc,
6912 TIMES_4,
6913 object_array_data_offset));
6914 __ j(kNotEqual, &start_loop);
6915 } else {
6916 __ jmp(type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006917 }
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006918 break;
6919 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006920 }
6921 __ Bind(&done);
6922
Roland Levillain0d5a2812015-11-13 10:07:31 +00006923 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006924}
6925
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006926void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
6927 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006928 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006929 InvokeRuntimeCallingConvention calling_convention;
6930 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6931}
6932
6933void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006934 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject
6935 : kQuickUnlockObject,
Alexandre Rames8158f282015-08-07 10:26:17 +01006936 instruction,
Serban Constantinescuba45db02016-07-12 22:53:02 +01006937 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006938 if (instruction->IsEnter()) {
6939 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6940 } else {
6941 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6942 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006943}
6944
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006945void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6946void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6947void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6948
6949void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6950 LocationSummary* locations =
6951 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6952 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6953 || instruction->GetResultType() == Primitive::kPrimLong);
6954 locations->SetInAt(0, Location::RequiresRegister());
6955 locations->SetInAt(1, Location::Any());
6956 locations->SetOut(Location::SameAsFirstInput());
6957}
6958
6959void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
6960 HandleBitwiseOperation(instruction);
6961}
6962
6963void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
6964 HandleBitwiseOperation(instruction);
6965}
6966
6967void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
6968 HandleBitwiseOperation(instruction);
6969}
6970
6971void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6972 LocationSummary* locations = instruction->GetLocations();
6973 Location first = locations->InAt(0);
6974 Location second = locations->InAt(1);
6975 DCHECK(first.Equals(locations->Out()));
6976
6977 if (instruction->GetResultType() == Primitive::kPrimInt) {
6978 if (second.IsRegister()) {
6979 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006980 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006981 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006982 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006983 } else {
6984 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006985 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006986 }
6987 } else if (second.IsConstant()) {
6988 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006989 __ andl(first.AsRegister<Register>(),
6990 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006991 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006992 __ orl(first.AsRegister<Register>(),
6993 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006994 } else {
6995 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00006996 __ xorl(first.AsRegister<Register>(),
6997 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006998 }
6999 } else {
7000 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007001 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007002 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007003 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007004 } else {
7005 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00007006 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007007 }
7008 }
7009 } else {
7010 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
7011 if (second.IsRegisterPair()) {
7012 if (instruction->IsAnd()) {
7013 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7014 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7015 } else if (instruction->IsOr()) {
7016 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7017 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7018 } else {
7019 DCHECK(instruction->IsXor());
7020 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7021 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7022 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007023 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007024 if (instruction->IsAnd()) {
7025 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7026 __ andl(first.AsRegisterPairHigh<Register>(),
7027 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7028 } else if (instruction->IsOr()) {
7029 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7030 __ orl(first.AsRegisterPairHigh<Register>(),
7031 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7032 } else {
7033 DCHECK(instruction->IsXor());
7034 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7035 __ xorl(first.AsRegisterPairHigh<Register>(),
7036 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7037 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007038 } else {
7039 DCHECK(second.IsConstant()) << second;
7040 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007041 int32_t low_value = Low32Bits(value);
7042 int32_t high_value = High32Bits(value);
7043 Immediate low(low_value);
7044 Immediate high(high_value);
7045 Register first_low = first.AsRegisterPairLow<Register>();
7046 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007047 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007048 if (low_value == 0) {
7049 __ xorl(first_low, first_low);
7050 } else if (low_value != -1) {
7051 __ andl(first_low, low);
7052 }
7053 if (high_value == 0) {
7054 __ xorl(first_high, first_high);
7055 } else if (high_value != -1) {
7056 __ andl(first_high, high);
7057 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007058 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007059 if (low_value != 0) {
7060 __ orl(first_low, low);
7061 }
7062 if (high_value != 0) {
7063 __ orl(first_high, high);
7064 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007065 } else {
7066 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007067 if (low_value != 0) {
7068 __ xorl(first_low, low);
7069 }
7070 if (high_value != 0) {
7071 __ xorl(first_high, high);
7072 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007073 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007074 }
7075 }
7076}
7077
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007078void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(
7079 HInstruction* instruction,
7080 Location out,
7081 uint32_t offset,
7082 Location maybe_temp,
7083 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007084 Register out_reg = out.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007085 if (read_barrier_option == kWithReadBarrier) {
7086 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007087 if (kUseBakerReadBarrier) {
7088 // Load with fast path based Baker's read barrier.
7089 // /* HeapReference<Object> */ out = *(out + offset)
7090 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00007091 instruction, out, out_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007092 } else {
7093 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007094 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain7c1559a2015-12-15 10:55:36 +00007095 // in the following move operation, as we will need it for the
7096 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00007097 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007098 __ movl(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007099 // /* HeapReference<Object> */ out = *(out + offset)
7100 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007101 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007102 }
7103 } else {
7104 // Plain load with no read barrier.
7105 // /* HeapReference<Object> */ out = *(out + offset)
7106 __ movl(out_reg, Address(out_reg, offset));
7107 __ MaybeUnpoisonHeapReference(out_reg);
7108 }
7109}
7110
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007111void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(
7112 HInstruction* instruction,
7113 Location out,
7114 Location obj,
7115 uint32_t offset,
7116 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007117 Register out_reg = out.AsRegister<Register>();
7118 Register obj_reg = obj.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007119 if (read_barrier_option == kWithReadBarrier) {
7120 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007121 if (kUseBakerReadBarrier) {
7122 // Load with fast path based Baker's read barrier.
7123 // /* HeapReference<Object> */ out = *(obj + offset)
7124 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00007125 instruction, out, obj_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007126 } else {
7127 // Load with slow path based read barrier.
7128 // /* HeapReference<Object> */ out = *(obj + offset)
7129 __ movl(out_reg, Address(obj_reg, offset));
7130 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
7131 }
7132 } else {
7133 // Plain load with no read barrier.
7134 // /* HeapReference<Object> */ out = *(obj + offset)
7135 __ movl(out_reg, Address(obj_reg, offset));
7136 __ MaybeUnpoisonHeapReference(out_reg);
7137 }
7138}
7139
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007140void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(
7141 HInstruction* instruction,
7142 Location root,
7143 const Address& address,
7144 Label* fixup_label,
7145 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007146 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007147 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07007148 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007149 if (kUseBakerReadBarrier) {
7150 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
7151 // Baker's read barrier are used:
7152 //
Roland Levillaind966ce72017-02-09 16:20:14 +00007153 // root = obj.field;
7154 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
7155 // if (temp != null) {
7156 // root = temp(root)
Roland Levillain7c1559a2015-12-15 10:55:36 +00007157 // }
7158
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007159 // /* GcRoot<mirror::Object> */ root = *address
7160 __ movl(root_reg, address);
7161 if (fixup_label != nullptr) {
7162 __ Bind(fixup_label);
7163 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007164 static_assert(
7165 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
7166 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
7167 "have different sizes.");
7168 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
7169 "art::mirror::CompressedReference<mirror::Object> and int32_t "
7170 "have different sizes.");
7171
Vladimir Marko953437b2016-08-24 08:30:46 +00007172 // Slow path marking the GC root `root`.
7173 SlowPathCode* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007174 instruction, root, /* unpoison_ref_before_marking */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007175 codegen_->AddSlowPath(slow_path);
7176
Roland Levillaind966ce72017-02-09 16:20:14 +00007177 // Test the entrypoint (`Thread::Current()->pReadBarrierMarkReg ## root.reg()`).
7178 const int32_t entry_point_offset =
7179 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kX86PointerSize>(root.reg());
7180 __ fs()->cmpl(Address::Absolute(entry_point_offset), Immediate(0));
7181 // The entrypoint is null when the GC is not marking.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007182 __ j(kNotEqual, slow_path->GetEntryLabel());
7183 __ Bind(slow_path->GetExitLabel());
7184 } else {
7185 // GC root loaded through a slow path for read barriers other
7186 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007187 // /* GcRoot<mirror::Object>* */ root = address
7188 __ leal(root_reg, address);
7189 if (fixup_label != nullptr) {
7190 __ Bind(fixup_label);
7191 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007192 // /* mirror::Object* */ root = root->Read()
7193 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
7194 }
7195 } else {
7196 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007197 // /* GcRoot<mirror::Object> */ root = *address
7198 __ movl(root_reg, address);
7199 if (fixup_label != nullptr) {
7200 __ Bind(fixup_label);
7201 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007202 // Note that GC roots are not affected by heap poisoning, thus we
7203 // do not have to unpoison `root_reg` here.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007204 }
7205}
7206
7207void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
7208 Location ref,
7209 Register obj,
7210 uint32_t offset,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007211 bool needs_null_check) {
7212 DCHECK(kEmitCompilerReadBarrier);
7213 DCHECK(kUseBakerReadBarrier);
7214
7215 // /* HeapReference<Object> */ ref = *(obj + offset)
7216 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007217 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007218}
7219
7220void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
7221 Location ref,
7222 Register obj,
7223 uint32_t data_offset,
7224 Location index,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007225 bool needs_null_check) {
7226 DCHECK(kEmitCompilerReadBarrier);
7227 DCHECK(kUseBakerReadBarrier);
7228
Roland Levillain3d312422016-06-23 13:53:42 +01007229 static_assert(
7230 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
7231 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00007232 // /* HeapReference<Object> */ ref =
7233 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007234 Address src = CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007235 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007236}
7237
7238void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
7239 Location ref,
7240 Register obj,
7241 const Address& src,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007242 bool needs_null_check,
7243 bool always_update_field,
7244 Register* temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007245 DCHECK(kEmitCompilerReadBarrier);
7246 DCHECK(kUseBakerReadBarrier);
7247
7248 // In slow path based read barriers, the read barrier call is
7249 // inserted after the original load. However, in fast path based
7250 // Baker's read barriers, we need to perform the load of
7251 // mirror::Object::monitor_ *before* the original reference load.
7252 // This load-load ordering is required by the read barrier.
7253 // The fast path/slow path (for Baker's algorithm) should look like:
7254 //
7255 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7256 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7257 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007258 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain7c1559a2015-12-15 10:55:36 +00007259 // if (is_gray) {
7260 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7261 // }
7262 //
7263 // Note: the original implementation in ReadBarrier::Barrier is
7264 // slightly more complex as:
7265 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007266 // the high-bits of rb_state, which are expected to be all zeroes
7267 // (we use CodeGeneratorX86::GenerateMemoryBarrier instead here,
7268 // which is a no-op thanks to the x86 memory model);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007269 // - it performs additional checks that we do not do here for
7270 // performance reasons.
7271
7272 Register ref_reg = ref.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +00007273 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7274
Vladimir Marko953437b2016-08-24 08:30:46 +00007275 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007276 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
7277 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko953437b2016-08-24 08:30:46 +00007278 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
7279 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
7280 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
7281
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007282 // if (rb_state == ReadBarrier::GrayState())
Vladimir Marko953437b2016-08-24 08:30:46 +00007283 // ref = ReadBarrier::Mark(ref);
7284 // At this point, just do the "if" and make sure that flags are preserved until the branch.
7285 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain7c1559a2015-12-15 10:55:36 +00007286 if (needs_null_check) {
7287 MaybeRecordImplicitNullCheck(instruction);
7288 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007289
7290 // Load fence to prevent load-load reordering.
7291 // Note that this is a no-op, thanks to the x86 memory model.
7292 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
7293
7294 // The actual reference load.
7295 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00007296 __ movl(ref_reg, src); // Flags are unaffected.
7297
7298 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
7299 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007300 SlowPathCode* slow_path;
7301 if (always_update_field) {
7302 DCHECK(temp != nullptr);
7303 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkAndUpdateFieldSlowPathX86(
7304 instruction, ref, obj, src, /* unpoison_ref_before_marking */ true, *temp);
7305 } else {
7306 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(
7307 instruction, ref, /* unpoison_ref_before_marking */ true);
7308 }
Vladimir Marko953437b2016-08-24 08:30:46 +00007309 AddSlowPath(slow_path);
7310
7311 // We have done the "if" of the gray bit check above, now branch based on the flags.
7312 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain7c1559a2015-12-15 10:55:36 +00007313
7314 // Object* ref = ref_addr->AsMirrorPtr()
7315 __ MaybeUnpoisonHeapReference(ref_reg);
7316
Roland Levillain7c1559a2015-12-15 10:55:36 +00007317 __ Bind(slow_path->GetExitLabel());
7318}
7319
7320void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
7321 Location out,
7322 Location ref,
7323 Location obj,
7324 uint32_t offset,
7325 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007326 DCHECK(kEmitCompilerReadBarrier);
7327
Roland Levillain7c1559a2015-12-15 10:55:36 +00007328 // Insert a slow path based read barrier *after* the reference load.
7329 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007330 // If heap poisoning is enabled, the unpoisoning of the loaded
7331 // reference will be carried out by the runtime within the slow
7332 // path.
7333 //
7334 // Note that `ref` currently does not get unpoisoned (when heap
7335 // poisoning is enabled), which is alright as the `ref` argument is
7336 // not used by the artReadBarrierSlow entry point.
7337 //
7338 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
7339 SlowPathCode* slow_path = new (GetGraph()->GetArena())
7340 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
7341 AddSlowPath(slow_path);
7342
Roland Levillain0d5a2812015-11-13 10:07:31 +00007343 __ jmp(slow_path->GetEntryLabel());
7344 __ Bind(slow_path->GetExitLabel());
7345}
7346
Roland Levillain7c1559a2015-12-15 10:55:36 +00007347void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7348 Location out,
7349 Location ref,
7350 Location obj,
7351 uint32_t offset,
7352 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007353 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007354 // Baker's read barriers shall be handled by the fast path
7355 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
7356 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007357 // If heap poisoning is enabled, unpoisoning will be taken care of
7358 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007359 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007360 } else if (kPoisonHeapReferences) {
7361 __ UnpoisonHeapReference(out.AsRegister<Register>());
7362 }
7363}
7364
Roland Levillain7c1559a2015-12-15 10:55:36 +00007365void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7366 Location out,
7367 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007368 DCHECK(kEmitCompilerReadBarrier);
7369
Roland Levillain7c1559a2015-12-15 10:55:36 +00007370 // Insert a slow path based read barrier *after* the GC root load.
7371 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007372 // Note that GC roots are not affected by heap poisoning, so we do
7373 // not need to do anything special for this here.
7374 SlowPathCode* slow_path =
7375 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86(instruction, out, root);
7376 AddSlowPath(slow_path);
7377
Roland Levillain0d5a2812015-11-13 10:07:31 +00007378 __ jmp(slow_path->GetEntryLabel());
7379 __ Bind(slow_path->GetExitLabel());
7380}
7381
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007382void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007383 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007384 LOG(FATAL) << "Unreachable";
7385}
7386
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007387void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007388 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007389 LOG(FATAL) << "Unreachable";
7390}
7391
Mark Mendellfe57faa2015-09-18 09:26:15 -04007392// Simple implementation of packed switch - generate cascaded compare/jumps.
7393void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7394 LocationSummary* locations =
7395 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7396 locations->SetInAt(0, Location::RequiresRegister());
7397}
7398
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007399void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
7400 int32_t lower_bound,
7401 uint32_t num_entries,
7402 HBasicBlock* switch_block,
7403 HBasicBlock* default_block) {
7404 // Figure out the correct compare values and jump conditions.
7405 // Handle the first compare/branch as a special case because it might
7406 // jump to the default case.
7407 DCHECK_GT(num_entries, 2u);
7408 Condition first_condition;
7409 uint32_t index;
7410 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
7411 if (lower_bound != 0) {
7412 first_condition = kLess;
7413 __ cmpl(value_reg, Immediate(lower_bound));
7414 __ j(first_condition, codegen_->GetLabelOf(default_block));
7415 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007416
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007417 index = 1;
7418 } else {
7419 // Handle all the compare/jumps below.
7420 first_condition = kBelow;
7421 index = 0;
7422 }
7423
7424 // Handle the rest of the compare/jumps.
7425 for (; index + 1 < num_entries; index += 2) {
7426 int32_t compare_to_value = lower_bound + index + 1;
7427 __ cmpl(value_reg, Immediate(compare_to_value));
7428 // Jump to successors[index] if value < case_value[index].
7429 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
7430 // Jump to successors[index + 1] if value == case_value[index + 1].
7431 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
7432 }
7433
7434 if (index != num_entries) {
7435 // There are an odd number of entries. Handle the last one.
7436 DCHECK_EQ(index + 1, num_entries);
7437 __ cmpl(value_reg, Immediate(lower_bound + index));
7438 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007439 }
7440
7441 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007442 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
7443 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007444 }
7445}
7446
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007447void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7448 int32_t lower_bound = switch_instr->GetStartValue();
7449 uint32_t num_entries = switch_instr->GetNumEntries();
7450 LocationSummary* locations = switch_instr->GetLocations();
7451 Register value_reg = locations->InAt(0).AsRegister<Register>();
7452
7453 GenPackedSwitchWithCompares(value_reg,
7454 lower_bound,
7455 num_entries,
7456 switch_instr->GetBlock(),
7457 switch_instr->GetDefaultBlock());
7458}
7459
Mark Mendell805b3b52015-09-18 14:10:29 -04007460void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7461 LocationSummary* locations =
7462 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7463 locations->SetInAt(0, Location::RequiresRegister());
7464
7465 // Constant area pointer.
7466 locations->SetInAt(1, Location::RequiresRegister());
7467
7468 // And the temporary we need.
7469 locations->AddTemp(Location::RequiresRegister());
7470}
7471
7472void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7473 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007474 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04007475 LocationSummary* locations = switch_instr->GetLocations();
7476 Register value_reg = locations->InAt(0).AsRegister<Register>();
7477 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7478
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007479 if (num_entries <= kPackedSwitchJumpTableThreshold) {
7480 GenPackedSwitchWithCompares(value_reg,
7481 lower_bound,
7482 num_entries,
7483 switch_instr->GetBlock(),
7484 default_block);
7485 return;
7486 }
7487
Mark Mendell805b3b52015-09-18 14:10:29 -04007488 // Optimizing has a jump area.
7489 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7490 Register constant_area = locations->InAt(1).AsRegister<Register>();
7491
7492 // Remove the bias, if needed.
7493 if (lower_bound != 0) {
7494 __ leal(temp_reg, Address(value_reg, -lower_bound));
7495 value_reg = temp_reg;
7496 }
7497
7498 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007499 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04007500 __ cmpl(value_reg, Immediate(num_entries - 1));
7501 __ j(kAbove, codegen_->GetLabelOf(default_block));
7502
7503 // We are in the range of the table.
7504 // Load (target-constant_area) from the jump table, indexing by the value.
7505 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
7506
7507 // Compute the actual target address by adding in constant_area.
7508 __ addl(temp_reg, constant_area);
7509
7510 // And jump.
7511 __ jmp(temp_reg);
7512}
7513
Mark Mendell0616ae02015-04-17 12:49:27 -04007514void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
7515 HX86ComputeBaseMethodAddress* insn) {
7516 LocationSummary* locations =
7517 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7518 locations->SetOut(Location::RequiresRegister());
7519}
7520
7521void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
7522 HX86ComputeBaseMethodAddress* insn) {
7523 LocationSummary* locations = insn->GetLocations();
7524 Register reg = locations->Out().AsRegister<Register>();
7525
7526 // Generate call to next instruction.
7527 Label next_instruction;
7528 __ call(&next_instruction);
7529 __ Bind(&next_instruction);
7530
7531 // Remember this offset for later use with constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007532 codegen_->AddMethodAddressOffset(insn, GetAssembler()->CodeSize());
Mark Mendell0616ae02015-04-17 12:49:27 -04007533
7534 // Grab the return address off the stack.
7535 __ popl(reg);
7536}
7537
7538void LocationsBuilderX86::VisitX86LoadFromConstantTable(
7539 HX86LoadFromConstantTable* insn) {
7540 LocationSummary* locations =
7541 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7542
7543 locations->SetInAt(0, Location::RequiresRegister());
7544 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
7545
7546 // If we don't need to be materialized, we only need the inputs to be set.
David Brazdilb3e773e2016-01-26 11:28:37 +00007547 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007548 return;
7549 }
7550
7551 switch (insn->GetType()) {
7552 case Primitive::kPrimFloat:
7553 case Primitive::kPrimDouble:
7554 locations->SetOut(Location::RequiresFpuRegister());
7555 break;
7556
7557 case Primitive::kPrimInt:
7558 locations->SetOut(Location::RequiresRegister());
7559 break;
7560
7561 default:
7562 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7563 }
7564}
7565
7566void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
David Brazdilb3e773e2016-01-26 11:28:37 +00007567 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007568 return;
7569 }
7570
7571 LocationSummary* locations = insn->GetLocations();
7572 Location out = locations->Out();
7573 Register const_area = locations->InAt(0).AsRegister<Register>();
7574 HConstant *value = insn->GetConstant();
7575
7576 switch (insn->GetType()) {
7577 case Primitive::kPrimFloat:
7578 __ movss(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007579 codegen_->LiteralFloatAddress(
7580 value->AsFloatConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04007581 break;
7582
7583 case Primitive::kPrimDouble:
7584 __ movsd(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007585 codegen_->LiteralDoubleAddress(
7586 value->AsDoubleConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04007587 break;
7588
7589 case Primitive::kPrimInt:
7590 __ movl(out.AsRegister<Register>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007591 codegen_->LiteralInt32Address(
7592 value->AsIntConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04007593 break;
7594
7595 default:
7596 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7597 }
7598}
7599
Mark Mendell0616ae02015-04-17 12:49:27 -04007600/**
7601 * Class to handle late fixup of offsets into constant area.
7602 */
Vladimir Marko5233f932015-09-29 19:01:15 +01007603class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04007604 public:
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007605 RIPFixup(CodeGeneratorX86& codegen,
7606 HX86ComputeBaseMethodAddress* base_method_address,
7607 size_t offset)
7608 : codegen_(&codegen),
7609 base_method_address_(base_method_address),
7610 offset_into_constant_area_(offset) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04007611
7612 protected:
7613 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
7614
7615 CodeGeneratorX86* codegen_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007616 HX86ComputeBaseMethodAddress* base_method_address_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007617
7618 private:
7619 void Process(const MemoryRegion& region, int pos) OVERRIDE {
7620 // Patch the correct offset for the instruction. The place to patch is the
7621 // last 4 bytes of the instruction.
7622 // The value to patch is the distance from the offset in the constant area
7623 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04007624 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007625 int32_t relative_position =
7626 constant_offset - codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell0616ae02015-04-17 12:49:27 -04007627
7628 // Patch in the right value.
7629 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
7630 }
7631
Mark Mendell0616ae02015-04-17 12:49:27 -04007632 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04007633 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007634};
7635
Mark Mendell805b3b52015-09-18 14:10:29 -04007636/**
7637 * Class to handle late fixup of offsets to a jump table that will be created in the
7638 * constant area.
7639 */
7640class JumpTableRIPFixup : public RIPFixup {
7641 public:
7642 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007643 : RIPFixup(codegen, switch_instr->GetBaseMethodAddress(), static_cast<size_t>(-1)),
7644 switch_instr_(switch_instr) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04007645
7646 void CreateJumpTable() {
7647 X86Assembler* assembler = codegen_->GetAssembler();
7648
7649 // Ensure that the reference to the jump table has the correct offset.
7650 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
7651 SetOffset(offset_in_constant_table);
7652
7653 // The label values in the jump table are computed relative to the
7654 // instruction addressing the constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007655 const int32_t relative_offset = codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell805b3b52015-09-18 14:10:29 -04007656
7657 // Populate the jump table with the correct values for the jump table.
7658 int32_t num_entries = switch_instr_->GetNumEntries();
7659 HBasicBlock* block = switch_instr_->GetBlock();
7660 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
7661 // The value that we want is the target offset - the position of the table.
7662 for (int32_t i = 0; i < num_entries; i++) {
7663 HBasicBlock* b = successors[i];
7664 Label* l = codegen_->GetLabelOf(b);
7665 DCHECK(l->IsBound());
7666 int32_t offset_to_block = l->Position() - relative_offset;
7667 assembler->AppendInt32(offset_to_block);
7668 }
7669 }
7670
7671 private:
7672 const HX86PackedSwitch* switch_instr_;
7673};
7674
7675void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
7676 // Generate the constant area if needed.
7677 X86Assembler* assembler = GetAssembler();
7678 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
7679 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
7680 // byte values.
7681 assembler->Align(4, 0);
7682 constant_area_start_ = assembler->CodeSize();
7683
7684 // Populate any jump tables.
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007685 for (JumpTableRIPFixup* jump_table : fixups_to_jump_tables_) {
Mark Mendell805b3b52015-09-18 14:10:29 -04007686 jump_table->CreateJumpTable();
7687 }
7688
7689 // And now add the constant area to the generated code.
7690 assembler->AddConstantArea();
7691 }
7692
7693 // And finish up.
7694 CodeGenerator::Finalize(allocator);
7695}
7696
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007697Address CodeGeneratorX86::LiteralDoubleAddress(double v,
7698 HX86ComputeBaseMethodAddress* method_base,
7699 Register reg) {
7700 AssemblerFixup* fixup =
7701 new (GetGraph()->GetArena()) RIPFixup(*this, method_base, __ AddDouble(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04007702 return Address(reg, kDummy32BitOffset, fixup);
7703}
7704
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007705Address CodeGeneratorX86::LiteralFloatAddress(float v,
7706 HX86ComputeBaseMethodAddress* method_base,
7707 Register reg) {
7708 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, method_base, __ AddFloat(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04007709 return Address(reg, kDummy32BitOffset, fixup);
7710}
7711
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007712Address CodeGeneratorX86::LiteralInt32Address(int32_t v,
7713 HX86ComputeBaseMethodAddress* method_base,
7714 Register reg) {
7715 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, method_base, __ AddInt32(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04007716 return Address(reg, kDummy32BitOffset, fixup);
7717}
7718
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007719Address CodeGeneratorX86::LiteralInt64Address(int64_t v,
7720 HX86ComputeBaseMethodAddress* method_base,
7721 Register reg) {
7722 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, method_base, __ AddInt64(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04007723 return Address(reg, kDummy32BitOffset, fixup);
7724}
7725
Aart Bika19616e2016-02-01 18:57:58 -08007726void CodeGeneratorX86::Load32BitValue(Register dest, int32_t value) {
7727 if (value == 0) {
7728 __ xorl(dest, dest);
7729 } else {
7730 __ movl(dest, Immediate(value));
7731 }
7732}
7733
7734void CodeGeneratorX86::Compare32BitValue(Register dest, int32_t value) {
7735 if (value == 0) {
7736 __ testl(dest, dest);
7737 } else {
7738 __ cmpl(dest, Immediate(value));
7739 }
7740}
7741
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007742void CodeGeneratorX86::GenerateIntCompare(Location lhs, Location rhs) {
7743 Register lhs_reg = lhs.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07007744 GenerateIntCompare(lhs_reg, rhs);
7745}
7746
7747void CodeGeneratorX86::GenerateIntCompare(Register lhs, Location rhs) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007748 if (rhs.IsConstant()) {
7749 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
jessicahandojo4877b792016-09-08 19:49:13 -07007750 Compare32BitValue(lhs, value);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007751 } else if (rhs.IsStackSlot()) {
jessicahandojo4877b792016-09-08 19:49:13 -07007752 __ cmpl(lhs, Address(ESP, rhs.GetStackIndex()));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007753 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07007754 __ cmpl(lhs, rhs.AsRegister<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007755 }
7756}
7757
7758Address CodeGeneratorX86::ArrayAddress(Register obj,
7759 Location index,
7760 ScaleFactor scale,
7761 uint32_t data_offset) {
7762 return index.IsConstant() ?
7763 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << scale) + data_offset) :
7764 Address(obj, index.AsRegister<Register>(), scale, data_offset);
7765}
7766
Mark Mendell805b3b52015-09-18 14:10:29 -04007767Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
7768 Register reg,
7769 Register value) {
7770 // Create a fixup to be used to create and address the jump table.
7771 JumpTableRIPFixup* table_fixup =
7772 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
7773
7774 // We have to populate the jump tables.
7775 fixups_to_jump_tables_.push_back(table_fixup);
7776
7777 // We want a scaled address, as we are extracting the correct offset from the table.
7778 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
7779}
7780
Andreas Gampe85b62f22015-09-09 13:15:38 -07007781// TODO: target as memory.
7782void CodeGeneratorX86::MoveFromReturnRegister(Location target, Primitive::Type type) {
7783 if (!target.IsValid()) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007784 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007785 return;
7786 }
7787
7788 DCHECK_NE(type, Primitive::kPrimVoid);
7789
7790 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
7791 if (target.Equals(return_loc)) {
7792 return;
7793 }
7794
7795 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7796 // with the else branch.
7797 if (type == Primitive::kPrimLong) {
7798 HParallelMove parallel_move(GetGraph()->GetArena());
7799 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), Primitive::kPrimInt, nullptr);
7800 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), Primitive::kPrimInt, nullptr);
7801 GetMoveResolver()->EmitNativeCode(&parallel_move);
7802 } else {
7803 // Let the parallel move resolver take care of all of this.
7804 HParallelMove parallel_move(GetGraph()->GetArena());
7805 parallel_move.AddMove(return_loc, target, type, nullptr);
7806 GetMoveResolver()->EmitNativeCode(&parallel_move);
7807 }
7808}
7809
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007810void CodeGeneratorX86::PatchJitRootUse(uint8_t* code,
7811 const uint8_t* roots_data,
7812 const PatchInfo<Label>& info,
7813 uint64_t index_in_table) const {
7814 uint32_t code_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
7815 uintptr_t address =
7816 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
7817 typedef __attribute__((__aligned__(1))) uint32_t unaligned_uint32_t;
7818 reinterpret_cast<unaligned_uint32_t*>(code + code_offset)[0] =
7819 dchecked_integral_cast<uint32_t>(address);
7820}
7821
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007822void CodeGeneratorX86::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
7823 for (const PatchInfo<Label>& info : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007824 const auto it = jit_string_roots_.find(
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007825 StringReference(&info.dex_file, dex::StringIndex(info.index)));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007826 DCHECK(it != jit_string_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007827 uint64_t index_in_table = it->second;
7828 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007829 }
7830
7831 for (const PatchInfo<Label>& info : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007832 const auto it = jit_class_roots_.find(
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007833 TypeReference(&info.dex_file, dex::TypeIndex(info.index)));
7834 DCHECK(it != jit_class_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007835 uint64_t index_in_table = it->second;
7836 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007837 }
7838}
7839
Roland Levillain4d027112015-07-01 15:41:14 +01007840#undef __
7841
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007842} // namespace x86
7843} // namespace art