blob: 828e7ffd1d9ae60cce41414e15a842c2ca300f1b [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"
Vladimir Marko94ec2db2017-09-06 17:21:03 +010020#include "class_table.h"
Guillaume Sanchez0f88e872015-03-30 17:55:45 +010021#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000022#include "compiled_method.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010023#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000024#include "entrypoints/quick/quick_entrypoints_enum.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010025#include "gc/accounting/card_table.h"
Andreas Gampe09659c22017-09-18 18:23:32 -070026#include "heap_poisoning.h"
Mark Mendell09ed1a32015-03-25 08:30:06 -040027#include "intrinsics.h"
28#include "intrinsics_x86.h"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010029#include "linker/linker_patch.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070030#include "lock_word.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070031#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070032#include "mirror/class-inl.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010033#include "thread.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000034#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010035#include "utils/stack_checks.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000036#include "utils/x86/assembler_x86.h"
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010037#include "utils/x86/managed_register_x86.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000038
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000039namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010040
Roland Levillain0d5a2812015-11-13 10:07:31 +000041template<class MirrorType>
42class GcRoot;
43
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000044namespace x86 {
45
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010046static constexpr int kCurrentMethodStackOffset = 0;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010047static constexpr Register kMethodRegisterArgument = EAX;
Mark Mendell5f874182015-03-04 15:42:45 -050048static constexpr Register kCoreCalleeSaves[] = { EBP, ESI, EDI };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010049
Mark Mendell24f2dfa2015-01-14 19:51:45 -050050static constexpr int kC2ConditionMask = 0x400;
51
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000052static constexpr int kFakeReturnRegister = Register(8);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +000053
Roland Levillain7cbd27f2016-08-11 23:53:33 +010054// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
55#define __ down_cast<X86Assembler*>(codegen->GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -070056#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kX86PointerSize, x).Int32Value()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010057
Andreas Gampe85b62f22015-09-09 13:15:38 -070058class NullCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010059 public:
David Srbecky9cd6d372016-02-09 15:24:47 +000060 explicit NullCheckSlowPathX86(HNullCheck* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010061
Alexandre Rames2ed20af2015-03-06 13:55:35 +000062 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +010063 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010064 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000065 if (instruction_->CanThrowIntoCatchBlock()) {
66 // Live registers will be restored in the catch block if caught.
67 SaveLiveRegisters(codegen, instruction_->GetLocations());
68 }
Serban Constantinescuba45db02016-07-12 22:53:02 +010069 x86_codegen->InvokeRuntime(kQuickThrowNullPointer,
Alexandre Rames8158f282015-08-07 10:26:17 +010070 instruction_,
71 instruction_->GetDexPc(),
72 this);
Roland Levillain888d0672015-11-23 18:53:50 +000073 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +010074 }
75
Alexandre Rames8158f282015-08-07 10:26:17 +010076 bool IsFatal() const OVERRIDE { return true; }
77
Alexandre Rames9931f312015-06-19 14:47:01 +010078 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathX86"; }
79
Nicolas Geoffraye5038322014-07-04 09:41:32 +010080 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +010081 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86);
82};
83
Andreas Gampe85b62f22015-09-09 13:15:38 -070084class DivZeroCheckSlowPathX86 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +000085 public:
David Srbecky9cd6d372016-02-09 15:24:47 +000086 explicit DivZeroCheckSlowPathX86(HDivZeroCheck* instruction) : SlowPathCode(instruction) {}
Calin Juravled0d48522014-11-04 16:40:20 +000087
Alexandre Rames2ed20af2015-03-06 13:55:35 +000088 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +010089 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Calin Juravled0d48522014-11-04 16:40:20 +000090 __ Bind(GetEntryLabel());
Serban Constantinescuba45db02016-07-12 22:53:02 +010091 x86_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +000092 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Calin Juravled0d48522014-11-04 16:40:20 +000093 }
94
Alexandre Rames8158f282015-08-07 10:26:17 +010095 bool IsFatal() const OVERRIDE { return true; }
96
Alexandre Rames9931f312015-06-19 14:47:01 +010097 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathX86"; }
98
Calin Juravled0d48522014-11-04 16:40:20 +000099 private:
Calin Juravled0d48522014-11-04 16:40:20 +0000100 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86);
101};
102
Andreas Gampe85b62f22015-09-09 13:15:38 -0700103class DivRemMinusOneSlowPathX86 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +0000104 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000105 DivRemMinusOneSlowPathX86(HInstruction* instruction, Register reg, bool is_div)
106 : SlowPathCode(instruction), reg_(reg), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000107
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000108 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +0000109 __ Bind(GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +0000110 if (is_div_) {
111 __ negl(reg_);
112 } else {
113 __ movl(reg_, Immediate(0));
114 }
Calin Juravled0d48522014-11-04 16:40:20 +0000115 __ jmp(GetExitLabel());
116 }
117
Alexandre Rames9931f312015-06-19 14:47:01 +0100118 const char* GetDescription() const OVERRIDE { return "DivRemMinusOneSlowPathX86"; }
119
Calin Juravled0d48522014-11-04 16:40:20 +0000120 private:
121 Register reg_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000122 bool is_div_;
123 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86);
Calin Juravled0d48522014-11-04 16:40:20 +0000124};
125
Andreas Gampe85b62f22015-09-09 13:15:38 -0700126class BoundsCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100127 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000128 explicit BoundsCheckSlowPathX86(HBoundsCheck* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100129
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000130 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100131 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100132 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100133 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000134 // We're moving two locations to locations that could overlap, so we need a parallel
135 // move resolver.
David Brazdil77a48ae2015-09-15 12:34:04 +0000136 if (instruction_->CanThrowIntoCatchBlock()) {
137 // Live registers will be restored in the catch block if caught.
138 SaveLiveRegisters(codegen, instruction_->GetLocations());
139 }
Mark Mendellee8d9712016-07-12 11:13:15 -0400140
141 // Are we using an array length from memory?
142 HInstruction* array_length = instruction_->InputAt(1);
143 Location length_loc = locations->InAt(1);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100144 InvokeRuntimeCallingConvention calling_convention;
Mark Mendellee8d9712016-07-12 11:13:15 -0400145 if (array_length->IsArrayLength() && array_length->IsEmittedAtUseSite()) {
146 // Load the array length into our temporary.
Nicolas Geoffray0aff3a82017-10-13 13:12:36 +0100147 HArrayLength* length = array_length->AsArrayLength();
148 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(length);
Mark Mendellee8d9712016-07-12 11:13:15 -0400149 Location array_loc = array_length->GetLocations()->InAt(0);
150 Address array_len(array_loc.AsRegister<Register>(), len_offset);
151 length_loc = Location::RegisterLocation(calling_convention.GetRegisterAt(1));
152 // Check for conflicts with index.
153 if (length_loc.Equals(locations->InAt(0))) {
154 // We know we aren't using parameter 2.
155 length_loc = Location::RegisterLocation(calling_convention.GetRegisterAt(2));
156 }
157 __ movl(length_loc.AsRegister<Register>(), array_len);
Nicolas Geoffray0aff3a82017-10-13 13:12:36 +0100158 if (mirror::kUseStringCompression && length->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +0100159 __ shrl(length_loc.AsRegister<Register>(), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -0700160 }
Mark Mendellee8d9712016-07-12 11:13:15 -0400161 }
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000162 x86_codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100163 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000164 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100165 DataType::Type::kInt32,
Mark Mendellee8d9712016-07-12 11:13:15 -0400166 length_loc,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100167 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100168 DataType::Type::kInt32);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100169 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
170 ? kQuickThrowStringBounds
171 : kQuickThrowArrayBounds;
172 x86_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100173 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Roland Levillain888d0672015-11-23 18:53:50 +0000174 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100175 }
176
Alexandre Rames8158f282015-08-07 10:26:17 +0100177 bool IsFatal() const OVERRIDE { return true; }
178
Alexandre Rames9931f312015-06-19 14:47:01 +0100179 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathX86"; }
180
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100181 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100182 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
183};
184
Andreas Gampe85b62f22015-09-09 13:15:38 -0700185class SuspendCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000186 public:
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000187 SuspendCheckSlowPathX86(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000188 : SlowPathCode(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000189
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000190 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bikb13c65b2017-03-21 20:14:07 -0700191 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100192 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000193 __ Bind(GetEntryLabel());
Aart Bik24b905f2017-04-06 09:59:06 -0700194 SaveLiveRegisters(codegen, locations); // Only saves full width XMM for SIMD.
Serban Constantinescuba45db02016-07-12 22:53:02 +0100195 x86_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000196 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Aart Bik24b905f2017-04-06 09:59:06 -0700197 RestoreLiveRegisters(codegen, locations); // Only restores full width XMM for SIMD.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100198 if (successor_ == nullptr) {
199 __ jmp(GetReturnLabel());
200 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100201 __ jmp(x86_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100202 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000203 }
204
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100205 Label* GetReturnLabel() {
206 DCHECK(successor_ == nullptr);
207 return &return_label_;
208 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000209
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100210 HBasicBlock* GetSuccessor() const {
211 return successor_;
212 }
213
Alexandre Rames9931f312015-06-19 14:47:01 +0100214 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathX86"; }
215
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000216 private:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100217 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000218 Label return_label_;
219
220 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
221};
222
Vladimir Markoaad75c62016-10-03 08:46:48 +0000223class LoadStringSlowPathX86 : public SlowPathCode {
224 public:
225 explicit LoadStringSlowPathX86(HLoadString* instruction): SlowPathCode(instruction) {}
226
227 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
228 LocationSummary* locations = instruction_->GetLocations();
229 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
230
231 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
232 __ Bind(GetEntryLabel());
233 SaveLiveRegisters(codegen, locations);
234
235 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000236 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
237 __ movl(calling_convention.GetRegisterAt(0), Immediate(string_index.index_));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000238 x86_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
239 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
240 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
241 RestoreLiveRegisters(codegen, locations);
242
243 // Store the resolved String to the BSS entry.
244 Register method_address = locations->InAt(0).AsRegister<Register>();
245 __ movl(Address(method_address, CodeGeneratorX86::kDummy32BitOffset),
246 locations->Out().AsRegister<Register>());
247 Label* fixup_label = x86_codegen->NewStringBssEntryPatch(instruction_->AsLoadString());
248 __ Bind(fixup_label);
249
250 __ jmp(GetExitLabel());
251 }
252
253 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathX86"; }
254
255 private:
256 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86);
257};
258
Andreas Gampe85b62f22015-09-09 13:15:38 -0700259class LoadClassSlowPathX86 : public SlowPathCode {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000260 public:
261 LoadClassSlowPathX86(HLoadClass* cls,
262 HInstruction* at,
263 uint32_t dex_pc,
264 bool do_clinit)
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000265 : SlowPathCode(at), cls_(cls), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000266 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
267 }
268
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000269 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000270 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000271 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
272 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000273 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000274
275 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000276 dex::TypeIndex type_index = cls_->GetTypeIndex();
277 __ movl(calling_convention.GetRegisterAt(0), Immediate(type_index.index_));
Serban Constantinescuba45db02016-07-12 22:53:02 +0100278 x86_codegen->InvokeRuntime(do_clinit_ ? kQuickInitializeStaticStorage
279 : kQuickInitializeType,
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000280 instruction_,
281 dex_pc_,
282 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000283 if (do_clinit_) {
284 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
285 } else {
286 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
287 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000288
289 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000290 Location out = locations->Out();
291 if (out.IsValid()) {
292 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
293 x86_codegen->Move32(out, Location::RegisterLocation(EAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000294 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000295 RestoreLiveRegisters(codegen, locations);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000296 // For HLoadClass/kBssEntry, store the resolved Class to the BSS entry.
297 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
298 if (cls_ == instruction_ && cls_->GetLoadKind() == HLoadClass::LoadKind::kBssEntry) {
299 DCHECK(out.IsValid());
300 Register method_address = locations->InAt(0).AsRegister<Register>();
301 __ movl(Address(method_address, CodeGeneratorX86::kDummy32BitOffset),
302 locations->Out().AsRegister<Register>());
303 Label* fixup_label = x86_codegen->NewTypeBssEntryPatch(cls_);
304 __ Bind(fixup_label);
305 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000306 __ jmp(GetExitLabel());
307 }
308
Alexandre Rames9931f312015-06-19 14:47:01 +0100309 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathX86"; }
310
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000311 private:
312 // The class this slow path will load.
313 HLoadClass* const cls_;
314
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000315 // The dex PC of `at_`.
316 const uint32_t dex_pc_;
317
318 // Whether to initialize the class.
319 const bool do_clinit_;
320
321 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
322};
323
Andreas Gampe85b62f22015-09-09 13:15:38 -0700324class TypeCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000325 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000326 TypeCheckSlowPathX86(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000327 : SlowPathCode(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000328
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000329 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000330 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000331 DCHECK(instruction_->IsCheckCast()
332 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000333
334 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
335 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000336
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000337 if (!is_fatal_) {
338 SaveLiveRegisters(codegen, locations);
339 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000340
341 // We're moving two locations to locations that could overlap, so we need a parallel
342 // move resolver.
343 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800344 x86_codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800345 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100346 DataType::Type::kReference,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800347 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800348 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100349 DataType::Type::kReference);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000350 if (instruction_->IsInstanceOf()) {
Serban Constantinescuba45db02016-07-12 22:53:02 +0100351 x86_codegen->InvokeRuntime(kQuickInstanceofNonTrivial,
Alexandre Rames8158f282015-08-07 10:26:17 +0100352 instruction_,
353 instruction_->GetDexPc(),
354 this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800355 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000356 } else {
357 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800358 x86_codegen->InvokeRuntime(kQuickCheckInstanceOf,
359 instruction_,
360 instruction_->GetDexPc(),
361 this);
362 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000363 }
364
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000365 if (!is_fatal_) {
366 if (instruction_->IsInstanceOf()) {
367 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
368 }
369 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray75374372015-09-17 17:12:19 +0000370
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000371 __ jmp(GetExitLabel());
372 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000373 }
374
Alexandre Rames9931f312015-06-19 14:47:01 +0100375 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathX86"; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000376 bool IsFatal() const OVERRIDE { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100377
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000378 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000379 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000380
381 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
382};
383
Andreas Gampe85b62f22015-09-09 13:15:38 -0700384class DeoptimizationSlowPathX86 : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700385 public:
Aart Bik42249c32016-01-07 15:33:50 -0800386 explicit DeoptimizationSlowPathX86(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000387 : SlowPathCode(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700388
389 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +0100390 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700391 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100392 LocationSummary* locations = instruction_->GetLocations();
393 SaveLiveRegisters(codegen, locations);
394 InvokeRuntimeCallingConvention calling_convention;
395 x86_codegen->Load32BitValue(
396 calling_convention.GetRegisterAt(0),
397 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescuba45db02016-07-12 22:53:02 +0100398 x86_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100399 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700400 }
401
Alexandre Rames9931f312015-06-19 14:47:01 +0100402 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86"; }
403
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700404 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700405 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86);
406};
407
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100408class ArraySetSlowPathX86 : public SlowPathCode {
409 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000410 explicit ArraySetSlowPathX86(HInstruction* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100411
412 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
413 LocationSummary* locations = instruction_->GetLocations();
414 __ Bind(GetEntryLabel());
415 SaveLiveRegisters(codegen, locations);
416
417 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100418 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100419 parallel_move.AddMove(
420 locations->InAt(0),
421 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100422 DataType::Type::kReference,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100423 nullptr);
424 parallel_move.AddMove(
425 locations->InAt(1),
426 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100427 DataType::Type::kInt32,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100428 nullptr);
429 parallel_move.AddMove(
430 locations->InAt(2),
431 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100432 DataType::Type::kReference,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100433 nullptr);
434 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
435
436 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100437 x86_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000438 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100439 RestoreLiveRegisters(codegen, locations);
440 __ jmp(GetExitLabel());
441 }
442
443 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathX86"; }
444
445 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100446 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathX86);
447};
448
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100449// Slow path marking an object reference `ref` during a read
450// barrier. The field `obj.field` in the object `obj` holding this
451// reference does not get updated by this slow path after marking (see
452// ReadBarrierMarkAndUpdateFieldSlowPathX86 below for that).
453//
454// This means that after the execution of this slow path, `ref` will
455// always be up-to-date, but `obj.field` may not; i.e., after the
456// flip, `ref` will be a to-space reference, but `obj.field` will
457// probably still be a from-space reference (unless it gets updated by
458// another thread, or if another thread installed another object
459// reference (different from `ref`) in `obj.field`).
Roland Levillain7c1559a2015-12-15 10:55:36 +0000460class ReadBarrierMarkSlowPathX86 : public SlowPathCode {
461 public:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100462 ReadBarrierMarkSlowPathX86(HInstruction* instruction,
463 Location ref,
464 bool unpoison_ref_before_marking)
465 : SlowPathCode(instruction),
466 ref_(ref),
467 unpoison_ref_before_marking_(unpoison_ref_before_marking) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000468 DCHECK(kEmitCompilerReadBarrier);
469 }
470
471 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathX86"; }
472
473 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
474 LocationSummary* locations = instruction_->GetLocations();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100475 Register ref_reg = ref_.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +0000476 DCHECK(locations->CanCall());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100477 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
Roland Levillain7c1559a2015-12-15 10:55:36 +0000478 DCHECK(instruction_->IsInstanceFieldGet() ||
479 instruction_->IsStaticFieldGet() ||
480 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100481 instruction_->IsArraySet() ||
Roland Levillain7c1559a2015-12-15 10:55:36 +0000482 instruction_->IsLoadClass() ||
483 instruction_->IsLoadString() ||
484 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100485 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100486 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
487 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain7c1559a2015-12-15 10:55:36 +0000488 << "Unexpected instruction in read barrier marking slow path: "
489 << instruction_->DebugName();
490
491 __ Bind(GetEntryLabel());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100492 if (unpoison_ref_before_marking_) {
Vladimir Marko953437b2016-08-24 08:30:46 +0000493 // Object* ref = ref_addr->AsMirrorPtr()
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100494 __ MaybeUnpoisonHeapReference(ref_reg);
Vladimir Marko953437b2016-08-24 08:30:46 +0000495 }
Roland Levillain4359e612016-07-20 11:32:19 +0100496 // No need to save live registers; it's taken care of by the
497 // entrypoint. Also, there is no need to update the stack mask,
498 // as this runtime call will not trigger a garbage collection.
Roland Levillain7c1559a2015-12-15 10:55:36 +0000499 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100500 DCHECK_NE(ref_reg, ESP);
501 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCpuRegisters) << ref_reg;
Roland Levillain02b75802016-07-13 11:54:35 +0100502 // "Compact" slow path, saving two moves.
503 //
504 // Instead of using the standard runtime calling convention (input
505 // and output in EAX):
506 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100507 // EAX <- ref
Roland Levillain02b75802016-07-13 11:54:35 +0100508 // EAX <- ReadBarrierMark(EAX)
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100509 // ref <- EAX
Roland Levillain02b75802016-07-13 11:54:35 +0100510 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100511 // we just use rX (the register containing `ref`) as input and output
Roland Levillain02b75802016-07-13 11:54:35 +0100512 // of a dedicated entrypoint:
513 //
514 // rX <- ReadBarrierMarkRegX(rX)
515 //
Roland Levillain97c46462017-05-11 14:04:03 +0100516 int32_t entry_point_offset = Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(ref_reg);
Roland Levillaindec8f632016-07-22 17:10:06 +0100517 // This runtime call does not require a stack map.
518 x86_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillain7c1559a2015-12-15 10:55:36 +0000519 __ jmp(GetExitLabel());
520 }
521
522 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100523 // The location (register) of the marked object reference.
524 const Location ref_;
525 // Should the reference in `ref_` be unpoisoned prior to marking it?
526 const bool unpoison_ref_before_marking_;
Roland Levillain7c1559a2015-12-15 10:55:36 +0000527
528 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86);
529};
530
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100531// Slow path marking an object reference `ref` during a read barrier,
532// and if needed, atomically updating the field `obj.field` in the
533// object `obj` holding this reference after marking (contrary to
534// ReadBarrierMarkSlowPathX86 above, which never tries to update
535// `obj.field`).
536//
537// This means that after the execution of this slow path, both `ref`
538// and `obj.field` will be up-to-date; i.e., after the flip, both will
539// hold the same to-space reference (unless another thread installed
540// another object reference (different from `ref`) in `obj.field`).
541class ReadBarrierMarkAndUpdateFieldSlowPathX86 : public SlowPathCode {
542 public:
543 ReadBarrierMarkAndUpdateFieldSlowPathX86(HInstruction* instruction,
544 Location ref,
545 Register obj,
546 const Address& field_addr,
547 bool unpoison_ref_before_marking,
548 Register temp)
549 : SlowPathCode(instruction),
550 ref_(ref),
551 obj_(obj),
552 field_addr_(field_addr),
553 unpoison_ref_before_marking_(unpoison_ref_before_marking),
554 temp_(temp) {
555 DCHECK(kEmitCompilerReadBarrier);
556 }
557
558 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkAndUpdateFieldSlowPathX86"; }
559
560 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
561 LocationSummary* locations = instruction_->GetLocations();
562 Register ref_reg = ref_.AsRegister<Register>();
563 DCHECK(locations->CanCall());
564 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
565 // This slow path is only used by the UnsafeCASObject intrinsic.
566 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
567 << "Unexpected instruction in read barrier marking and field updating slow path: "
568 << instruction_->DebugName();
569 DCHECK(instruction_->GetLocations()->Intrinsified());
570 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
571
572 __ Bind(GetEntryLabel());
573 if (unpoison_ref_before_marking_) {
574 // Object* ref = ref_addr->AsMirrorPtr()
575 __ MaybeUnpoisonHeapReference(ref_reg);
576 }
577
578 // Save the old (unpoisoned) reference.
579 __ movl(temp_, ref_reg);
580
581 // No need to save live registers; it's taken care of by the
582 // entrypoint. Also, there is no need to update the stack mask,
583 // as this runtime call will not trigger a garbage collection.
584 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
585 DCHECK_NE(ref_reg, ESP);
586 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCpuRegisters) << ref_reg;
587 // "Compact" slow path, saving two moves.
588 //
589 // Instead of using the standard runtime calling convention (input
590 // and output in EAX):
591 //
592 // EAX <- ref
593 // EAX <- ReadBarrierMark(EAX)
594 // ref <- EAX
595 //
596 // we just use rX (the register containing `ref`) as input and output
597 // of a dedicated entrypoint:
598 //
599 // rX <- ReadBarrierMarkRegX(rX)
600 //
Roland Levillain97c46462017-05-11 14:04:03 +0100601 int32_t entry_point_offset = Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(ref_reg);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100602 // This runtime call does not require a stack map.
603 x86_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
604
605 // If the new reference is different from the old reference,
606 // update the field in the holder (`*field_addr`).
607 //
608 // Note that this field could also hold a different object, if
609 // another thread had concurrently changed it. In that case, the
610 // LOCK CMPXCHGL instruction in the compare-and-set (CAS)
611 // operation below would abort the CAS, leaving the field as-is.
612 NearLabel done;
613 __ cmpl(temp_, ref_reg);
614 __ j(kEqual, &done);
615
616 // Update the the holder's field atomically. This may fail if
617 // mutator updates before us, but it's OK. This is achieved
618 // using a strong compare-and-set (CAS) operation with relaxed
619 // memory synchronization ordering, where the expected value is
620 // the old reference and the desired value is the new reference.
621 // This operation is implemented with a 32-bit LOCK CMPXLCHG
622 // instruction, which requires the expected value (the old
623 // reference) to be in EAX. Save EAX beforehand, and move the
624 // expected value (stored in `temp_`) into EAX.
625 __ pushl(EAX);
626 __ movl(EAX, temp_);
627
628 // Convenience aliases.
629 Register base = obj_;
630 Register expected = EAX;
631 Register value = ref_reg;
632
633 bool base_equals_value = (base == value);
634 if (kPoisonHeapReferences) {
635 if (base_equals_value) {
636 // If `base` and `value` are the same register location, move
637 // `value` to a temporary register. This way, poisoning
638 // `value` won't invalidate `base`.
639 value = temp_;
640 __ movl(value, base);
641 }
642
643 // Check that the register allocator did not assign the location
644 // of `expected` (EAX) to `value` nor to `base`, so that heap
645 // poisoning (when enabled) works as intended below.
646 // - If `value` were equal to `expected`, both references would
647 // be poisoned twice, meaning they would not be poisoned at
648 // all, as heap poisoning uses address negation.
649 // - If `base` were equal to `expected`, poisoning `expected`
650 // would invalidate `base`.
651 DCHECK_NE(value, expected);
652 DCHECK_NE(base, expected);
653
654 __ PoisonHeapReference(expected);
655 __ PoisonHeapReference(value);
656 }
657
658 __ LockCmpxchgl(field_addr_, value);
659
660 // If heap poisoning is enabled, we need to unpoison the values
661 // that were poisoned earlier.
662 if (kPoisonHeapReferences) {
663 if (base_equals_value) {
664 // `value` has been moved to a temporary register, no need
665 // to unpoison it.
666 } else {
667 __ UnpoisonHeapReference(value);
668 }
669 // No need to unpoison `expected` (EAX), as it is be overwritten below.
670 }
671
672 // Restore EAX.
673 __ popl(EAX);
674
675 __ Bind(&done);
676 __ jmp(GetExitLabel());
677 }
678
679 private:
680 // The location (register) of the marked object reference.
681 const Location ref_;
682 // The register containing the object holding the marked object reference field.
683 const Register obj_;
684 // The address of the marked reference field. The base of this address must be `obj_`.
685 const Address field_addr_;
686
687 // Should the reference in `ref_` be unpoisoned prior to marking it?
688 const bool unpoison_ref_before_marking_;
689
690 const Register temp_;
691
692 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathX86);
693};
694
Roland Levillain0d5a2812015-11-13 10:07:31 +0000695// Slow path generating a read barrier for a heap reference.
696class ReadBarrierForHeapReferenceSlowPathX86 : public SlowPathCode {
697 public:
698 ReadBarrierForHeapReferenceSlowPathX86(HInstruction* instruction,
699 Location out,
700 Location ref,
701 Location obj,
702 uint32_t offset,
703 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000704 : SlowPathCode(instruction),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000705 out_(out),
706 ref_(ref),
707 obj_(obj),
708 offset_(offset),
709 index_(index) {
710 DCHECK(kEmitCompilerReadBarrier);
711 // If `obj` is equal to `out` or `ref`, it means the initial object
712 // has been overwritten by (or after) the heap object reference load
713 // to be instrumented, e.g.:
714 //
715 // __ movl(out, Address(out, offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000716 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000717 //
718 // In that case, we have lost the information about the original
719 // object, and the emitted read barrier cannot work properly.
720 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
721 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
722 }
723
724 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
725 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
726 LocationSummary* locations = instruction_->GetLocations();
727 Register reg_out = out_.AsRegister<Register>();
728 DCHECK(locations->CanCall());
729 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain3d312422016-06-23 13:53:42 +0100730 DCHECK(instruction_->IsInstanceFieldGet() ||
731 instruction_->IsStaticFieldGet() ||
732 instruction_->IsArrayGet() ||
733 instruction_->IsInstanceOf() ||
734 instruction_->IsCheckCast() ||
Andreas Gamped9911ee2017-03-27 13:27:24 -0700735 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain7c1559a2015-12-15 10:55:36 +0000736 << "Unexpected instruction in read barrier for heap reference slow path: "
737 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000738
739 __ Bind(GetEntryLabel());
740 SaveLiveRegisters(codegen, locations);
741
742 // We may have to change the index's value, but as `index_` is a
743 // constant member (like other "inputs" of this slow path),
744 // introduce a copy of it, `index`.
745 Location index = index_;
746 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100747 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain0d5a2812015-11-13 10:07:31 +0000748 if (instruction_->IsArrayGet()) {
749 // Compute the actual memory offset and store it in `index`.
750 Register index_reg = index_.AsRegister<Register>();
751 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
752 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
753 // We are about to change the value of `index_reg` (see the
754 // calls to art::x86::X86Assembler::shll and
755 // art::x86::X86Assembler::AddImmediate below), but it has
756 // not been saved by the previous call to
757 // art::SlowPathCode::SaveLiveRegisters, as it is a
758 // callee-save register --
759 // art::SlowPathCode::SaveLiveRegisters does not consider
760 // callee-save registers, as it has been designed with the
761 // assumption that callee-save registers are supposed to be
762 // handled by the called function. So, as a callee-save
763 // register, `index_reg` _would_ eventually be saved onto
764 // the stack, but it would be too late: we would have
765 // changed its value earlier. Therefore, we manually save
766 // it here into another freely available register,
767 // `free_reg`, chosen of course among the caller-save
768 // registers (as a callee-save `free_reg` register would
769 // exhibit the same problem).
770 //
771 // Note we could have requested a temporary register from
772 // the register allocator instead; but we prefer not to, as
773 // this is a slow path, and we know we can find a
774 // caller-save register that is available.
775 Register free_reg = FindAvailableCallerSaveRegister(codegen);
776 __ movl(free_reg, index_reg);
777 index_reg = free_reg;
778 index = Location::RegisterLocation(index_reg);
779 } else {
780 // The initial register stored in `index_` has already been
781 // saved in the call to art::SlowPathCode::SaveLiveRegisters
782 // (as it is not a callee-save register), so we can freely
783 // use it.
784 }
785 // Shifting the index value contained in `index_reg` by the scale
786 // factor (2) cannot overflow in practice, as the runtime is
787 // unable to allocate object arrays with a size larger than
788 // 2^26 - 1 (that is, 2^28 - 4 bytes).
789 __ shll(index_reg, Immediate(TIMES_4));
790 static_assert(
791 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
792 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
793 __ AddImmediate(index_reg, Immediate(offset_));
794 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100795 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
796 // intrinsics, `index_` is not shifted by a scale factor of 2
797 // (as in the case of ArrayGet), as it is actually an offset
798 // to an object field within an object.
799 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000800 DCHECK(instruction_->GetLocations()->Intrinsified());
801 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
802 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
803 << instruction_->AsInvoke()->GetIntrinsic();
804 DCHECK_EQ(offset_, 0U);
805 DCHECK(index_.IsRegisterPair());
806 // UnsafeGet's offset location is a register pair, the low
807 // part contains the correct offset.
808 index = index_.ToLow();
809 }
810 }
811
812 // We're moving two or three locations to locations that could
813 // overlap, so we need a parallel move resolver.
814 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100815 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Roland Levillain0d5a2812015-11-13 10:07:31 +0000816 parallel_move.AddMove(ref_,
817 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100818 DataType::Type::kReference,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000819 nullptr);
820 parallel_move.AddMove(obj_,
821 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100822 DataType::Type::kReference,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000823 nullptr);
824 if (index.IsValid()) {
825 parallel_move.AddMove(index,
826 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100827 DataType::Type::kInt32,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000828 nullptr);
829 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
830 } else {
831 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
832 __ movl(calling_convention.GetRegisterAt(2), Immediate(offset_));
833 }
Serban Constantinescuba45db02016-07-12 22:53:02 +0100834 x86_codegen->InvokeRuntime(kQuickReadBarrierSlow, instruction_, instruction_->GetDexPc(), this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000835 CheckEntrypointTypes<
836 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
837 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
838
839 RestoreLiveRegisters(codegen, locations);
840 __ jmp(GetExitLabel());
841 }
842
843 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathX86"; }
844
845 private:
846 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
847 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
848 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
849 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
850 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
851 return static_cast<Register>(i);
852 }
853 }
854 // We shall never fail to find a free caller-save register, as
855 // there are more than two core caller-save registers on x86
856 // (meaning it is possible to find one which is different from
857 // `ref` and `obj`).
858 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
859 LOG(FATAL) << "Could not find a free caller-save register";
860 UNREACHABLE();
861 }
862
Roland Levillain0d5a2812015-11-13 10:07:31 +0000863 const Location out_;
864 const Location ref_;
865 const Location obj_;
866 const uint32_t offset_;
867 // An additional location containing an index to an array.
868 // Only used for HArrayGet and the UnsafeGetObject &
869 // UnsafeGetObjectVolatile intrinsics.
870 const Location index_;
871
872 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86);
873};
874
875// Slow path generating a read barrier for a GC root.
876class ReadBarrierForRootSlowPathX86 : public SlowPathCode {
877 public:
878 ReadBarrierForRootSlowPathX86(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000879 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000880 DCHECK(kEmitCompilerReadBarrier);
881 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000882
883 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
884 LocationSummary* locations = instruction_->GetLocations();
885 Register reg_out = out_.AsRegister<Register>();
886 DCHECK(locations->CanCall());
887 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000888 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
889 << "Unexpected instruction in read barrier for GC root slow path: "
890 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000891
892 __ Bind(GetEntryLabel());
893 SaveLiveRegisters(codegen, locations);
894
895 InvokeRuntimeCallingConvention calling_convention;
896 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
897 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100898 x86_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000899 instruction_,
900 instruction_->GetDexPc(),
901 this);
902 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
903 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
904
905 RestoreLiveRegisters(codegen, locations);
906 __ jmp(GetExitLabel());
907 }
908
909 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathX86"; }
910
911 private:
Roland Levillain0d5a2812015-11-13 10:07:31 +0000912 const Location out_;
913 const Location root_;
914
915 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86);
916};
917
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100918#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100919// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
920#define __ down_cast<X86Assembler*>(GetAssembler())-> // NOLINT
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100921
Aart Bike9f37602015-10-09 11:15:55 -0700922inline Condition X86Condition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700923 switch (cond) {
924 case kCondEQ: return kEqual;
925 case kCondNE: return kNotEqual;
926 case kCondLT: return kLess;
927 case kCondLE: return kLessEqual;
928 case kCondGT: return kGreater;
929 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700930 case kCondB: return kBelow;
931 case kCondBE: return kBelowEqual;
932 case kCondA: return kAbove;
933 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700934 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100935 LOG(FATAL) << "Unreachable";
936 UNREACHABLE();
937}
938
Aart Bike9f37602015-10-09 11:15:55 -0700939// Maps signed condition to unsigned condition and FP condition to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100940inline Condition X86UnsignedOrFPCondition(IfCondition cond) {
941 switch (cond) {
942 case kCondEQ: return kEqual;
943 case kCondNE: return kNotEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700944 // Signed to unsigned, and FP to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100945 case kCondLT: return kBelow;
946 case kCondLE: return kBelowEqual;
947 case kCondGT: return kAbove;
948 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700949 // Unsigned remain unchanged.
950 case kCondB: return kBelow;
951 case kCondBE: return kBelowEqual;
952 case kCondA: return kAbove;
953 case kCondAE: return kAboveEqual;
Roland Levillain4fa13f62015-07-06 18:11:54 +0100954 }
955 LOG(FATAL) << "Unreachable";
956 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700957}
958
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100959void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100960 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100961}
962
963void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100964 stream << XmmRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100965}
966
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100967size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
968 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
969 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100970}
971
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100972size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
973 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
974 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100975}
976
Mark Mendell7c8d0092015-01-26 11:21:33 -0500977size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Aart Bikb13c65b2017-03-21 20:14:07 -0700978 if (GetGraph()->HasSIMD()) {
Aart Bik5576f372017-03-23 16:17:37 -0700979 __ movups(Address(ESP, stack_index), XmmRegister(reg_id));
Aart Bikb13c65b2017-03-21 20:14:07 -0700980 } else {
981 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
982 }
Mark Mendell7c8d0092015-01-26 11:21:33 -0500983 return GetFloatingPointSpillSlotSize();
984}
985
986size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Aart Bikb13c65b2017-03-21 20:14:07 -0700987 if (GetGraph()->HasSIMD()) {
Aart Bik5576f372017-03-23 16:17:37 -0700988 __ movups(XmmRegister(reg_id), Address(ESP, stack_index));
Aart Bikb13c65b2017-03-21 20:14:07 -0700989 } else {
990 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
991 }
Mark Mendell7c8d0092015-01-26 11:21:33 -0500992 return GetFloatingPointSpillSlotSize();
993}
994
Calin Juravle175dc732015-08-25 15:42:32 +0100995void CodeGeneratorX86::InvokeRuntime(QuickEntrypointEnum entrypoint,
996 HInstruction* instruction,
997 uint32_t dex_pc,
998 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +0100999 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +01001000 GenerateInvokeRuntime(GetThreadOffset<kX86PointerSize>(entrypoint).Int32Value());
1001 if (EntrypointRequiresStackMap(entrypoint)) {
1002 RecordPcInfo(instruction, dex_pc, slow_path);
1003 }
Alexandre Rames8158f282015-08-07 10:26:17 +01001004}
1005
Roland Levillaindec8f632016-07-22 17:10:06 +01001006void CodeGeneratorX86::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1007 HInstruction* instruction,
1008 SlowPathCode* slow_path) {
1009 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +01001010 GenerateInvokeRuntime(entry_point_offset);
1011}
1012
1013void CodeGeneratorX86::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001014 __ fs()->call(Address::Absolute(entry_point_offset));
1015}
1016
Mark Mendellfb8d2792015-03-31 22:16:59 -04001017CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +00001018 const X86InstructionSetFeatures& isa_features,
1019 const CompilerOptions& compiler_options,
1020 OptimizingCompilerStats* stats)
Mark Mendell5f874182015-03-04 15:42:45 -05001021 : CodeGenerator(graph,
1022 kNumberOfCpuRegisters,
1023 kNumberOfXmmRegisters,
1024 kNumberOfRegisterPairs,
1025 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1026 arraysize(kCoreCalleeSaves))
1027 | (1 << kFakeReturnRegister),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001028 0,
1029 compiler_options,
1030 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +01001031 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001032 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001033 instruction_visitor_(graph, this),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001034 move_resolver_(graph->GetAllocator(), this),
1035 assembler_(graph->GetAllocator()),
Vladimir Marko58155012015-08-19 12:49:41 +00001036 isa_features_(isa_features),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001037 boot_image_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1038 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1039 boot_image_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1040 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1041 string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1042 string_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1043 jit_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1044 jit_class_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko93205e32016-04-13 11:59:46 +01001045 constant_area_start_(-1),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001046 fixups_to_jump_tables_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001047 method_address_offset_(std::less<uint32_t>(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001048 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001049 // Use a fake return address register to mimic Quick.
1050 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001051}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001052
David Brazdil58282f42016-01-14 12:45:10 +00001053void CodeGeneratorX86::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001054 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001055 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001056}
1057
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001058InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001059 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001060 assembler_(codegen->GetAssembler()),
1061 codegen_(codegen) {}
1062
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001063static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001064 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001065}
1066
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001067void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001068 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001069 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +00001070 bool skip_overflow_check =
1071 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001072 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +00001073
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001074 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +01001075 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001076 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +01001077 }
1078
Mark Mendell5f874182015-03-04 15:42:45 -05001079 if (HasEmptyFrame()) {
1080 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001081 }
Mark Mendell5f874182015-03-04 15:42:45 -05001082
1083 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
1084 Register reg = kCoreCalleeSaves[i];
1085 if (allocated_registers_.ContainsCoreRegister(reg)) {
1086 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001087 __ cfi().AdjustCFAOffset(kX86WordSize);
1088 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -05001089 }
1090 }
1091
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001092 int adjust = GetFrameSize() - FrameEntrySpillSize();
1093 __ subl(ESP, Immediate(adjust));
1094 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001095 // Save the current method if we need it. Note that we do not
1096 // do this in HCurrentMethod, as the instruction might have been removed
1097 // in the SSA graph.
1098 if (RequiresCurrentMethod()) {
1099 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
1100 }
Nicolas Geoffrayf7893532017-06-15 12:34:36 +01001101
1102 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1103 // Initialize should_deoptimize flag to 0.
1104 __ movl(Address(ESP, GetStackOffsetOfShouldDeoptimizeFlag()), Immediate(0));
1105 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001106}
1107
1108void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001109 __ cfi().RememberState();
1110 if (!HasEmptyFrame()) {
1111 int adjust = GetFrameSize() - FrameEntrySpillSize();
1112 __ addl(ESP, Immediate(adjust));
1113 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -05001114
David Srbeckyc34dc932015-04-12 09:27:43 +01001115 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1116 Register reg = kCoreCalleeSaves[i];
1117 if (allocated_registers_.ContainsCoreRegister(reg)) {
1118 __ popl(reg);
1119 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
1120 __ cfi().Restore(DWARFReg(reg));
1121 }
Mark Mendell5f874182015-03-04 15:42:45 -05001122 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001123 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001124 __ ret();
1125 __ cfi().RestoreState();
1126 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001127}
1128
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001129void CodeGeneratorX86::Bind(HBasicBlock* block) {
1130 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001131}
1132
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001133Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(DataType::Type type) const {
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001134 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001135 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001136 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001137 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001138 case DataType::Type::kInt8:
1139 case DataType::Type::kUint16:
1140 case DataType::Type::kInt16:
1141 case DataType::Type::kInt32:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001142 return Location::RegisterLocation(EAX);
1143
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001144 case DataType::Type::kInt64:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001145 return Location::RegisterPairLocation(EAX, EDX);
1146
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001147 case DataType::Type::kVoid:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001148 return Location::NoLocation();
1149
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001150 case DataType::Type::kFloat64:
1151 case DataType::Type::kFloat32:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001152 return Location::FpuRegisterLocation(XMM0);
1153 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001154
1155 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001156}
1157
1158Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
1159 return Location::RegisterLocation(kMethodRegisterArgument);
1160}
1161
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001162Location InvokeDexCallingConventionVisitorX86::GetNextLocation(DataType::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001163 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001164 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001165 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001166 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001167 case DataType::Type::kInt8:
1168 case DataType::Type::kUint16:
1169 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001170 case DataType::Type::kInt32: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001171 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001172 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001173 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001174 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001175 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001176 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001177 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001178 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001179
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001180 case DataType::Type::kInt64: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001181 uint32_t index = gp_index_;
1182 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001183 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001184 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001185 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
1186 calling_convention.GetRegisterPairAt(index));
1187 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001188 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001189 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1190 }
1191 }
1192
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001193 case DataType::Type::kFloat32: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001194 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001195 stack_index_++;
1196 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1197 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1198 } else {
1199 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1200 }
1201 }
1202
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001203 case DataType::Type::kFloat64: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001204 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001205 stack_index_ += 2;
1206 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1207 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1208 } else {
1209 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001210 }
1211 }
1212
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001213 case DataType::Type::kVoid:
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001214 LOG(FATAL) << "Unexpected parameter type " << type;
1215 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001216 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00001217 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001218}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001219
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001220void CodeGeneratorX86::Move32(Location destination, Location source) {
1221 if (source.Equals(destination)) {
1222 return;
1223 }
1224 if (destination.IsRegister()) {
1225 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001226 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001227 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001228 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001229 } else {
1230 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001231 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001232 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001233 } else if (destination.IsFpuRegister()) {
1234 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001235 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001236 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001237 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001238 } else {
1239 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001240 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001241 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001242 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001243 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001244 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001245 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001246 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001247 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05001248 } else if (source.IsConstant()) {
1249 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001250 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05001251 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001252 } else {
1253 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001254 __ pushl(Address(ESP, source.GetStackIndex()));
1255 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001256 }
1257 }
1258}
1259
1260void CodeGeneratorX86::Move64(Location destination, Location source) {
1261 if (source.Equals(destination)) {
1262 return;
1263 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001264 if (destination.IsRegisterPair()) {
1265 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001266 EmitParallelMoves(
1267 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1268 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001269 DataType::Type::kInt32,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001270 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001271 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001272 DataType::Type::kInt32);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001273 } else if (source.IsFpuRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001274 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
1275 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
1276 __ psrlq(src_reg, Immediate(32));
1277 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001278 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001279 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001280 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001281 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
1282 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001283 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
1284 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001285 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05001286 if (source.IsFpuRegister()) {
1287 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
1288 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001289 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001290 } else if (source.IsRegisterPair()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001291 size_t elem_size = DataType::Size(DataType::Type::kInt32);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001292 // Create stack space for 2 elements.
1293 __ subl(ESP, Immediate(2 * elem_size));
1294 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
1295 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
1296 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
1297 // And remove the temporary stack space we allocated.
1298 __ addl(ESP, Immediate(2 * elem_size));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001299 } else {
1300 LOG(FATAL) << "Unimplemented";
1301 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001302 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001303 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001304 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001305 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001306 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001307 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001308 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001309 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001310 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001311 } else if (source.IsConstant()) {
1312 HConstant* constant = source.GetConstant();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001313 DCHECK(constant->IsLongConstant() || constant->IsDoubleConstant());
1314 int64_t value = GetInt64ValueOf(constant);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001315 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001316 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
1317 Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001318 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001319 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001320 EmitParallelMoves(
1321 Location::StackSlot(source.GetStackIndex()),
1322 Location::StackSlot(destination.GetStackIndex()),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001323 DataType::Type::kInt32,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001324 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001325 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001326 DataType::Type::kInt32);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001327 }
1328 }
1329}
1330
Calin Juravle175dc732015-08-25 15:42:32 +01001331void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
1332 DCHECK(location.IsRegister());
1333 __ movl(location.AsRegister<Register>(), Immediate(value));
1334}
1335
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001336void CodeGeneratorX86::MoveLocation(Location dst, Location src, DataType::Type dst_type) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001337 HParallelMove move(GetGraph()->GetAllocator());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001338 if (dst_type == DataType::Type::kInt64 && !src.IsConstant() && !src.IsFpuRegister()) {
1339 move.AddMove(src.ToLow(), dst.ToLow(), DataType::Type::kInt32, nullptr);
1340 move.AddMove(src.ToHigh(), dst.ToHigh(), DataType::Type::kInt32, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001341 } else {
David Brazdil74eb1b22015-12-14 11:44:01 +00001342 move.AddMove(src, dst, dst_type, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001343 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001344 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001345}
1346
1347void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1348 if (location.IsRegister()) {
1349 locations->AddTemp(location);
1350 } else if (location.IsRegisterPair()) {
1351 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1352 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1353 } else {
1354 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1355 }
1356}
1357
David Brazdilfc6a86a2015-06-26 10:33:45 +00001358void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001359 DCHECK(!successor->IsExitBlock());
1360
1361 HBasicBlock* block = got->GetBlock();
1362 HInstruction* previous = got->GetPrevious();
1363
1364 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001365 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001366 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1367 return;
1368 }
1369
1370 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1371 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1372 }
1373 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001374 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001375 }
1376}
1377
David Brazdilfc6a86a2015-06-26 10:33:45 +00001378void LocationsBuilderX86::VisitGoto(HGoto* got) {
1379 got->SetLocations(nullptr);
1380}
1381
1382void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1383 HandleGoto(got, got->GetSuccessor());
1384}
1385
1386void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1387 try_boundary->SetLocations(nullptr);
1388}
1389
1390void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1391 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1392 if (!successor->IsExitBlock()) {
1393 HandleGoto(try_boundary, successor);
1394 }
1395}
1396
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001397void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001398 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001399}
1400
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001401void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001402}
1403
Mark Mendell152408f2015-12-31 12:28:50 -05001404template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001405void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001406 LabelType* true_label,
1407 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001408 if (cond->IsFPConditionTrueIfNaN()) {
1409 __ j(kUnordered, true_label);
1410 } else if (cond->IsFPConditionFalseIfNaN()) {
1411 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001412 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001413 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001414}
1415
Mark Mendell152408f2015-12-31 12:28:50 -05001416template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001417void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001418 LabelType* true_label,
1419 LabelType* false_label) {
Mark Mendellc4701932015-04-10 13:18:51 -04001420 LocationSummary* locations = cond->GetLocations();
1421 Location left = locations->InAt(0);
1422 Location right = locations->InAt(1);
1423 IfCondition if_cond = cond->GetCondition();
1424
Mark Mendellc4701932015-04-10 13:18:51 -04001425 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001426 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001427 IfCondition true_high_cond = if_cond;
1428 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001429 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001430
1431 // Set the conditions for the test, remembering that == needs to be
1432 // decided using the low words.
1433 switch (if_cond) {
1434 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001435 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001436 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001437 break;
1438 case kCondLT:
1439 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001440 break;
1441 case kCondLE:
1442 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001443 break;
1444 case kCondGT:
1445 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001446 break;
1447 case kCondGE:
1448 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001449 break;
Aart Bike9f37602015-10-09 11:15:55 -07001450 case kCondB:
1451 false_high_cond = kCondA;
1452 break;
1453 case kCondBE:
1454 true_high_cond = kCondB;
1455 break;
1456 case kCondA:
1457 false_high_cond = kCondB;
1458 break;
1459 case kCondAE:
1460 true_high_cond = kCondA;
1461 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001462 }
1463
1464 if (right.IsConstant()) {
1465 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001466 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001467 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001468
Aart Bika19616e2016-02-01 18:57:58 -08001469 codegen_->Compare32BitValue(left_high, val_high);
Mark Mendellc4701932015-04-10 13:18:51 -04001470 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001471 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001472 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001473 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001474 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001475 __ j(X86Condition(true_high_cond), true_label);
1476 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001477 }
1478 // Must be equal high, so compare the lows.
Aart Bika19616e2016-02-01 18:57:58 -08001479 codegen_->Compare32BitValue(left_low, val_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001480 } else if (right.IsRegisterPair()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001481 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001482 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001483
1484 __ cmpl(left_high, right_high);
1485 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001486 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001487 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001488 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001489 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001490 __ j(X86Condition(true_high_cond), true_label);
1491 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001492 }
1493 // Must be equal high, so compare the lows.
1494 __ cmpl(left_low, right_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001495 } else {
1496 DCHECK(right.IsDoubleStackSlot());
1497 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
1498 if (if_cond == kCondNE) {
1499 __ j(X86Condition(true_high_cond), true_label);
1500 } else if (if_cond == kCondEQ) {
1501 __ j(X86Condition(false_high_cond), false_label);
1502 } else {
1503 __ j(X86Condition(true_high_cond), true_label);
1504 __ j(X86Condition(false_high_cond), false_label);
1505 }
1506 // Must be equal high, so compare the lows.
1507 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Mark Mendellc4701932015-04-10 13:18:51 -04001508 }
1509 // The last comparison might be unsigned.
1510 __ j(final_condition, true_label);
1511}
1512
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001513void InstructionCodeGeneratorX86::GenerateFPCompare(Location lhs,
1514 Location rhs,
1515 HInstruction* insn,
1516 bool is_double) {
1517 HX86LoadFromConstantTable* const_area = insn->InputAt(1)->AsX86LoadFromConstantTable();
1518 if (is_double) {
1519 if (rhs.IsFpuRegister()) {
1520 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1521 } else if (const_area != nullptr) {
1522 DCHECK(const_area->IsEmittedAtUseSite());
1523 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(),
1524 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001525 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
1526 const_area->GetBaseMethodAddress(),
1527 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001528 } else {
1529 DCHECK(rhs.IsDoubleStackSlot());
1530 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1531 }
1532 } else {
1533 if (rhs.IsFpuRegister()) {
1534 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1535 } else if (const_area != nullptr) {
1536 DCHECK(const_area->IsEmittedAtUseSite());
1537 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(),
1538 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001539 const_area->GetConstant()->AsFloatConstant()->GetValue(),
1540 const_area->GetBaseMethodAddress(),
1541 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001542 } else {
1543 DCHECK(rhs.IsStackSlot());
1544 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1545 }
1546 }
1547}
1548
Mark Mendell152408f2015-12-31 12:28:50 -05001549template<class LabelType>
David Brazdil0debae72015-11-12 18:37:00 +00001550void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendell152408f2015-12-31 12:28:50 -05001551 LabelType* true_target_in,
1552 LabelType* false_target_in) {
David Brazdil0debae72015-11-12 18:37:00 +00001553 // Generated branching requires both targets to be explicit. If either of the
1554 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Mark Mendell152408f2015-12-31 12:28:50 -05001555 LabelType fallthrough_target;
1556 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1557 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
David Brazdil0debae72015-11-12 18:37:00 +00001558
Mark Mendellc4701932015-04-10 13:18:51 -04001559 LocationSummary* locations = condition->GetLocations();
1560 Location left = locations->InAt(0);
1561 Location right = locations->InAt(1);
1562
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001563 DataType::Type type = condition->InputAt(0)->GetType();
Mark Mendellc4701932015-04-10 13:18:51 -04001564 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001565 case DataType::Type::kInt64:
Mark Mendellc4701932015-04-10 13:18:51 -04001566 GenerateLongComparesAndJumps(condition, true_target, false_target);
1567 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001568 case DataType::Type::kFloat32:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001569 GenerateFPCompare(left, right, condition, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001570 GenerateFPJumps(condition, true_target, false_target);
1571 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001572 case DataType::Type::kFloat64:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001573 GenerateFPCompare(left, right, condition, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001574 GenerateFPJumps(condition, true_target, false_target);
1575 break;
1576 default:
1577 LOG(FATAL) << "Unexpected compare type " << type;
1578 }
1579
David Brazdil0debae72015-11-12 18:37:00 +00001580 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001581 __ jmp(false_target);
1582 }
David Brazdil0debae72015-11-12 18:37:00 +00001583
1584 if (fallthrough_target.IsLinked()) {
1585 __ Bind(&fallthrough_target);
1586 }
Mark Mendellc4701932015-04-10 13:18:51 -04001587}
1588
David Brazdil0debae72015-11-12 18:37:00 +00001589static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1590 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1591 // are set only strictly before `branch`. We can't use the eflags on long/FP
1592 // conditions if they are materialized due to the complex branching.
1593 return cond->IsCondition() &&
1594 cond->GetNext() == branch &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001595 cond->InputAt(0)->GetType() != DataType::Type::kInt64 &&
1596 !DataType::IsFloatingPointType(cond->InputAt(0)->GetType());
David Brazdil0debae72015-11-12 18:37:00 +00001597}
1598
Mark Mendell152408f2015-12-31 12:28:50 -05001599template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001600void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001601 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001602 LabelType* true_target,
1603 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001604 HInstruction* cond = instruction->InputAt(condition_input_index);
1605
1606 if (true_target == nullptr && false_target == nullptr) {
1607 // Nothing to do. The code always falls through.
1608 return;
1609 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001610 // Constant condition, statically compared against "true" (integer value 1).
1611 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001612 if (true_target != nullptr) {
1613 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001614 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001615 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001616 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001617 if (false_target != nullptr) {
1618 __ jmp(false_target);
1619 }
1620 }
1621 return;
1622 }
1623
1624 // The following code generates these patterns:
1625 // (1) true_target == nullptr && false_target != nullptr
1626 // - opposite condition true => branch to false_target
1627 // (2) true_target != nullptr && false_target == nullptr
1628 // - condition true => branch to true_target
1629 // (3) true_target != nullptr && false_target != nullptr
1630 // - condition true => branch to true_target
1631 // - branch to false_target
1632 if (IsBooleanValueOrMaterializedCondition(cond)) {
1633 if (AreEflagsSetFrom(cond, instruction)) {
1634 if (true_target == nullptr) {
1635 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
1636 } else {
1637 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
1638 }
1639 } else {
1640 // Materialized condition, compare against 0.
1641 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1642 if (lhs.IsRegister()) {
1643 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1644 } else {
1645 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1646 }
1647 if (true_target == nullptr) {
1648 __ j(kEqual, false_target);
1649 } else {
1650 __ j(kNotEqual, true_target);
1651 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001652 }
1653 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001654 // Condition has not been materialized, use its inputs as the comparison and
1655 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001656 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001657
1658 // If this is a long or FP comparison that has been folded into
1659 // the HCondition, generate the comparison directly.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001660 DataType::Type type = condition->InputAt(0)->GetType();
1661 if (type == DataType::Type::kInt64 || DataType::IsFloatingPointType(type)) {
David Brazdil0debae72015-11-12 18:37:00 +00001662 GenerateCompareTestAndBranch(condition, true_target, false_target);
1663 return;
1664 }
1665
1666 Location lhs = condition->GetLocations()->InAt(0);
1667 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001668 // LHS is guaranteed to be in a register (see LocationsBuilderX86::HandleCondition).
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001669 codegen_->GenerateIntCompare(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00001670 if (true_target == nullptr) {
1671 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1672 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001673 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001674 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001675 }
David Brazdil0debae72015-11-12 18:37:00 +00001676
1677 // If neither branch falls through (case 3), the conditional branch to `true_target`
1678 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1679 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001680 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001681 }
1682}
1683
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001684void LocationsBuilderX86::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001685 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00001686 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001687 locations->SetInAt(0, Location::Any());
1688 }
1689}
1690
1691void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001692 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1693 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1694 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1695 nullptr : codegen_->GetLabelOf(true_successor);
1696 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1697 nullptr : codegen_->GetLabelOf(false_successor);
1698 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001699}
1700
1701void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001702 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001703 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01001704 InvokeRuntimeCallingConvention calling_convention;
1705 RegisterSet caller_saves = RegisterSet::Empty();
1706 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1707 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00001708 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001709 locations->SetInAt(0, Location::Any());
1710 }
1711}
1712
1713void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001714 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001715 GenerateTestAndBranch<Label>(deoptimize,
1716 /* condition_input_index */ 0,
1717 slow_path->GetEntryLabel(),
1718 /* false_target */ nullptr);
1719}
1720
Mingyao Yang063fc772016-08-02 11:02:54 -07001721void LocationsBuilderX86::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001722 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yang063fc772016-08-02 11:02:54 -07001723 LocationSummary(flag, LocationSummary::kNoCall);
1724 locations->SetOut(Location::RequiresRegister());
1725}
1726
1727void InstructionCodeGeneratorX86::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
1728 __ movl(flag->GetLocations()->Out().AsRegister<Register>(),
1729 Address(ESP, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
1730}
1731
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001732static bool SelectCanUseCMOV(HSelect* select) {
1733 // There are no conditional move instructions for XMMs.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001734 if (DataType::IsFloatingPointType(select->GetType())) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001735 return false;
1736 }
1737
1738 // A FP condition doesn't generate the single CC that we need.
1739 // In 32 bit mode, a long condition doesn't generate a single CC either.
1740 HInstruction* condition = select->GetCondition();
1741 if (condition->IsCondition()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001742 DataType::Type compare_type = condition->InputAt(0)->GetType();
1743 if (compare_type == DataType::Type::kInt64 ||
1744 DataType::IsFloatingPointType(compare_type)) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001745 return false;
1746 }
1747 }
1748
1749 // We can generate a CMOV for this Select.
1750 return true;
1751}
1752
David Brazdil74eb1b22015-12-14 11:44:01 +00001753void LocationsBuilderX86::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001754 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001755 if (DataType::IsFloatingPointType(select->GetType())) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001756 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001757 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001758 } else {
1759 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001760 if (SelectCanUseCMOV(select)) {
1761 if (select->InputAt(1)->IsConstant()) {
1762 // Cmov can't handle a constant value.
1763 locations->SetInAt(1, Location::RequiresRegister());
1764 } else {
1765 locations->SetInAt(1, Location::Any());
1766 }
1767 } else {
1768 locations->SetInAt(1, Location::Any());
1769 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001770 }
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001771 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1772 locations->SetInAt(2, Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00001773 }
1774 locations->SetOut(Location::SameAsFirstInput());
1775}
1776
1777void InstructionCodeGeneratorX86::VisitSelect(HSelect* select) {
1778 LocationSummary* locations = select->GetLocations();
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001779 DCHECK(locations->InAt(0).Equals(locations->Out()));
1780 if (SelectCanUseCMOV(select)) {
1781 // If both the condition and the source types are integer, we can generate
1782 // a CMOV to implement Select.
1783
1784 HInstruction* select_condition = select->GetCondition();
1785 Condition cond = kNotEqual;
1786
1787 // Figure out how to test the 'condition'.
1788 if (select_condition->IsCondition()) {
1789 HCondition* condition = select_condition->AsCondition();
1790 if (!condition->IsEmittedAtUseSite()) {
1791 // This was a previously materialized condition.
1792 // Can we use the existing condition code?
1793 if (AreEflagsSetFrom(condition, select)) {
1794 // Materialization was the previous instruction. Condition codes are right.
1795 cond = X86Condition(condition->GetCondition());
1796 } else {
1797 // No, we have to recreate the condition code.
1798 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1799 __ testl(cond_reg, cond_reg);
1800 }
1801 } else {
1802 // We can't handle FP or long here.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001803 DCHECK_NE(condition->InputAt(0)->GetType(), DataType::Type::kInt64);
1804 DCHECK(!DataType::IsFloatingPointType(condition->InputAt(0)->GetType()));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001805 LocationSummary* cond_locations = condition->GetLocations();
Roland Levillain0b671c02016-08-19 12:02:34 +01001806 codegen_->GenerateIntCompare(cond_locations->InAt(0), cond_locations->InAt(1));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001807 cond = X86Condition(condition->GetCondition());
1808 }
1809 } else {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01001810 // Must be a Boolean condition, which needs to be compared to 0.
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001811 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1812 __ testl(cond_reg, cond_reg);
1813 }
1814
1815 // If the condition is true, overwrite the output, which already contains false.
1816 Location false_loc = locations->InAt(0);
1817 Location true_loc = locations->InAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001818 if (select->GetType() == DataType::Type::kInt64) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001819 // 64 bit conditional move.
1820 Register false_high = false_loc.AsRegisterPairHigh<Register>();
1821 Register false_low = false_loc.AsRegisterPairLow<Register>();
1822 if (true_loc.IsRegisterPair()) {
1823 __ cmovl(cond, false_high, true_loc.AsRegisterPairHigh<Register>());
1824 __ cmovl(cond, false_low, true_loc.AsRegisterPairLow<Register>());
1825 } else {
1826 __ cmovl(cond, false_high, Address(ESP, true_loc.GetHighStackIndex(kX86WordSize)));
1827 __ cmovl(cond, false_low, Address(ESP, true_loc.GetStackIndex()));
1828 }
1829 } else {
1830 // 32 bit conditional move.
1831 Register false_reg = false_loc.AsRegister<Register>();
1832 if (true_loc.IsRegister()) {
1833 __ cmovl(cond, false_reg, true_loc.AsRegister<Register>());
1834 } else {
1835 __ cmovl(cond, false_reg, Address(ESP, true_loc.GetStackIndex()));
1836 }
1837 }
1838 } else {
1839 NearLabel false_target;
1840 GenerateTestAndBranch<NearLabel>(
1841 select, /* condition_input_index */ 2, /* true_target */ nullptr, &false_target);
1842 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1843 __ Bind(&false_target);
1844 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001845}
1846
David Srbecky0cf44932015-12-09 14:09:59 +00001847void LocationsBuilderX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001848 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00001849}
1850
David Srbeckyd28f4a02016-03-14 17:14:24 +00001851void InstructionCodeGeneratorX86::VisitNativeDebugInfo(HNativeDebugInfo*) {
1852 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001853}
1854
1855void CodeGeneratorX86::GenerateNop() {
1856 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001857}
1858
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001859void LocationsBuilderX86::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001860 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01001861 new (GetGraph()->GetAllocator()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001862 // Handle the long/FP comparisons made in instruction simplification.
1863 switch (cond->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001864 case DataType::Type::kInt64: {
Mark Mendellc4701932015-04-10 13:18:51 -04001865 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell8659e842016-02-16 10:41:46 -05001866 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001867 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001868 locations->SetOut(Location::RequiresRegister());
1869 }
1870 break;
1871 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001872 case DataType::Type::kFloat32:
1873 case DataType::Type::kFloat64: {
Mark Mendellc4701932015-04-10 13:18:51 -04001874 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001875 if (cond->InputAt(1)->IsX86LoadFromConstantTable()) {
1876 DCHECK(cond->InputAt(1)->IsEmittedAtUseSite());
1877 } else if (cond->InputAt(1)->IsConstant()) {
1878 locations->SetInAt(1, Location::RequiresFpuRegister());
1879 } else {
1880 locations->SetInAt(1, Location::Any());
1881 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001882 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001883 locations->SetOut(Location::RequiresRegister());
1884 }
1885 break;
1886 }
1887 default:
1888 locations->SetInAt(0, Location::RequiresRegister());
1889 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001890 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001891 // We need a byte register.
1892 locations->SetOut(Location::RegisterLocation(ECX));
1893 }
1894 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001895 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001896}
1897
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001898void InstructionCodeGeneratorX86::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001899 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001900 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001901 }
Mark Mendellc4701932015-04-10 13:18:51 -04001902
1903 LocationSummary* locations = cond->GetLocations();
1904 Location lhs = locations->InAt(0);
1905 Location rhs = locations->InAt(1);
1906 Register reg = locations->Out().AsRegister<Register>();
Mark Mendell152408f2015-12-31 12:28:50 -05001907 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001908
1909 switch (cond->InputAt(0)->GetType()) {
1910 default: {
1911 // Integer case.
1912
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01001913 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04001914 __ xorl(reg, reg);
Roland Levillain0b671c02016-08-19 12:02:34 +01001915 codegen_->GenerateIntCompare(lhs, rhs);
Aart Bike9f37602015-10-09 11:15:55 -07001916 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001917 return;
1918 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001919 case DataType::Type::kInt64:
Mark Mendellc4701932015-04-10 13:18:51 -04001920 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1921 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001922 case DataType::Type::kFloat32:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001923 GenerateFPCompare(lhs, rhs, cond, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001924 GenerateFPJumps(cond, &true_label, &false_label);
1925 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001926 case DataType::Type::kFloat64:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001927 GenerateFPCompare(lhs, rhs, cond, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001928 GenerateFPJumps(cond, &true_label, &false_label);
1929 break;
1930 }
1931
1932 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001933 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001934
Roland Levillain4fa13f62015-07-06 18:11:54 +01001935 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001936 __ Bind(&false_label);
1937 __ xorl(reg, reg);
1938 __ jmp(&done_label);
1939
Roland Levillain4fa13f62015-07-06 18:11:54 +01001940 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001941 __ Bind(&true_label);
1942 __ movl(reg, Immediate(1));
1943 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001944}
1945
1946void LocationsBuilderX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001947 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001948}
1949
1950void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001951 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001952}
1953
1954void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001955 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001956}
1957
1958void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001959 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001960}
1961
1962void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001963 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001964}
1965
1966void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001967 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001968}
1969
1970void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001971 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001972}
1973
1974void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001975 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001976}
1977
1978void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001979 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001980}
1981
1982void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001983 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001984}
1985
1986void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001987 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001988}
1989
1990void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001991 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001992}
1993
Aart Bike9f37602015-10-09 11:15:55 -07001994void LocationsBuilderX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001995 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001996}
1997
1998void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001999 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002000}
2001
2002void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002003 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002004}
2005
2006void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002007 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002008}
2009
2010void LocationsBuilderX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002011 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002012}
2013
2014void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002015 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002016}
2017
2018void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002019 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002020}
2021
2022void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002023 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002024}
2025
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002026void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002027 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002028 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002029 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002030}
2031
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002032void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002033 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002034}
2035
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002036void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
2037 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002038 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002039 locations->SetOut(Location::ConstantLocation(constant));
2040}
2041
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002042void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002043 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002044}
2045
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002046void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002047 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002048 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002049 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002050}
2051
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002052void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002053 // Will be generated at use site.
2054}
2055
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002056void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
2057 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002058 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002059 locations->SetOut(Location::ConstantLocation(constant));
2060}
2061
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002062void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002063 // Will be generated at use site.
2064}
2065
2066void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
2067 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002068 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002069 locations->SetOut(Location::ConstantLocation(constant));
2070}
2071
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002072void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002073 // Will be generated at use site.
2074}
2075
Igor Murashkind01745e2017-04-05 16:40:31 -07002076void LocationsBuilderX86::VisitConstructorFence(HConstructorFence* constructor_fence) {
2077 constructor_fence->SetLocations(nullptr);
2078}
2079
2080void InstructionCodeGeneratorX86::VisitConstructorFence(
2081 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
2082 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
2083}
2084
Calin Juravle27df7582015-04-17 19:12:31 +01002085void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2086 memory_barrier->SetLocations(nullptr);
2087}
2088
2089void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00002090 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002091}
2092
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002093void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002094 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002095}
2096
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002097void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002098 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002099}
2100
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002101void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002102 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002103 new (GetGraph()->GetAllocator()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002104 switch (ret->InputAt(0)->GetType()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002105 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002106 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002107 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002108 case DataType::Type::kInt8:
2109 case DataType::Type::kUint16:
2110 case DataType::Type::kInt16:
2111 case DataType::Type::kInt32:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002112 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002113 break;
2114
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002115 case DataType::Type::kInt64:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002116 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002117 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002118 break;
2119
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002120 case DataType::Type::kFloat32:
2121 case DataType::Type::kFloat64:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002122 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002123 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002124 break;
2125
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002126 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002127 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002128 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002129}
2130
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002131void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002132 if (kIsDebugBuild) {
2133 switch (ret->InputAt(0)->GetType()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002134 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002135 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002136 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002137 case DataType::Type::kInt8:
2138 case DataType::Type::kUint16:
2139 case DataType::Type::kInt16:
2140 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002141 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002142 break;
2143
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002144 case DataType::Type::kInt64:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002145 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
2146 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002147 break;
2148
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002149 case DataType::Type::kFloat32:
2150 case DataType::Type::kFloat64:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002151 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002152 break;
2153
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002154 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002155 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002156 }
2157 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002158 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002159}
2160
Calin Juravle175dc732015-08-25 15:42:32 +01002161void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2162 // The trampoline uses the same calling convention as dex calling conventions,
2163 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2164 // the method_idx.
2165 HandleInvoke(invoke);
2166}
2167
2168void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2169 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2170}
2171
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002172void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002173 // Explicit clinit checks triggered by static invokes must have been pruned by
2174 // art::PrepareForRegisterAllocation.
2175 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002176
Mark Mendellfb8d2792015-03-31 22:16:59 -04002177 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002178 if (intrinsic.TryDispatch(invoke)) {
Vladimir Marko65979462017-05-19 17:25:12 +01002179 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeMethodLoadKind()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00002180 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002181 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04002182 return;
2183 }
2184
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002185 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002186
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002187 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
Vladimir Marko65979462017-05-19 17:25:12 +01002188 if (invoke->HasPcRelativeMethodLoadKind()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00002189 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002190 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002191}
2192
Mark Mendell09ed1a32015-03-25 08:30:06 -04002193static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
2194 if (invoke->GetLocations()->Intrinsified()) {
2195 IntrinsicCodeGeneratorX86 intrinsic(codegen);
2196 intrinsic.Dispatch(invoke);
2197 return true;
2198 }
2199 return false;
2200}
2201
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002202void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002203 // Explicit clinit checks triggered by static invokes must have been pruned by
2204 // art::PrepareForRegisterAllocation.
2205 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002206
Mark Mendell09ed1a32015-03-25 08:30:06 -04002207 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2208 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002209 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002210
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002211 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04002212 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002213 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002214}
2215
2216void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00002217 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
2218 if (intrinsic.TryDispatch(invoke)) {
2219 return;
2220 }
2221
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002222 HandleInvoke(invoke);
2223}
2224
2225void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002226 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002227 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002228}
2229
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002230void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002231 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2232 return;
2233 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002234
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002235 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002236 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002237}
2238
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002239void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002240 // This call to HandleInvoke allocates a temporary (core) register
2241 // which is also used to transfer the hidden argument from FP to
2242 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002243 HandleInvoke(invoke);
2244 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002245 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002246}
2247
2248void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
2249 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002250 LocationSummary* locations = invoke->GetLocations();
2251 Register temp = locations->GetTemp(0).AsRegister<Register>();
2252 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002253 Location receiver = locations->InAt(0);
2254 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2255
Roland Levillain0d5a2812015-11-13 10:07:31 +00002256 // Set the hidden argument. This is safe to do this here, as XMM7
2257 // won't be modified thereafter, before the `call` instruction.
2258 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002259 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002260 __ movd(hidden_reg, temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002261
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002262 if (receiver.IsStackSlot()) {
2263 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002264 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002265 __ movl(temp, Address(temp, class_offset));
2266 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002267 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002268 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002269 }
Roland Levillain4d027112015-07-01 15:41:14 +01002270 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002271 // Instead of simply (possibly) unpoisoning `temp` here, we should
2272 // emit a read barrier for the previous class reference load.
2273 // However this is not required in practice, as this is an
2274 // intermediate/temporary reference and because the current
2275 // concurrent copying collector keeps the from-space memory
2276 // intact/accessible until the end of the marking phase (the
2277 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002278 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002279 // temp = temp->GetAddressOfIMT()
2280 __ movl(temp,
2281 Address(temp, mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002282 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002283 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002284 invoke->GetImtIndex(), kX86PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002285 __ movl(temp, Address(temp, method_offset));
2286 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002287 __ call(Address(temp,
Andreas Gampe542451c2016-07-26 09:02:02 -07002288 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002289
2290 DCHECK(!codegen_->IsLeafMethod());
2291 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2292}
2293
Orion Hodsonac141392017-01-13 11:53:47 +00002294void LocationsBuilderX86::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2295 HandleInvoke(invoke);
2296}
2297
2298void InstructionCodeGeneratorX86::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2299 codegen_->GenerateInvokePolymorphicCall(invoke);
2300}
2301
Roland Levillain88cb1752014-10-20 16:36:47 +01002302void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2303 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002304 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Roland Levillain88cb1752014-10-20 16:36:47 +01002305 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002306 case DataType::Type::kInt32:
2307 case DataType::Type::kInt64:
Roland Levillain88cb1752014-10-20 16:36:47 +01002308 locations->SetInAt(0, Location::RequiresRegister());
2309 locations->SetOut(Location::SameAsFirstInput());
2310 break;
2311
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002312 case DataType::Type::kFloat32:
Roland Levillain5368c212014-11-27 15:03:41 +00002313 locations->SetInAt(0, Location::RequiresFpuRegister());
2314 locations->SetOut(Location::SameAsFirstInput());
2315 locations->AddTemp(Location::RequiresRegister());
2316 locations->AddTemp(Location::RequiresFpuRegister());
2317 break;
2318
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002319 case DataType::Type::kFloat64:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002320 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002321 locations->SetOut(Location::SameAsFirstInput());
2322 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002323 break;
2324
2325 default:
2326 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2327 }
2328}
2329
2330void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2331 LocationSummary* locations = neg->GetLocations();
2332 Location out = locations->Out();
2333 Location in = locations->InAt(0);
2334 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002335 case DataType::Type::kInt32:
Roland Levillain88cb1752014-10-20 16:36:47 +01002336 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002337 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002338 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002339 break;
2340
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002341 case DataType::Type::kInt64:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002342 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002343 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002344 __ negl(out.AsRegisterPairLow<Register>());
2345 // Negation is similar to subtraction from zero. The least
2346 // significant byte triggers a borrow when it is different from
2347 // zero; to take it into account, add 1 to the most significant
2348 // byte if the carry flag (CF) is set to 1 after the first NEGL
2349 // operation.
2350 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2351 __ negl(out.AsRegisterPairHigh<Register>());
2352 break;
2353
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002354 case DataType::Type::kFloat32: {
Roland Levillain5368c212014-11-27 15:03:41 +00002355 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002356 Register constant = locations->GetTemp(0).AsRegister<Register>();
2357 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002358 // Implement float negation with an exclusive or with value
2359 // 0x80000000 (mask for bit 31, representing the sign of a
2360 // single-precision floating-point number).
2361 __ movl(constant, Immediate(INT32_C(0x80000000)));
2362 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002363 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002364 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002365 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002366
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002367 case DataType::Type::kFloat64: {
Roland Levillain5368c212014-11-27 15:03:41 +00002368 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002369 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002370 // Implement double negation with an exclusive or with value
2371 // 0x8000000000000000 (mask for bit 63, representing the sign of
2372 // a double-precision floating-point number).
2373 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002374 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002375 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002376 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002377
2378 default:
2379 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2380 }
2381}
2382
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002383void LocationsBuilderX86::VisitX86FPNeg(HX86FPNeg* neg) {
2384 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002385 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002386 DCHECK(DataType::IsFloatingPointType(neg->GetType()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002387 locations->SetInAt(0, Location::RequiresFpuRegister());
2388 locations->SetInAt(1, Location::RequiresRegister());
2389 locations->SetOut(Location::SameAsFirstInput());
2390 locations->AddTemp(Location::RequiresFpuRegister());
2391}
2392
2393void InstructionCodeGeneratorX86::VisitX86FPNeg(HX86FPNeg* neg) {
2394 LocationSummary* locations = neg->GetLocations();
2395 Location out = locations->Out();
2396 DCHECK(locations->InAt(0).Equals(out));
2397
2398 Register constant_area = locations->InAt(1).AsRegister<Register>();
2399 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002400 if (neg->GetType() == DataType::Type::kFloat32) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00002401 __ movss(mask, codegen_->LiteralInt32Address(INT32_C(0x80000000),
2402 neg->GetBaseMethodAddress(),
2403 constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002404 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
2405 } else {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00002406 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000),
2407 neg->GetBaseMethodAddress(),
2408 constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002409 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
2410 }
2411}
2412
Roland Levillaindff1f282014-11-05 14:15:05 +00002413void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002414 DataType::Type result_type = conversion->GetResultType();
2415 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002416 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
2417 << input_type << " -> " << result_type;
Roland Levillain624279f2014-12-04 11:54:28 +00002418
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002419 // The float-to-long and double-to-long type conversions rely on a
2420 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002421 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002422 ((input_type == DataType::Type::kFloat32 || input_type == DataType::Type::kFloat64)
2423 && result_type == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002424 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002425 : LocationSummary::kNoCall;
2426 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002427 new (GetGraph()->GetAllocator()) LocationSummary(conversion, call_kind);
Roland Levillain624279f2014-12-04 11:54:28 +00002428
Roland Levillaindff1f282014-11-05 14:15:05 +00002429 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002430 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002431 case DataType::Type::kInt8:
Roland Levillain51d3fc42014-11-13 14:11:42 +00002432 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002433 case DataType::Type::kUint8:
2434 case DataType::Type::kInt8:
2435 case DataType::Type::kUint16:
2436 case DataType::Type::kInt16:
2437 case DataType::Type::kInt32:
2438 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2439 // Make the output overlap to please the register allocator. This greatly simplifies
2440 // the validation of the linear scan implementation
2441 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2442 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002443 case DataType::Type::kInt64: {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002444 HInstruction* input = conversion->InputAt(0);
2445 Location input_location = input->IsConstant()
2446 ? Location::ConstantLocation(input->AsConstant())
2447 : Location::RegisterPairLocation(EAX, EDX);
2448 locations->SetInAt(0, input_location);
2449 // Make the output overlap to please the register allocator. This greatly simplifies
2450 // the validation of the linear scan implementation
2451 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2452 break;
2453 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002454
2455 default:
2456 LOG(FATAL) << "Unexpected type conversion from " << input_type
2457 << " to " << result_type;
2458 }
2459 break;
2460
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002461 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002462 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002463 DCHECK(DataType::IsIntegralType(input_type)) << input_type;
2464 locations->SetInAt(0, Location::Any());
2465 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Roland Levillain01a8d712014-11-14 16:27:39 +00002466 break;
2467
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002468 case DataType::Type::kInt32:
Roland Levillain946e1432014-11-11 17:35:19 +00002469 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002470 case DataType::Type::kInt64:
Roland Levillain946e1432014-11-11 17:35:19 +00002471 locations->SetInAt(0, Location::Any());
2472 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2473 break;
2474
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002475 case DataType::Type::kFloat32:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002476 locations->SetInAt(0, Location::RequiresFpuRegister());
2477 locations->SetOut(Location::RequiresRegister());
2478 locations->AddTemp(Location::RequiresFpuRegister());
2479 break;
2480
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002481 case DataType::Type::kFloat64:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002482 locations->SetInAt(0, Location::RequiresFpuRegister());
2483 locations->SetOut(Location::RequiresRegister());
2484 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002485 break;
2486
2487 default:
2488 LOG(FATAL) << "Unexpected type conversion from " << input_type
2489 << " to " << result_type;
2490 }
2491 break;
2492
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002493 case DataType::Type::kInt64:
Roland Levillaindff1f282014-11-05 14:15:05 +00002494 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002495 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002496 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002497 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002498 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002499 case DataType::Type::kInt16:
2500 case DataType::Type::kInt32:
Roland Levillaindff1f282014-11-05 14:15:05 +00002501 locations->SetInAt(0, Location::RegisterLocation(EAX));
2502 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2503 break;
2504
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002505 case DataType::Type::kFloat32:
2506 case DataType::Type::kFloat64: {
Vladimir Marko949c91f2015-01-27 10:48:44 +00002507 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002508 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2509 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2510
Vladimir Marko949c91f2015-01-27 10:48:44 +00002511 // The runtime helper puts the result in EAX, EDX.
2512 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002513 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002514 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002515
2516 default:
2517 LOG(FATAL) << "Unexpected type conversion from " << input_type
2518 << " to " << result_type;
2519 }
2520 break;
2521
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002522 case DataType::Type::kFloat32:
Roland Levillaincff13742014-11-17 14:32:17 +00002523 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002524 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002525 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002526 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002527 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002528 case DataType::Type::kInt16:
2529 case DataType::Type::kInt32:
Roland Levillaincff13742014-11-17 14:32:17 +00002530 locations->SetInAt(0, Location::RequiresRegister());
2531 locations->SetOut(Location::RequiresFpuRegister());
2532 break;
2533
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002534 case DataType::Type::kInt64:
Roland Levillain232ade02015-04-20 15:14:36 +01002535 locations->SetInAt(0, Location::Any());
2536 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002537 break;
2538
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002539 case DataType::Type::kFloat64:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002540 locations->SetInAt(0, Location::RequiresFpuRegister());
2541 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002542 break;
2543
2544 default:
2545 LOG(FATAL) << "Unexpected type conversion from " << input_type
2546 << " to " << result_type;
2547 };
2548 break;
2549
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002550 case DataType::Type::kFloat64:
Roland Levillaincff13742014-11-17 14:32:17 +00002551 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002552 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002553 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002554 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002555 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002556 case DataType::Type::kInt16:
2557 case DataType::Type::kInt32:
Roland Levillaincff13742014-11-17 14:32:17 +00002558 locations->SetInAt(0, Location::RequiresRegister());
2559 locations->SetOut(Location::RequiresFpuRegister());
2560 break;
2561
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002562 case DataType::Type::kInt64:
Roland Levillain232ade02015-04-20 15:14:36 +01002563 locations->SetInAt(0, Location::Any());
2564 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002565 break;
2566
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002567 case DataType::Type::kFloat32:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002568 locations->SetInAt(0, Location::RequiresFpuRegister());
2569 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002570 break;
2571
2572 default:
2573 LOG(FATAL) << "Unexpected type conversion from " << input_type
2574 << " to " << result_type;
2575 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002576 break;
2577
2578 default:
2579 LOG(FATAL) << "Unexpected type conversion from " << input_type
2580 << " to " << result_type;
2581 }
2582}
2583
2584void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2585 LocationSummary* locations = conversion->GetLocations();
2586 Location out = locations->Out();
2587 Location in = locations->InAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002588 DataType::Type result_type = conversion->GetResultType();
2589 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002590 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
2591 << input_type << " -> " << result_type;
Roland Levillaindff1f282014-11-05 14:15:05 +00002592 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002593 case DataType::Type::kUint8:
2594 switch (input_type) {
2595 case DataType::Type::kInt8:
2596 case DataType::Type::kUint16:
2597 case DataType::Type::kInt16:
2598 case DataType::Type::kInt32:
2599 if (in.IsRegister()) {
2600 __ movzxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
2601 } else {
2602 DCHECK(in.GetConstant()->IsIntConstant());
2603 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2604 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint8_t>(value)));
2605 }
2606 break;
2607 case DataType::Type::kInt64:
2608 if (in.IsRegisterPair()) {
2609 __ movzxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2610 } else {
2611 DCHECK(in.GetConstant()->IsLongConstant());
2612 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2613 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint8_t>(value)));
2614 }
2615 break;
2616
2617 default:
2618 LOG(FATAL) << "Unexpected type conversion from " << input_type
2619 << " to " << result_type;
2620 }
2621 break;
2622
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002623 case DataType::Type::kInt8:
Roland Levillain51d3fc42014-11-13 14:11:42 +00002624 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002625 case DataType::Type::kUint8:
2626 case DataType::Type::kUint16:
2627 case DataType::Type::kInt16:
2628 case DataType::Type::kInt32:
2629 if (in.IsRegister()) {
2630 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
2631 } else {
2632 DCHECK(in.GetConstant()->IsIntConstant());
2633 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2634 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2635 }
2636 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002637 case DataType::Type::kInt64:
Vladimir Markob52bbde2016-02-12 12:06:05 +00002638 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;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002646
2647 default:
2648 LOG(FATAL) << "Unexpected type conversion from " << input_type
2649 << " to " << result_type;
2650 }
2651 break;
2652
2653 case DataType::Type::kUint16:
2654 switch (input_type) {
2655 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002656 case DataType::Type::kInt16:
2657 case DataType::Type::kInt32:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002658 if (in.IsRegister()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002659 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
2660 } else if (in.IsStackSlot()) {
2661 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002662 } else {
2663 DCHECK(in.GetConstant()->IsIntConstant());
2664 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002665 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
2666 }
2667 break;
2668 case DataType::Type::kInt64:
2669 if (in.IsRegisterPair()) {
2670 __ movzxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2671 } else if (in.IsDoubleStackSlot()) {
2672 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2673 } else {
2674 DCHECK(in.GetConstant()->IsLongConstant());
2675 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2676 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002677 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002678 break;
2679
2680 default:
2681 LOG(FATAL) << "Unexpected type conversion from " << input_type
2682 << " to " << result_type;
2683 }
2684 break;
2685
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002686 case DataType::Type::kInt16:
Roland Levillain01a8d712014-11-14 16:27:39 +00002687 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002688 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002689 case DataType::Type::kInt32:
Roland Levillain01a8d712014-11-14 16:27:39 +00002690 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002691 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002692 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002693 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002694 } else {
2695 DCHECK(in.GetConstant()->IsIntConstant());
2696 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002697 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002698 }
2699 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002700 case DataType::Type::kInt64:
2701 if (in.IsRegisterPair()) {
2702 __ movsxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2703 } else if (in.IsDoubleStackSlot()) {
2704 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2705 } else {
2706 DCHECK(in.GetConstant()->IsLongConstant());
2707 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2708 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
2709 }
2710 break;
Roland Levillain01a8d712014-11-14 16:27:39 +00002711
2712 default:
2713 LOG(FATAL) << "Unexpected type conversion from " << input_type
2714 << " to " << result_type;
2715 }
2716 break;
2717
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002718 case DataType::Type::kInt32:
Roland Levillain946e1432014-11-11 17:35:19 +00002719 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002720 case DataType::Type::kInt64:
Roland Levillain946e1432014-11-11 17:35:19 +00002721 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002722 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002723 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002724 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002725 } else {
2726 DCHECK(in.IsConstant());
2727 DCHECK(in.GetConstant()->IsLongConstant());
2728 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002729 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002730 }
2731 break;
2732
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002733 case DataType::Type::kFloat32: {
Roland Levillain3f8f9362014-12-02 17:45:01 +00002734 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2735 Register output = out.AsRegister<Register>();
2736 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002737 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002738
2739 __ movl(output, Immediate(kPrimIntMax));
2740 // temp = int-to-float(output)
2741 __ cvtsi2ss(temp, output);
2742 // if input >= temp goto done
2743 __ comiss(input, temp);
2744 __ j(kAboveEqual, &done);
2745 // if input == NaN goto nan
2746 __ j(kUnordered, &nan);
2747 // output = float-to-int-truncate(input)
2748 __ cvttss2si(output, input);
2749 __ jmp(&done);
2750 __ Bind(&nan);
2751 // output = 0
2752 __ xorl(output, output);
2753 __ Bind(&done);
2754 break;
2755 }
2756
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002757 case DataType::Type::kFloat64: {
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002758 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2759 Register output = out.AsRegister<Register>();
2760 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002761 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002762
2763 __ movl(output, Immediate(kPrimIntMax));
2764 // temp = int-to-double(output)
2765 __ cvtsi2sd(temp, output);
2766 // if input >= temp goto done
2767 __ comisd(input, temp);
2768 __ j(kAboveEqual, &done);
2769 // if input == NaN goto nan
2770 __ j(kUnordered, &nan);
2771 // output = double-to-int-truncate(input)
2772 __ cvttsd2si(output, input);
2773 __ jmp(&done);
2774 __ Bind(&nan);
2775 // output = 0
2776 __ xorl(output, output);
2777 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002778 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002779 }
Roland Levillain946e1432014-11-11 17:35:19 +00002780
2781 default:
2782 LOG(FATAL) << "Unexpected type conversion from " << input_type
2783 << " to " << result_type;
2784 }
2785 break;
2786
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002787 case DataType::Type::kInt64:
Roland Levillaindff1f282014-11-05 14:15:05 +00002788 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002789 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002790 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002791 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002792 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002793 case DataType::Type::kInt16:
2794 case DataType::Type::kInt32:
Roland Levillaindff1f282014-11-05 14:15:05 +00002795 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2796 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002797 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002798 __ cdq();
2799 break;
2800
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002801 case DataType::Type::kFloat32:
Serban Constantinescuba45db02016-07-12 22:53:02 +01002802 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002803 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002804 break;
2805
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002806 case DataType::Type::kFloat64:
Serban Constantinescuba45db02016-07-12 22:53:02 +01002807 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002808 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002809 break;
2810
2811 default:
2812 LOG(FATAL) << "Unexpected type conversion from " << input_type
2813 << " to " << result_type;
2814 }
2815 break;
2816
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002817 case DataType::Type::kFloat32:
Roland Levillaincff13742014-11-17 14:32:17 +00002818 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002819 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002820 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002821 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002822 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002823 case DataType::Type::kInt16:
2824 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002825 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002826 break;
2827
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002828 case DataType::Type::kInt64: {
Roland Levillain232ade02015-04-20 15:14:36 +01002829 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002830
Roland Levillain232ade02015-04-20 15:14:36 +01002831 // Create stack space for the call to
2832 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2833 // TODO: enhance register allocator to ask for stack temporaries.
2834 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002835 adjustment = DataType::Size(DataType::Type::kInt64);
Roland Levillain232ade02015-04-20 15:14:36 +01002836 __ subl(ESP, Immediate(adjustment));
2837 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002838
Roland Levillain232ade02015-04-20 15:14:36 +01002839 // Load the value to the FP stack, using temporaries if needed.
2840 PushOntoFPStack(in, 0, adjustment, false, true);
2841
2842 if (out.IsStackSlot()) {
2843 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2844 } else {
2845 __ fstps(Address(ESP, 0));
2846 Location stack_temp = Location::StackSlot(0);
2847 codegen_->Move32(out, stack_temp);
2848 }
2849
2850 // Remove the temporary stack space we allocated.
2851 if (adjustment != 0) {
2852 __ addl(ESP, Immediate(adjustment));
2853 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002854 break;
2855 }
2856
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002857 case DataType::Type::kFloat64:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002858 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002859 break;
2860
2861 default:
2862 LOG(FATAL) << "Unexpected type conversion from " << input_type
2863 << " to " << result_type;
2864 };
2865 break;
2866
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002867 case DataType::Type::kFloat64:
Roland Levillaincff13742014-11-17 14:32:17 +00002868 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002869 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002870 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002871 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002872 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002873 case DataType::Type::kInt16:
2874 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002875 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002876 break;
2877
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002878 case DataType::Type::kInt64: {
Roland Levillain232ade02015-04-20 15:14:36 +01002879 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002880
Roland Levillain232ade02015-04-20 15:14:36 +01002881 // Create stack space for the call to
2882 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2883 // TODO: enhance register allocator to ask for stack temporaries.
2884 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002885 adjustment = DataType::Size(DataType::Type::kInt64);
Roland Levillain232ade02015-04-20 15:14:36 +01002886 __ subl(ESP, Immediate(adjustment));
2887 }
2888
2889 // Load the value to the FP stack, using temporaries if needed.
2890 PushOntoFPStack(in, 0, adjustment, false, true);
2891
2892 if (out.IsDoubleStackSlot()) {
2893 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2894 } else {
2895 __ fstpl(Address(ESP, 0));
2896 Location stack_temp = Location::DoubleStackSlot(0);
2897 codegen_->Move64(out, stack_temp);
2898 }
2899
2900 // Remove the temporary stack space we allocated.
2901 if (adjustment != 0) {
2902 __ addl(ESP, Immediate(adjustment));
2903 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002904 break;
2905 }
2906
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002907 case DataType::Type::kFloat32:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002908 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002909 break;
2910
2911 default:
2912 LOG(FATAL) << "Unexpected type conversion from " << input_type
2913 << " to " << result_type;
2914 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002915 break;
2916
2917 default:
2918 LOG(FATAL) << "Unexpected type conversion from " << input_type
2919 << " to " << result_type;
2920 }
2921}
2922
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002923void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002924 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002925 new (GetGraph()->GetAllocator()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002926 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002927 case DataType::Type::kInt32: {
Mark Mendell09b84632015-02-13 17:48:38 -05002928 locations->SetInAt(0, Location::RequiresRegister());
2929 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2930 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2931 break;
2932 }
2933
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002934 case DataType::Type::kInt64: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002935 locations->SetInAt(0, Location::RequiresRegister());
2936 locations->SetInAt(1, Location::Any());
2937 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002938 break;
2939 }
2940
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002941 case DataType::Type::kFloat32:
2942 case DataType::Type::kFloat64: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002943 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002944 if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2945 DCHECK(add->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002946 } else if (add->InputAt(1)->IsConstant()) {
2947 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002948 } else {
2949 locations->SetInAt(1, Location::Any());
2950 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002951 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002952 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002953 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002954
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002955 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002956 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2957 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002958 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002959}
2960
2961void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2962 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002963 Location first = locations->InAt(0);
2964 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002965 Location out = locations->Out();
2966
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002967 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002968 case DataType::Type::kInt32: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002969 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002970 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2971 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002972 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2973 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05002974 } else {
2975 __ leal(out.AsRegister<Register>(), Address(
2976 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
2977 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002978 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002979 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
2980 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2981 __ addl(out.AsRegister<Register>(), Immediate(value));
2982 } else {
2983 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
2984 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002985 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05002986 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002987 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002988 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002989 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002990 }
2991
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002992 case DataType::Type::kInt64: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002993 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002994 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2995 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002996 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002997 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
2998 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002999 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003000 } else {
3001 DCHECK(second.IsConstant()) << second;
3002 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3003 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3004 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003005 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003006 break;
3007 }
3008
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003009 case DataType::Type::kFloat32: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003010 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003011 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04003012 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3013 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003014 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003015 __ addss(first.AsFpuRegister<XmmRegister>(),
3016 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003017 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3018 const_area->GetBaseMethodAddress(),
3019 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003020 } else {
3021 DCHECK(second.IsStackSlot());
3022 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003023 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003024 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003025 }
3026
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003027 case DataType::Type::kFloat64: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003028 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003029 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04003030 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3031 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003032 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003033 __ addsd(first.AsFpuRegister<XmmRegister>(),
3034 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003035 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3036 const_area->GetBaseMethodAddress(),
3037 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003038 } else {
3039 DCHECK(second.IsDoubleStackSlot());
3040 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003041 }
3042 break;
3043 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003044
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003045 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003046 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003047 }
3048}
3049
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003050void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003051 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003052 new (GetGraph()->GetAllocator()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003053 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003054 case DataType::Type::kInt32:
3055 case DataType::Type::kInt64: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003056 locations->SetInAt(0, Location::RequiresRegister());
3057 locations->SetInAt(1, Location::Any());
3058 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003059 break;
3060 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003061 case DataType::Type::kFloat32:
3062 case DataType::Type::kFloat64: {
Calin Juravle11351682014-10-23 15:38:15 +01003063 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003064 if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3065 DCHECK(sub->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003066 } else if (sub->InputAt(1)->IsConstant()) {
3067 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003068 } else {
3069 locations->SetInAt(1, Location::Any());
3070 }
Calin Juravle11351682014-10-23 15:38:15 +01003071 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003072 break;
Calin Juravle11351682014-10-23 15:38:15 +01003073 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003074
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003075 default:
Calin Juravle11351682014-10-23 15:38:15 +01003076 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003077 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003078}
3079
3080void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
3081 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003082 Location first = locations->InAt(0);
3083 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01003084 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003085 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003086 case DataType::Type::kInt32: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003087 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003088 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003089 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003090 __ subl(first.AsRegister<Register>(),
3091 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003092 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003093 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003094 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003095 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003096 }
3097
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003098 case DataType::Type::kInt64: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003099 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01003100 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3101 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003102 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01003103 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003104 __ sbbl(first.AsRegisterPairHigh<Register>(),
3105 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003106 } else {
3107 DCHECK(second.IsConstant()) << second;
3108 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3109 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3110 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003111 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003112 break;
3113 }
3114
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003115 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003116 if (second.IsFpuRegister()) {
3117 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3118 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3119 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003120 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003121 __ subss(first.AsFpuRegister<XmmRegister>(),
3122 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003123 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3124 const_area->GetBaseMethodAddress(),
3125 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003126 } else {
3127 DCHECK(second.IsStackSlot());
3128 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3129 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003130 break;
Calin Juravle11351682014-10-23 15:38:15 +01003131 }
3132
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003133 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003134 if (second.IsFpuRegister()) {
3135 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3136 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3137 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003138 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003139 __ subsd(first.AsFpuRegister<XmmRegister>(),
3140 codegen_->LiteralDoubleAddress(
3141 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003142 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003143 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3144 } else {
3145 DCHECK(second.IsDoubleStackSlot());
3146 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3147 }
Calin Juravle11351682014-10-23 15:38:15 +01003148 break;
3149 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003150
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003151 default:
Calin Juravle11351682014-10-23 15:38:15 +01003152 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003153 }
3154}
3155
Calin Juravle34bacdf2014-10-07 20:23:36 +01003156void LocationsBuilderX86::VisitMul(HMul* mul) {
3157 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003158 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Calin Juravle34bacdf2014-10-07 20:23:36 +01003159 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003160 case DataType::Type::kInt32:
Calin Juravle34bacdf2014-10-07 20:23:36 +01003161 locations->SetInAt(0, Location::RequiresRegister());
3162 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003163 if (mul->InputAt(1)->IsIntConstant()) {
3164 // Can use 3 operand multiply.
3165 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3166 } else {
3167 locations->SetOut(Location::SameAsFirstInput());
3168 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003169 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003170 case DataType::Type::kInt64: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003171 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003172 locations->SetInAt(1, Location::Any());
3173 locations->SetOut(Location::SameAsFirstInput());
3174 // Needed for imul on 32bits with 64bits output.
3175 locations->AddTemp(Location::RegisterLocation(EAX));
3176 locations->AddTemp(Location::RegisterLocation(EDX));
3177 break;
3178 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003179 case DataType::Type::kFloat32:
3180 case DataType::Type::kFloat64: {
Calin Juravleb5bfa962014-10-21 18:02:24 +01003181 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003182 if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3183 DCHECK(mul->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003184 } else if (mul->InputAt(1)->IsConstant()) {
3185 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003186 } else {
3187 locations->SetInAt(1, Location::Any());
3188 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003189 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003190 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003191 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003192
3193 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003194 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003195 }
3196}
3197
3198void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
3199 LocationSummary* locations = mul->GetLocations();
3200 Location first = locations->InAt(0);
3201 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003202 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003203
3204 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003205 case DataType::Type::kInt32:
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003206 // The constant may have ended up in a register, so test explicitly to avoid
3207 // problems where the output may not be the same as the first operand.
3208 if (mul->InputAt(1)->IsIntConstant()) {
3209 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3210 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
3211 } else if (second.IsRegister()) {
3212 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003213 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003214 } else {
3215 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003216 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003217 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003218 }
3219 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003220
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003221 case DataType::Type::kInt64: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003222 Register in1_hi = first.AsRegisterPairHigh<Register>();
3223 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003224 Register eax = locations->GetTemp(0).AsRegister<Register>();
3225 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003226
3227 DCHECK_EQ(EAX, eax);
3228 DCHECK_EQ(EDX, edx);
3229
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003230 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01003231 // output: in1
3232 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3233 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3234 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003235 if (second.IsConstant()) {
3236 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003237
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003238 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3239 int32_t low_value = Low32Bits(value);
3240 int32_t high_value = High32Bits(value);
3241 Immediate low(low_value);
3242 Immediate high(high_value);
3243
3244 __ movl(eax, high);
3245 // eax <- in1.lo * in2.hi
3246 __ imull(eax, in1_lo);
3247 // in1.hi <- in1.hi * in2.lo
3248 __ imull(in1_hi, low);
3249 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3250 __ addl(in1_hi, eax);
3251 // move in2_lo to eax to prepare for double precision
3252 __ movl(eax, low);
3253 // edx:eax <- in1.lo * in2.lo
3254 __ mull(in1_lo);
3255 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3256 __ addl(in1_hi, edx);
3257 // in1.lo <- (in1.lo * in2.lo)[31:0];
3258 __ movl(in1_lo, eax);
3259 } else if (second.IsRegisterPair()) {
3260 Register in2_hi = second.AsRegisterPairHigh<Register>();
3261 Register in2_lo = second.AsRegisterPairLow<Register>();
3262
3263 __ movl(eax, in2_hi);
3264 // eax <- in1.lo * in2.hi
3265 __ imull(eax, in1_lo);
3266 // in1.hi <- in1.hi * in2.lo
3267 __ imull(in1_hi, in2_lo);
3268 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3269 __ addl(in1_hi, eax);
3270 // move in1_lo to eax to prepare for double precision
3271 __ movl(eax, in1_lo);
3272 // edx:eax <- in1.lo * in2.lo
3273 __ mull(in2_lo);
3274 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3275 __ addl(in1_hi, edx);
3276 // in1.lo <- (in1.lo * in2.lo)[31:0];
3277 __ movl(in1_lo, eax);
3278 } else {
3279 DCHECK(second.IsDoubleStackSlot()) << second;
3280 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
3281 Address in2_lo(ESP, second.GetStackIndex());
3282
3283 __ movl(eax, in2_hi);
3284 // eax <- in1.lo * in2.hi
3285 __ imull(eax, in1_lo);
3286 // in1.hi <- in1.hi * in2.lo
3287 __ imull(in1_hi, in2_lo);
3288 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3289 __ addl(in1_hi, eax);
3290 // move in1_lo to eax to prepare for double precision
3291 __ movl(eax, in1_lo);
3292 // edx:eax <- in1.lo * in2.lo
3293 __ mull(in2_lo);
3294 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3295 __ addl(in1_hi, edx);
3296 // in1.lo <- (in1.lo * in2.lo)[31:0];
3297 __ movl(in1_lo, eax);
3298 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003299
3300 break;
3301 }
3302
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003303 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003304 DCHECK(first.Equals(locations->Out()));
3305 if (second.IsFpuRegister()) {
3306 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3307 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3308 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003309 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003310 __ mulss(first.AsFpuRegister<XmmRegister>(),
3311 codegen_->LiteralFloatAddress(
3312 const_area->GetConstant()->AsFloatConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003313 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003314 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3315 } else {
3316 DCHECK(second.IsStackSlot());
3317 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3318 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003319 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003320 }
3321
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003322 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003323 DCHECK(first.Equals(locations->Out()));
3324 if (second.IsFpuRegister()) {
3325 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3326 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3327 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003328 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003329 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3330 codegen_->LiteralDoubleAddress(
3331 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003332 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003333 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3334 } else {
3335 DCHECK(second.IsDoubleStackSlot());
3336 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3337 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003338 break;
3339 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003340
3341 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003342 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003343 }
3344}
3345
Roland Levillain232ade02015-04-20 15:14:36 +01003346void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3347 uint32_t temp_offset,
3348 uint32_t stack_adjustment,
3349 bool is_fp,
3350 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003351 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003352 DCHECK(!is_wide);
3353 if (is_fp) {
3354 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3355 } else {
3356 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3357 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003358 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003359 DCHECK(is_wide);
3360 if (is_fp) {
3361 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3362 } else {
3363 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3364 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003365 } else {
3366 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003367 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003368 Location stack_temp = Location::StackSlot(temp_offset);
3369 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003370 if (is_fp) {
3371 __ flds(Address(ESP, temp_offset));
3372 } else {
3373 __ filds(Address(ESP, temp_offset));
3374 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003375 } else {
3376 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3377 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003378 if (is_fp) {
3379 __ fldl(Address(ESP, temp_offset));
3380 } else {
3381 __ fildl(Address(ESP, temp_offset));
3382 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003383 }
3384 }
3385}
3386
3387void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003388 DataType::Type type = rem->GetResultType();
3389 bool is_float = type == DataType::Type::kFloat32;
3390 size_t elem_size = DataType::Size(type);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003391 LocationSummary* locations = rem->GetLocations();
3392 Location first = locations->InAt(0);
3393 Location second = locations->InAt(1);
3394 Location out = locations->Out();
3395
3396 // Create stack space for 2 elements.
3397 // TODO: enhance register allocator to ask for stack temporaries.
3398 __ subl(ESP, Immediate(2 * elem_size));
3399
3400 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003401 const bool is_wide = !is_float;
3402 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
3403 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003404
3405 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003406 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003407 __ Bind(&retry);
3408 __ fprem();
3409
3410 // Move FP status to AX.
3411 __ fstsw();
3412
3413 // And see if the argument reduction is complete. This is signaled by the
3414 // C2 FPU flag bit set to 0.
3415 __ andl(EAX, Immediate(kC2ConditionMask));
3416 __ j(kNotEqual, &retry);
3417
3418 // We have settled on the final value. Retrieve it into an XMM register.
3419 // Store FP top of stack to real stack.
3420 if (is_float) {
3421 __ fsts(Address(ESP, 0));
3422 } else {
3423 __ fstl(Address(ESP, 0));
3424 }
3425
3426 // Pop the 2 items from the FP stack.
3427 __ fucompp();
3428
3429 // Load the value from the stack into an XMM register.
3430 DCHECK(out.IsFpuRegister()) << out;
3431 if (is_float) {
3432 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3433 } else {
3434 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3435 }
3436
3437 // And remove the temporary stack space we allocated.
3438 __ addl(ESP, Immediate(2 * elem_size));
3439}
3440
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003441
3442void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3443 DCHECK(instruction->IsDiv() || instruction->IsRem());
3444
3445 LocationSummary* locations = instruction->GetLocations();
3446 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003447 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003448
3449 Register out_register = locations->Out().AsRegister<Register>();
3450 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003451 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003452
3453 DCHECK(imm == 1 || imm == -1);
3454
3455 if (instruction->IsRem()) {
3456 __ xorl(out_register, out_register);
3457 } else {
3458 __ movl(out_register, input_register);
3459 if (imm == -1) {
3460 __ negl(out_register);
3461 }
3462 }
3463}
3464
3465
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003466void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003467 LocationSummary* locations = instruction->GetLocations();
3468
3469 Register out_register = locations->Out().AsRegister<Register>();
3470 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003471 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003472 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3473 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003474
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003475 Register num = locations->GetTemp(0).AsRegister<Register>();
3476
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003477 __ leal(num, Address(input_register, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003478 __ testl(input_register, input_register);
3479 __ cmovl(kGreaterEqual, num, input_register);
3480 int shift = CTZ(imm);
3481 __ sarl(num, Immediate(shift));
3482
3483 if (imm < 0) {
3484 __ negl(num);
3485 }
3486
3487 __ movl(out_register, num);
3488}
3489
3490void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3491 DCHECK(instruction->IsDiv() || instruction->IsRem());
3492
3493 LocationSummary* locations = instruction->GetLocations();
3494 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3495
3496 Register eax = locations->InAt(0).AsRegister<Register>();
3497 Register out = locations->Out().AsRegister<Register>();
3498 Register num;
3499 Register edx;
3500
3501 if (instruction->IsDiv()) {
3502 edx = locations->GetTemp(0).AsRegister<Register>();
3503 num = locations->GetTemp(1).AsRegister<Register>();
3504 } else {
3505 edx = locations->Out().AsRegister<Register>();
3506 num = locations->GetTemp(0).AsRegister<Register>();
3507 }
3508
3509 DCHECK_EQ(EAX, eax);
3510 DCHECK_EQ(EDX, edx);
3511 if (instruction->IsDiv()) {
3512 DCHECK_EQ(EAX, out);
3513 } else {
3514 DCHECK_EQ(EDX, out);
3515 }
3516
3517 int64_t magic;
3518 int shift;
3519 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3520
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003521 // Save the numerator.
3522 __ movl(num, eax);
3523
3524 // EAX = magic
3525 __ movl(eax, Immediate(magic));
3526
3527 // EDX:EAX = magic * numerator
3528 __ imull(num);
3529
3530 if (imm > 0 && magic < 0) {
3531 // EDX += num
3532 __ addl(edx, num);
3533 } else if (imm < 0 && magic > 0) {
3534 __ subl(edx, num);
3535 }
3536
3537 // Shift if needed.
3538 if (shift != 0) {
3539 __ sarl(edx, Immediate(shift));
3540 }
3541
3542 // EDX += 1 if EDX < 0
3543 __ movl(eax, edx);
3544 __ shrl(edx, Immediate(31));
3545 __ addl(edx, eax);
3546
3547 if (instruction->IsRem()) {
3548 __ movl(eax, num);
3549 __ imull(edx, Immediate(imm));
3550 __ subl(eax, edx);
3551 __ movl(edx, eax);
3552 } else {
3553 __ movl(eax, edx);
3554 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003555}
3556
Calin Juravlebacfec32014-11-14 15:54:36 +00003557void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3558 DCHECK(instruction->IsDiv() || instruction->IsRem());
3559
3560 LocationSummary* locations = instruction->GetLocations();
3561 Location out = locations->Out();
3562 Location first = locations->InAt(0);
3563 Location second = locations->InAt(1);
3564 bool is_div = instruction->IsDiv();
3565
3566 switch (instruction->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003567 case DataType::Type::kInt32: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003568 DCHECK_EQ(EAX, first.AsRegister<Register>());
3569 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003570
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003571 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003572 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003573
3574 if (imm == 0) {
3575 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3576 } else if (imm == 1 || imm == -1) {
3577 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003578 } else if (is_div && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003579 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003580 } else {
3581 DCHECK(imm <= -2 || imm >= 2);
3582 GenerateDivRemWithAnyConstant(instruction);
3583 }
3584 } else {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003585 SlowPathCode* slow_path = new (GetGraph()->GetAllocator()) DivRemMinusOneSlowPathX86(
David Srbecky9cd6d372016-02-09 15:24:47 +00003586 instruction, out.AsRegister<Register>(), is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003587 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003588
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003589 Register second_reg = second.AsRegister<Register>();
3590 // 0x80000000/-1 triggers an arithmetic exception!
3591 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3592 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003593
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003594 __ cmpl(second_reg, Immediate(-1));
3595 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003596
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003597 // edx:eax <- sign-extended of eax
3598 __ cdq();
3599 // eax = quotient, edx = remainder
3600 __ idivl(second_reg);
3601 __ Bind(slow_path->GetExitLabel());
3602 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003603 break;
3604 }
3605
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003606 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003607 InvokeRuntimeCallingConvention calling_convention;
3608 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3609 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3610 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3611 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3612 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3613 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3614
3615 if (is_div) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003616 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003617 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003618 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003619 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003620 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003621 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003622 break;
3623 }
3624
3625 default:
3626 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3627 }
3628}
3629
Calin Juravle7c4954d2014-10-28 16:57:40 +00003630void LocationsBuilderX86::VisitDiv(HDiv* div) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003631 LocationSummary::CallKind call_kind = (div->GetResultType() == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003632 ? LocationSummary::kCallOnMainOnly
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003633 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01003634 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(div, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003635
Calin Juravle7c4954d2014-10-28 16:57:40 +00003636 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003637 case DataType::Type::kInt32: {
Calin Juravled0d48522014-11-04 16:40:20 +00003638 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003639 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003640 locations->SetOut(Location::SameAsFirstInput());
3641 // Intel uses edx:eax as the dividend.
3642 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003643 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3644 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3645 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003646 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003647 locations->AddTemp(Location::RequiresRegister());
3648 }
Calin Juravled0d48522014-11-04 16:40:20 +00003649 break;
3650 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003651 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003652 InvokeRuntimeCallingConvention calling_convention;
3653 locations->SetInAt(0, Location::RegisterPairLocation(
3654 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3655 locations->SetInAt(1, Location::RegisterPairLocation(
3656 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3657 // Runtime helper puts the result in EAX, EDX.
3658 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003659 break;
3660 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003661 case DataType::Type::kFloat32:
3662 case DataType::Type::kFloat64: {
Calin Juravle7c4954d2014-10-28 16:57:40 +00003663 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003664 if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3665 DCHECK(div->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003666 } else if (div->InputAt(1)->IsConstant()) {
3667 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003668 } else {
3669 locations->SetInAt(1, Location::Any());
3670 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003671 locations->SetOut(Location::SameAsFirstInput());
3672 break;
3673 }
3674
3675 default:
3676 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3677 }
3678}
3679
3680void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3681 LocationSummary* locations = div->GetLocations();
3682 Location first = locations->InAt(0);
3683 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003684
3685 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003686 case DataType::Type::kInt32:
3687 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003688 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003689 break;
3690 }
3691
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003692 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003693 if (second.IsFpuRegister()) {
3694 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3695 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3696 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003697 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003698 __ divss(first.AsFpuRegister<XmmRegister>(),
3699 codegen_->LiteralFloatAddress(
3700 const_area->GetConstant()->AsFloatConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003701 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003702 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3703 } else {
3704 DCHECK(second.IsStackSlot());
3705 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3706 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003707 break;
3708 }
3709
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003710 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003711 if (second.IsFpuRegister()) {
3712 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3713 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3714 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003715 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003716 __ divsd(first.AsFpuRegister<XmmRegister>(),
3717 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003718 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3719 const_area->GetBaseMethodAddress(),
3720 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003721 } else {
3722 DCHECK(second.IsDoubleStackSlot());
3723 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3724 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003725 break;
3726 }
3727
3728 default:
3729 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3730 }
3731}
3732
Calin Juravlebacfec32014-11-14 15:54:36 +00003733void LocationsBuilderX86::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003734 DataType::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003735
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003736 LocationSummary::CallKind call_kind = (rem->GetResultType() == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003737 ? LocationSummary::kCallOnMainOnly
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003738 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01003739 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003740
Calin Juravled2ec87d2014-12-08 14:24:46 +00003741 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003742 case DataType::Type::kInt32: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003743 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003744 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003745 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003746 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3747 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3748 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003749 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003750 locations->AddTemp(Location::RequiresRegister());
3751 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003752 break;
3753 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003754 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003755 InvokeRuntimeCallingConvention calling_convention;
3756 locations->SetInAt(0, Location::RegisterPairLocation(
3757 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3758 locations->SetInAt(1, Location::RegisterPairLocation(
3759 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3760 // Runtime helper puts the result in EAX, EDX.
3761 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3762 break;
3763 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003764 case DataType::Type::kFloat64:
3765 case DataType::Type::kFloat32: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003766 locations->SetInAt(0, Location::Any());
3767 locations->SetInAt(1, Location::Any());
3768 locations->SetOut(Location::RequiresFpuRegister());
3769 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003770 break;
3771 }
3772
3773 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003774 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003775 }
3776}
3777
3778void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003779 DataType::Type type = rem->GetResultType();
Calin Juravlebacfec32014-11-14 15:54:36 +00003780 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003781 case DataType::Type::kInt32:
3782 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003783 GenerateDivRemIntegral(rem);
3784 break;
3785 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003786 case DataType::Type::kFloat32:
3787 case DataType::Type::kFloat64: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003788 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003789 break;
3790 }
3791 default:
3792 LOG(FATAL) << "Unexpected rem type " << type;
3793 }
3794}
3795
Calin Juravled0d48522014-11-04 16:40:20 +00003796void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003797 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003798 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003799 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003800 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003801 case DataType::Type::kInt8:
3802 case DataType::Type::kUint16:
3803 case DataType::Type::kInt16:
3804 case DataType::Type::kInt32: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003805 locations->SetInAt(0, Location::Any());
3806 break;
3807 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003808 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003809 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
3810 if (!instruction->IsConstant()) {
3811 locations->AddTemp(Location::RequiresRegister());
3812 }
3813 break;
3814 }
3815 default:
3816 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3817 }
Calin Juravled0d48522014-11-04 16:40:20 +00003818}
3819
3820void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003821 SlowPathCode* slow_path = new (GetGraph()->GetAllocator()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003822 codegen_->AddSlowPath(slow_path);
3823
3824 LocationSummary* locations = instruction->GetLocations();
3825 Location value = locations->InAt(0);
3826
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003827 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003828 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003829 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003830 case DataType::Type::kInt8:
3831 case DataType::Type::kUint16:
3832 case DataType::Type::kInt16:
3833 case DataType::Type::kInt32: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003834 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003835 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003836 __ j(kEqual, slow_path->GetEntryLabel());
3837 } else if (value.IsStackSlot()) {
3838 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
3839 __ j(kEqual, slow_path->GetEntryLabel());
3840 } else {
3841 DCHECK(value.IsConstant()) << value;
3842 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01003843 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003844 }
3845 }
3846 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003847 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003848 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003849 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003850 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003851 __ movl(temp, value.AsRegisterPairLow<Register>());
3852 __ orl(temp, value.AsRegisterPairHigh<Register>());
3853 __ j(kEqual, slow_path->GetEntryLabel());
3854 } else {
3855 DCHECK(value.IsConstant()) << value;
3856 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3857 __ jmp(slow_path->GetEntryLabel());
3858 }
3859 }
3860 break;
3861 }
3862 default:
3863 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003864 }
Calin Juravled0d48522014-11-04 16:40:20 +00003865}
3866
Calin Juravle9aec02f2014-11-18 23:06:35 +00003867void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3868 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3869
3870 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003871 new (GetGraph()->GetAllocator()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003872
3873 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003874 case DataType::Type::kInt32:
3875 case DataType::Type::kInt64: {
Mark P Mendell73945692015-04-29 14:56:17 +00003876 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003877 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003878 // The shift count needs to be in CL or a constant.
3879 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003880 locations->SetOut(Location::SameAsFirstInput());
3881 break;
3882 }
3883 default:
3884 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3885 }
3886}
3887
3888void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3889 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3890
3891 LocationSummary* locations = op->GetLocations();
3892 Location first = locations->InAt(0);
3893 Location second = locations->InAt(1);
3894 DCHECK(first.Equals(locations->Out()));
3895
3896 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003897 case DataType::Type::kInt32: {
Mark P Mendell73945692015-04-29 14:56:17 +00003898 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003899 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003900 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003901 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003902 DCHECK_EQ(ECX, second_reg);
3903 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003904 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003905 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003906 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003907 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003908 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003909 }
3910 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003911 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003912 if (shift == 0) {
3913 return;
3914 }
3915 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003916 if (op->IsShl()) {
3917 __ shll(first_reg, imm);
3918 } else if (op->IsShr()) {
3919 __ sarl(first_reg, imm);
3920 } else {
3921 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003922 }
3923 }
3924 break;
3925 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003926 case DataType::Type::kInt64: {
Mark P Mendell73945692015-04-29 14:56:17 +00003927 if (second.IsRegister()) {
3928 Register second_reg = second.AsRegister<Register>();
3929 DCHECK_EQ(ECX, second_reg);
3930 if (op->IsShl()) {
3931 GenerateShlLong(first, second_reg);
3932 } else if (op->IsShr()) {
3933 GenerateShrLong(first, second_reg);
3934 } else {
3935 GenerateUShrLong(first, second_reg);
3936 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003937 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003938 // Shift by a constant.
Roland Levillain5b5b9312016-03-22 14:57:31 +00003939 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003940 // Nothing to do if the shift is 0, as the input is already the output.
3941 if (shift != 0) {
3942 if (op->IsShl()) {
3943 GenerateShlLong(first, shift);
3944 } else if (op->IsShr()) {
3945 GenerateShrLong(first, shift);
3946 } else {
3947 GenerateUShrLong(first, shift);
3948 }
3949 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003950 }
3951 break;
3952 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003953 default:
3954 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3955 }
3956}
3957
Mark P Mendell73945692015-04-29 14:56:17 +00003958void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3959 Register low = loc.AsRegisterPairLow<Register>();
3960 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003961 if (shift == 1) {
3962 // This is just an addition.
3963 __ addl(low, low);
3964 __ adcl(high, high);
3965 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003966 // Shift by 32 is easy. High gets low, and low gets 0.
3967 codegen_->EmitParallelMoves(
3968 loc.ToLow(),
3969 loc.ToHigh(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003970 DataType::Type::kInt32,
Mark P Mendell73945692015-04-29 14:56:17 +00003971 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3972 loc.ToLow(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003973 DataType::Type::kInt32);
Mark P Mendell73945692015-04-29 14:56:17 +00003974 } else if (shift > 32) {
3975 // Low part becomes 0. High part is low part << (shift-32).
3976 __ movl(high, low);
3977 __ shll(high, Immediate(shift - 32));
3978 __ xorl(low, low);
3979 } else {
3980 // Between 1 and 31.
3981 __ shld(high, low, Immediate(shift));
3982 __ shll(low, Immediate(shift));
3983 }
3984}
3985
Calin Juravle9aec02f2014-11-18 23:06:35 +00003986void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003987 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003988 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
3989 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
3990 __ testl(shifter, Immediate(32));
3991 __ j(kEqual, &done);
3992 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
3993 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
3994 __ Bind(&done);
3995}
3996
Mark P Mendell73945692015-04-29 14:56:17 +00003997void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
3998 Register low = loc.AsRegisterPairLow<Register>();
3999 Register high = loc.AsRegisterPairHigh<Register>();
4000 if (shift == 32) {
4001 // Need to copy the sign.
4002 DCHECK_NE(low, high);
4003 __ movl(low, high);
4004 __ sarl(high, Immediate(31));
4005 } else if (shift > 32) {
4006 DCHECK_NE(low, high);
4007 // High part becomes sign. Low part is shifted by shift - 32.
4008 __ movl(low, high);
4009 __ sarl(high, Immediate(31));
4010 __ sarl(low, Immediate(shift - 32));
4011 } else {
4012 // Between 1 and 31.
4013 __ shrd(low, high, Immediate(shift));
4014 __ sarl(high, Immediate(shift));
4015 }
4016}
4017
Calin Juravle9aec02f2014-11-18 23:06:35 +00004018void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004019 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004020 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4021 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
4022 __ testl(shifter, Immediate(32));
4023 __ j(kEqual, &done);
4024 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4025 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
4026 __ Bind(&done);
4027}
4028
Mark P Mendell73945692015-04-29 14:56:17 +00004029void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
4030 Register low = loc.AsRegisterPairLow<Register>();
4031 Register high = loc.AsRegisterPairHigh<Register>();
4032 if (shift == 32) {
4033 // Shift by 32 is easy. Low gets high, and high gets 0.
4034 codegen_->EmitParallelMoves(
4035 loc.ToHigh(),
4036 loc.ToLow(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004037 DataType::Type::kInt32,
Mark P Mendell73945692015-04-29 14:56:17 +00004038 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
4039 loc.ToHigh(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004040 DataType::Type::kInt32);
Mark P Mendell73945692015-04-29 14:56:17 +00004041 } else if (shift > 32) {
4042 // Low part is high >> (shift - 32). High part becomes 0.
4043 __ movl(low, high);
4044 __ shrl(low, Immediate(shift - 32));
4045 __ xorl(high, high);
4046 } else {
4047 // Between 1 and 31.
4048 __ shrd(low, high, Immediate(shift));
4049 __ shrl(high, Immediate(shift));
4050 }
4051}
4052
Calin Juravle9aec02f2014-11-18 23:06:35 +00004053void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004054 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004055 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4056 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
4057 __ testl(shifter, Immediate(32));
4058 __ j(kEqual, &done);
4059 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4060 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
4061 __ Bind(&done);
4062}
4063
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004064void LocationsBuilderX86::VisitRor(HRor* ror) {
4065 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004066 new (GetGraph()->GetAllocator()) LocationSummary(ror, LocationSummary::kNoCall);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004067
4068 switch (ror->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004069 case DataType::Type::kInt64:
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004070 // Add the temporary needed.
4071 locations->AddTemp(Location::RequiresRegister());
4072 FALLTHROUGH_INTENDED;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004073 case DataType::Type::kInt32:
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004074 locations->SetInAt(0, Location::RequiresRegister());
4075 // The shift count needs to be in CL (unless it is a constant).
4076 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
4077 locations->SetOut(Location::SameAsFirstInput());
4078 break;
4079 default:
4080 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4081 UNREACHABLE();
4082 }
4083}
4084
4085void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
4086 LocationSummary* locations = ror->GetLocations();
4087 Location first = locations->InAt(0);
4088 Location second = locations->InAt(1);
4089
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004090 if (ror->GetResultType() == DataType::Type::kInt32) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004091 Register first_reg = first.AsRegister<Register>();
4092 if (second.IsRegister()) {
4093 Register second_reg = second.AsRegister<Register>();
4094 __ rorl(first_reg, second_reg);
4095 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004096 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004097 __ rorl(first_reg, imm);
4098 }
4099 return;
4100 }
4101
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004102 DCHECK_EQ(ror->GetResultType(), DataType::Type::kInt64);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004103 Register first_reg_lo = first.AsRegisterPairLow<Register>();
4104 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
4105 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
4106 if (second.IsRegister()) {
4107 Register second_reg = second.AsRegister<Register>();
4108 DCHECK_EQ(second_reg, ECX);
4109 __ movl(temp_reg, first_reg_hi);
4110 __ shrd(first_reg_hi, first_reg_lo, second_reg);
4111 __ shrd(first_reg_lo, temp_reg, second_reg);
4112 __ movl(temp_reg, first_reg_hi);
4113 __ testl(second_reg, Immediate(32));
4114 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
4115 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
4116 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004117 int32_t shift_amt = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004118 if (shift_amt == 0) {
4119 // Already fine.
4120 return;
4121 }
4122 if (shift_amt == 32) {
4123 // Just swap.
4124 __ movl(temp_reg, first_reg_lo);
4125 __ movl(first_reg_lo, first_reg_hi);
4126 __ movl(first_reg_hi, temp_reg);
4127 return;
4128 }
4129
4130 Immediate imm(shift_amt);
4131 // Save the constents of the low value.
4132 __ movl(temp_reg, first_reg_lo);
4133
4134 // Shift right into low, feeding bits from high.
4135 __ shrd(first_reg_lo, first_reg_hi, imm);
4136
4137 // Shift right into high, feeding bits from the original low.
4138 __ shrd(first_reg_hi, temp_reg, imm);
4139
4140 // Swap if needed.
4141 if (shift_amt > 32) {
4142 __ movl(temp_reg, first_reg_lo);
4143 __ movl(first_reg_lo, first_reg_hi);
4144 __ movl(first_reg_hi, temp_reg);
4145 }
4146 }
4147}
4148
Calin Juravle9aec02f2014-11-18 23:06:35 +00004149void LocationsBuilderX86::VisitShl(HShl* shl) {
4150 HandleShift(shl);
4151}
4152
4153void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
4154 HandleShift(shl);
4155}
4156
4157void LocationsBuilderX86::VisitShr(HShr* shr) {
4158 HandleShift(shr);
4159}
4160
4161void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
4162 HandleShift(shr);
4163}
4164
4165void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
4166 HandleShift(ushr);
4167}
4168
4169void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
4170 HandleShift(ushr);
4171}
4172
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004173void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004174 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
4175 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004176 locations->SetOut(Location::RegisterLocation(EAX));
David Brazdil6de19382016-01-08 17:37:10 +00004177 if (instruction->IsStringAlloc()) {
4178 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4179 } else {
4180 InvokeRuntimeCallingConvention calling_convention;
4181 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00004182 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004183}
4184
4185void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004186 // Note: if heap poisoning is enabled, the entry point takes cares
4187 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004188 if (instruction->IsStringAlloc()) {
4189 // String is allocated through StringFactory. Call NewEmptyString entry point.
4190 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07004191 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004192 __ fs()->movl(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString)));
4193 __ call(Address(temp, code_offset.Int32Value()));
4194 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4195 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01004196 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00004197 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00004198 DCHECK(!codegen_->IsLeafMethod());
4199 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004200}
4201
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004202void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004203 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
4204 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004205 locations->SetOut(Location::RegisterLocation(EAX));
4206 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004207 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4208 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004209}
4210
4211void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004212 // Note: if heap poisoning is enabled, the entry point takes cares
4213 // of poisoning the reference.
Nicolas Geoffrayd0958442017-01-30 14:57:16 +00004214 QuickEntrypointEnum entrypoint =
4215 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
4216 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004217 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004218 DCHECK(!codegen_->IsLeafMethod());
4219}
4220
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004221void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004222 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004223 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004224 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4225 if (location.IsStackSlot()) {
4226 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4227 } else if (location.IsDoubleStackSlot()) {
4228 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004229 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004230 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004231}
4232
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004233void InstructionCodeGeneratorX86::VisitParameterValue(
4234 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4235}
4236
4237void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
4238 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004239 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004240 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4241}
4242
4243void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004244}
4245
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004246void LocationsBuilderX86::VisitClassTableGet(HClassTableGet* instruction) {
4247 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004248 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004249 locations->SetInAt(0, Location::RequiresRegister());
4250 locations->SetOut(Location::RequiresRegister());
4251}
4252
4253void InstructionCodeGeneratorX86::VisitClassTableGet(HClassTableGet* instruction) {
4254 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004255 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004256 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004257 instruction->GetIndex(), kX86PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004258 __ movl(locations->Out().AsRegister<Register>(),
4259 Address(locations->InAt(0).AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004260 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004261 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004262 instruction->GetIndex(), kX86PointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004263 __ movl(locations->Out().AsRegister<Register>(),
4264 Address(locations->InAt(0).AsRegister<Register>(),
4265 mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
4266 // temp = temp->GetImtEntryAt(method_offset);
4267 __ movl(locations->Out().AsRegister<Register>(),
4268 Address(locations->Out().AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004269 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004270}
4271
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004272void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004273 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004274 new (GetGraph()->GetAllocator()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004275 locations->SetInAt(0, Location::RequiresRegister());
4276 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004277}
4278
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004279void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
4280 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01004281 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004282 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01004283 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004284 switch (not_->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004285 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004286 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004287 break;
4288
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004289 case DataType::Type::kInt64:
Roland Levillain70566432014-10-24 16:20:17 +01004290 __ notl(out.AsRegisterPairLow<Register>());
4291 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004292 break;
4293
4294 default:
4295 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4296 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004297}
4298
David Brazdil66d126e2015-04-03 16:02:44 +01004299void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
4300 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004301 new (GetGraph()->GetAllocator()) LocationSummary(bool_not, LocationSummary::kNoCall);
David Brazdil66d126e2015-04-03 16:02:44 +01004302 locations->SetInAt(0, Location::RequiresRegister());
4303 locations->SetOut(Location::SameAsFirstInput());
4304}
4305
4306void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004307 LocationSummary* locations = bool_not->GetLocations();
4308 Location in = locations->InAt(0);
4309 Location out = locations->Out();
4310 DCHECK(in.Equals(out));
4311 __ xorl(out.AsRegister<Register>(), Immediate(1));
4312}
4313
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004314void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004315 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004316 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004317 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004318 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004319 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004320 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004321 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004322 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004323 case DataType::Type::kInt32:
4324 case DataType::Type::kInt64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00004325 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00004326 locations->SetInAt(1, Location::Any());
4327 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4328 break;
4329 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004330 case DataType::Type::kFloat32:
4331 case DataType::Type::kFloat64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00004332 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004333 if (compare->InputAt(1)->IsX86LoadFromConstantTable()) {
4334 DCHECK(compare->InputAt(1)->IsEmittedAtUseSite());
4335 } else if (compare->InputAt(1)->IsConstant()) {
4336 locations->SetInAt(1, Location::RequiresFpuRegister());
4337 } else {
4338 locations->SetInAt(1, Location::Any());
4339 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004340 locations->SetOut(Location::RequiresRegister());
4341 break;
4342 }
4343 default:
4344 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4345 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004346}
4347
4348void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004349 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004350 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004351 Location left = locations->InAt(0);
4352 Location right = locations->InAt(1);
4353
Mark Mendell0c9497d2015-08-21 09:30:05 -04004354 NearLabel less, greater, done;
Aart Bika19616e2016-02-01 18:57:58 -08004355 Condition less_cond = kLess;
4356
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004357 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004358 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004359 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004360 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004361 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004362 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004363 case DataType::Type::kInt32: {
Roland Levillain0b671c02016-08-19 12:02:34 +01004364 codegen_->GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08004365 break;
4366 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004367 case DataType::Type::kInt64: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004368 Register left_low = left.AsRegisterPairLow<Register>();
4369 Register left_high = left.AsRegisterPairHigh<Register>();
4370 int32_t val_low = 0;
4371 int32_t val_high = 0;
4372 bool right_is_const = false;
4373
4374 if (right.IsConstant()) {
4375 DCHECK(right.GetConstant()->IsLongConstant());
4376 right_is_const = true;
4377 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
4378 val_low = Low32Bits(val);
4379 val_high = High32Bits(val);
4380 }
4381
Calin Juravleddb7df22014-11-25 20:56:51 +00004382 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004383 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004384 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004385 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004386 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004387 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004388 codegen_->Compare32BitValue(left_high, val_high);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004389 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004390 __ j(kLess, &less); // Signed compare.
4391 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004392 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004393 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004394 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004395 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004396 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004397 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004398 codegen_->Compare32BitValue(left_low, val_low);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004399 }
Aart Bika19616e2016-02-01 18:57:58 -08004400 less_cond = kBelow; // for CF (unsigned).
Calin Juravleddb7df22014-11-25 20:56:51 +00004401 break;
4402 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004403 case DataType::Type::kFloat32: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004404 GenerateFPCompare(left, right, compare, false);
Calin Juravleddb7df22014-11-25 20:56:51 +00004405 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004406 less_cond = kBelow; // for CF (floats).
Calin Juravleddb7df22014-11-25 20:56:51 +00004407 break;
4408 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004409 case DataType::Type::kFloat64: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004410 GenerateFPCompare(left, right, compare, true);
Calin Juravleddb7df22014-11-25 20:56:51 +00004411 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004412 less_cond = kBelow; // for CF (floats).
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004413 break;
4414 }
4415 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004416 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004417 }
Aart Bika19616e2016-02-01 18:57:58 -08004418
Calin Juravleddb7df22014-11-25 20:56:51 +00004419 __ movl(out, Immediate(0));
4420 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08004421 __ j(less_cond, &less);
Calin Juravleddb7df22014-11-25 20:56:51 +00004422
4423 __ Bind(&greater);
4424 __ movl(out, Immediate(1));
4425 __ jmp(&done);
4426
4427 __ Bind(&less);
4428 __ movl(out, Immediate(-1));
4429
4430 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004431}
4432
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004433void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004434 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004435 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004436 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004437 locations->SetInAt(i, Location::Any());
4438 }
4439 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004440}
4441
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004442void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004443 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004444}
4445
Roland Levillain7c1559a2015-12-15 10:55:36 +00004446void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004447 /*
4448 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
4449 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
4450 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4451 */
4452 switch (kind) {
4453 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004454 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004455 break;
4456 }
4457 case MemBarrierKind::kAnyStore:
4458 case MemBarrierKind::kLoadAny:
4459 case MemBarrierKind::kStoreStore: {
4460 // nop
4461 break;
4462 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004463 case MemBarrierKind::kNTStoreStore:
4464 // Non-Temporal Store/Store needs an explicit fence.
4465 MemoryFence(/* non-temporal */ true);
4466 break;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004467 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004468}
4469
Vladimir Markodc151b22015-10-15 18:02:30 +01004470HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
4471 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004472 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004473 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004474}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004475
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004476Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4477 Register temp) {
4478 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Markoc53c0792015-11-19 15:48:33 +00004479 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004480 if (!invoke->GetLocations()->Intrinsified()) {
4481 return location.AsRegister<Register>();
4482 }
4483 // For intrinsics we allow any location, so it may be on the stack.
4484 if (!location.IsRegister()) {
4485 __ movl(temp, Address(ESP, location.GetStackIndex()));
4486 return temp;
4487 }
4488 // For register locations, check if the register was saved. If so, get it from the stack.
4489 // Note: There is a chance that the register was saved but not overwritten, so we could
4490 // save one load. However, since this is just an intrinsic slow path we prefer this
4491 // simple and more robust approach rather that trying to determine if that's the case.
4492 SlowPathCode* slow_path = GetCurrentSlowPath();
Vladimir Marko4ee8e292017-06-02 15:39:30 +00004493 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
4494 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4495 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4496 __ movl(temp, Address(ESP, stack_offset));
4497 return temp;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004498 }
4499 return location.AsRegister<Register>();
4500}
4501
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004502void CodeGeneratorX86::GenerateStaticOrDirectCall(
4503 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Vladimir Marko58155012015-08-19 12:49:41 +00004504 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4505 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004506 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00004507 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004508 uint32_t offset =
4509 GetThreadOffset<kX86PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
4510 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004511 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004512 }
Vladimir Marko58155012015-08-19 12:49:41 +00004513 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004514 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004515 break;
Vladimir Marko65979462017-05-19 17:25:12 +01004516 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
4517 DCHECK(GetCompilerOptions().IsBootImage());
4518 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4519 temp.AsRegister<Register>());
4520 __ leal(temp.AsRegister<Register>(), Address(base_reg, CodeGeneratorX86::kDummy32BitOffset));
4521 RecordBootMethodPatch(invoke);
4522 break;
4523 }
Vladimir Marko58155012015-08-19 12:49:41 +00004524 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4525 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
4526 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004527 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004528 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4529 temp.AsRegister<Register>());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004530 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004531 // Bind a new fixup label at the end of the "movl" insn.
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004532 __ Bind(NewMethodBssEntryPatch(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004533 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress(),
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004534 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex())));
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004535 break;
4536 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004537 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
4538 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
4539 return; // No code pointer retrieval; the runtime performs the call directly.
Vladimir Marko9b688a02015-05-06 14:12:42 +01004540 }
Vladimir Marko58155012015-08-19 12:49:41 +00004541 }
4542
4543 switch (invoke->GetCodePtrLocation()) {
4544 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4545 __ call(GetFrameEntryLabel());
4546 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004547 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4548 // (callee_method + offset_of_quick_compiled_code)()
4549 __ call(Address(callee_method.AsRegister<Register>(),
4550 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07004551 kX86PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004552 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04004553 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004554 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Mark Mendell09ed1a32015-03-25 08:30:06 -04004555
4556 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04004557}
4558
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004559void CodeGeneratorX86::GenerateVirtualCall(
4560 HInvokeVirtual* invoke, Location temp_in, SlowPathCode* slow_path) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004561 Register temp = temp_in.AsRegister<Register>();
4562 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4563 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004564
4565 // Use the calling convention instead of the location of the receiver, as
4566 // intrinsics may have put the receiver in a different register. In the intrinsics
4567 // slow path, the arguments have been moved to the right place, so here we are
4568 // guaranteed that the receiver is the first register of the calling convention.
4569 InvokeDexCallingConvention calling_convention;
4570 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004571 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004572 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004573 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004574 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004575 // Instead of simply (possibly) unpoisoning `temp` here, we should
4576 // emit a read barrier for the previous class reference load.
4577 // However this is not required in practice, as this is an
4578 // intermediate/temporary reference and because the current
4579 // concurrent copying collector keeps the from-space memory
4580 // intact/accessible until the end of the marking phase (the
4581 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004582 __ MaybeUnpoisonHeapReference(temp);
4583 // temp = temp->GetMethodAt(method_offset);
4584 __ movl(temp, Address(temp, method_offset));
4585 // call temp->GetEntryPoint();
4586 __ call(Address(
Andreas Gampe542451c2016-07-26 09:02:02 -07004587 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004588 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004589}
4590
Vladimir Marko65979462017-05-19 17:25:12 +01004591void CodeGeneratorX86::RecordBootMethodPatch(HInvokeStaticOrDirect* invoke) {
4592 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
4593 HX86ComputeBaseMethodAddress* address =
4594 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
4595 boot_image_method_patches_.emplace_back(address,
4596 *invoke->GetTargetMethod().dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07004597 invoke->GetTargetMethod().index);
Vladimir Marko65979462017-05-19 17:25:12 +01004598 __ Bind(&boot_image_method_patches_.back().label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004599}
4600
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004601Label* CodeGeneratorX86::NewMethodBssEntryPatch(
4602 HX86ComputeBaseMethodAddress* method_address,
4603 MethodReference target_method) {
4604 // Add the patch entry and bind its label at the end of the instruction.
4605 method_bss_entry_patches_.emplace_back(method_address,
4606 *target_method.dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07004607 target_method.index);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004608 return &method_bss_entry_patches_.back().label;
4609}
4610
Vladimir Marko1998cd02017-01-13 13:02:58 +00004611void CodeGeneratorX86::RecordBootTypePatch(HLoadClass* load_class) {
Vladimir Marko00916b92017-05-17 17:45:45 +01004612 HX86ComputeBaseMethodAddress* address = load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004613 boot_image_type_patches_.emplace_back(address,
4614 load_class->GetDexFile(),
Vladimir Marko1998cd02017-01-13 13:02:58 +00004615 load_class->GetTypeIndex().index_);
4616 __ Bind(&boot_image_type_patches_.back().label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004617}
4618
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004619Label* CodeGeneratorX86::NewTypeBssEntryPatch(HLoadClass* load_class) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004620 HX86ComputeBaseMethodAddress* address =
4621 load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
4622 type_bss_entry_patches_.emplace_back(
4623 address, load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00004624 return &type_bss_entry_patches_.back().label;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004625}
4626
Vladimir Marko65979462017-05-19 17:25:12 +01004627void CodeGeneratorX86::RecordBootStringPatch(HLoadString* load_string) {
Vladimir Marko65979462017-05-19 17:25:12 +01004628 HX86ComputeBaseMethodAddress* address = load_string->InputAt(0)->AsX86ComputeBaseMethodAddress();
4629 string_patches_.emplace_back(address,
4630 load_string->GetDexFile(),
4631 load_string->GetStringIndex().index_);
4632 __ Bind(&string_patches_.back().label);
4633}
4634
Vladimir Markoaad75c62016-10-03 08:46:48 +00004635Label* CodeGeneratorX86::NewStringBssEntryPatch(HLoadString* load_string) {
4636 DCHECK(!GetCompilerOptions().IsBootImage());
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004637 HX86ComputeBaseMethodAddress* address =
4638 load_string->InputAt(0)->AsX86ComputeBaseMethodAddress();
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004639 string_bss_entry_patches_.emplace_back(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004640 address, load_string->GetDexFile(), load_string->GetStringIndex().index_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004641 return &string_bss_entry_patches_.back().label;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004642}
4643
Vladimir Markoaad75c62016-10-03 08:46:48 +00004644// The label points to the end of the "movl" or another instruction but the literal offset
4645// for method patch needs to point to the embedded constant which occupies the last 4 bytes.
4646constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
4647
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004648template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +00004649inline void CodeGeneratorX86::EmitPcRelativeLinkerPatches(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004650 const ArenaDeque<X86PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004651 ArenaVector<linker::LinkerPatch>* linker_patches) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004652 for (const X86PcRelativePatchInfo& info : infos) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00004653 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004654 linker_patches->push_back(Factory(
4655 literal_offset, &info.dex_file, GetMethodAddressOffset(info.method_address), info.index));
Vladimir Markoaad75c62016-10-03 08:46:48 +00004656 }
4657}
4658
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004659void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Marko58155012015-08-19 12:49:41 +00004660 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004661 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01004662 boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004663 method_bss_entry_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +00004664 boot_image_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01004665 type_bss_entry_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004666 string_patches_.size() +
4667 string_bss_entry_patches_.size();
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004668 linker_patches->reserve(size);
Vladimir Marko764d4542017-05-16 10:31:41 +01004669 if (GetCompilerOptions().IsBootImage()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004670 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
4671 boot_image_method_patches_, linker_patches);
4672 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
4673 boot_image_type_patches_, linker_patches);
4674 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
4675 string_patches_, linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004676 } else {
Vladimir Marko65979462017-05-19 17:25:12 +01004677 DCHECK(boot_image_method_patches_.empty());
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004678 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeClassTablePatch>(
4679 boot_image_type_patches_, linker_patches);
4680 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringInternTablePatch>(
4681 string_patches_, linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004682 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004683 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
4684 method_bss_entry_patches_, linker_patches);
4685 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
4686 type_bss_entry_patches_, linker_patches);
4687 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
4688 string_bss_entry_patches_, linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00004689 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00004690}
4691
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004692void CodeGeneratorX86::MarkGCCard(Register temp,
4693 Register card,
4694 Register object,
4695 Register value,
4696 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004697 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004698 if (value_can_be_null) {
4699 __ testl(value, value);
4700 __ j(kEqual, &is_null);
4701 }
Andreas Gampe542451c2016-07-26 09:02:02 -07004702 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86PointerSize>().Int32Value()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004703 __ movl(temp, object);
4704 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004705 __ movb(Address(temp, card, TIMES_1, 0),
4706 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004707 if (value_can_be_null) {
4708 __ Bind(&is_null);
4709 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004710}
4711
Calin Juravle52c48962014-12-16 17:02:57 +00004712void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4713 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00004714
4715 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004716 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004717 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004718 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
4719 kEmitCompilerReadBarrier
4720 ? LocationSummary::kCallOnSlowPath
4721 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004722 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004723 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004724 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004725 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004726
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004727 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004728 locations->SetOut(Location::RequiresFpuRegister());
4729 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004730 // The output overlaps in case of long: we don't want the low move
4731 // to overwrite the object's location. Likewise, in the case of
4732 // an object field get with read barriers enabled, we do not want
4733 // the move to overwrite the object's location, as we need it to emit
4734 // the read barrier.
4735 locations->SetOut(
4736 Location::RequiresRegister(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004737 (object_field_get_with_read_barrier || instruction->GetType() == DataType::Type::kInt64) ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00004738 Location::kOutputOverlap :
4739 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004740 }
Calin Juravle52c48962014-12-16 17:02:57 +00004741
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004742 if (field_info.IsVolatile() && (field_info.GetFieldType() == DataType::Type::kInt64)) {
Calin Juravle52c48962014-12-16 17:02:57 +00004743 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00004744 // So we use an XMM register as a temp to achieve atomicity (first
4745 // load the temp into the XMM and then copy the XMM into the
4746 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00004747 locations->AddTemp(Location::RequiresFpuRegister());
4748 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004749}
4750
Calin Juravle52c48962014-12-16 17:02:57 +00004751void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
4752 const FieldInfo& field_info) {
4753 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004754
Calin Juravle52c48962014-12-16 17:02:57 +00004755 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004756 Location base_loc = locations->InAt(0);
4757 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004758 Location out = locations->Out();
4759 bool is_volatile = field_info.IsVolatile();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004760 DataType::Type field_type = field_info.GetFieldType();
Calin Juravle52c48962014-12-16 17:02:57 +00004761 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4762
4763 switch (field_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004764 case DataType::Type::kBool:
4765 case DataType::Type::kUint8: {
Calin Juravle52c48962014-12-16 17:02:57 +00004766 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004767 break;
4768 }
4769
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004770 case DataType::Type::kInt8: {
Calin Juravle52c48962014-12-16 17:02:57 +00004771 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004772 break;
4773 }
4774
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004775 case DataType::Type::kUint16: {
4776 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004777 break;
4778 }
4779
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004780 case DataType::Type::kInt16: {
4781 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004782 break;
4783 }
4784
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004785 case DataType::Type::kInt32:
Calin Juravle52c48962014-12-16 17:02:57 +00004786 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004787 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00004788
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004789 case DataType::Type::kReference: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004790 // /* HeapReference<Object> */ out = *(base + offset)
4791 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004792 // Note that a potential implicit null check is handled in this
4793 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4794 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00004795 instruction, out, base, offset, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00004796 if (is_volatile) {
4797 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4798 }
4799 } else {
4800 __ movl(out.AsRegister<Register>(), Address(base, offset));
4801 codegen_->MaybeRecordImplicitNullCheck(instruction);
4802 if (is_volatile) {
4803 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4804 }
4805 // If read barriers are enabled, emit read barriers other than
4806 // Baker's using a slow path (and also unpoison the loaded
4807 // reference, if heap poisoning is enabled).
4808 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4809 }
4810 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004811 }
4812
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004813 case DataType::Type::kInt64: {
Calin Juravle52c48962014-12-16 17:02:57 +00004814 if (is_volatile) {
4815 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4816 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004817 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004818 __ movd(out.AsRegisterPairLow<Register>(), temp);
4819 __ psrlq(temp, Immediate(32));
4820 __ movd(out.AsRegisterPairHigh<Register>(), temp);
4821 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004822 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00004823 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004824 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004825 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
4826 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004827 break;
4828 }
4829
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004830 case DataType::Type::kFloat32: {
Calin Juravle52c48962014-12-16 17:02:57 +00004831 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004832 break;
4833 }
4834
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004835 case DataType::Type::kFloat64: {
Calin Juravle52c48962014-12-16 17:02:57 +00004836 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004837 break;
4838 }
4839
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004840 case DataType::Type::kVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004841 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004842 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004843 }
Calin Juravle52c48962014-12-16 17:02:57 +00004844
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004845 if (field_type == DataType::Type::kReference || field_type == DataType::Type::kInt64) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004846 // Potential implicit null checks, in the case of reference or
4847 // long fields, are handled in the previous switch statement.
4848 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004849 codegen_->MaybeRecordImplicitNullCheck(instruction);
4850 }
4851
Calin Juravle52c48962014-12-16 17:02:57 +00004852 if (is_volatile) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004853 if (field_type == DataType::Type::kReference) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004854 // Memory barriers, in the case of references, are also handled
4855 // in the previous switch statement.
4856 } else {
4857 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4858 }
Roland Levillain4d027112015-07-01 15:41:14 +01004859 }
Calin Juravle52c48962014-12-16 17:02:57 +00004860}
4861
4862void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4863 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4864
4865 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004866 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00004867 locations->SetInAt(0, Location::RequiresRegister());
4868 bool is_volatile = field_info.IsVolatile();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004869 DataType::Type field_type = field_info.GetFieldType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004870 bool is_byte_type = DataType::Size(field_type) == 1u;
Calin Juravle52c48962014-12-16 17:02:57 +00004871
4872 // The register allocator does not support multiple
4873 // inputs that die at entry with one in a specific register.
4874 if (is_byte_type) {
4875 // Ensure the value is in a byte register.
4876 locations->SetInAt(1, Location::RegisterLocation(EAX));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004877 } else if (DataType::IsFloatingPointType(field_type)) {
4878 if (is_volatile && field_type == DataType::Type::kFloat64) {
Mark Mendell81489372015-11-04 11:30:41 -05004879 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4880 locations->SetInAt(1, Location::RequiresFpuRegister());
4881 } else {
4882 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4883 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004884 } else if (is_volatile && field_type == DataType::Type::kInt64) {
Mark Mendell81489372015-11-04 11:30:41 -05004885 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00004886 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004887
Calin Juravle52c48962014-12-16 17:02:57 +00004888 // 64bits value can be atomically written to an address with movsd and an XMM register.
4889 // We need two XMM registers because there's no easier way to (bit) copy a register pair
4890 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
4891 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
4892 // isolated cases when we need this it isn't worth adding the extra complexity.
4893 locations->AddTemp(Location::RequiresFpuRegister());
4894 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004895 } else {
4896 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4897
4898 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4899 // Temporary registers for the write barrier.
4900 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
4901 // Ensure the card is in a byte register.
4902 locations->AddTemp(Location::RegisterLocation(ECX));
4903 }
Calin Juravle52c48962014-12-16 17:02:57 +00004904 }
4905}
4906
4907void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004908 const FieldInfo& field_info,
4909 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004910 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4911
4912 LocationSummary* locations = instruction->GetLocations();
4913 Register base = locations->InAt(0).AsRegister<Register>();
4914 Location value = locations->InAt(1);
4915 bool is_volatile = field_info.IsVolatile();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004916 DataType::Type field_type = field_info.GetFieldType();
Calin Juravle52c48962014-12-16 17:02:57 +00004917 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004918 bool needs_write_barrier =
4919 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004920
4921 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004922 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004923 }
4924
Mark Mendell81489372015-11-04 11:30:41 -05004925 bool maybe_record_implicit_null_check_done = false;
4926
Calin Juravle52c48962014-12-16 17:02:57 +00004927 switch (field_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004928 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004929 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004930 case DataType::Type::kInt8: {
Calin Juravle52c48962014-12-16 17:02:57 +00004931 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
4932 break;
4933 }
4934
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004935 case DataType::Type::kUint16:
4936 case DataType::Type::kInt16: {
Mark Mendell81489372015-11-04 11:30:41 -05004937 if (value.IsConstant()) {
Nicolas Geoffray78612082017-07-24 14:18:53 +01004938 __ movw(Address(base, offset),
4939 Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Mark Mendell81489372015-11-04 11:30:41 -05004940 } else {
4941 __ movw(Address(base, offset), value.AsRegister<Register>());
4942 }
Calin Juravle52c48962014-12-16 17:02:57 +00004943 break;
4944 }
4945
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004946 case DataType::Type::kInt32:
4947 case DataType::Type::kReference: {
Roland Levillain4d027112015-07-01 15:41:14 +01004948 if (kPoisonHeapReferences && needs_write_barrier) {
4949 // Note that in the case where `value` is a null reference,
4950 // we do not enter this block, as the reference does not
4951 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004952 DCHECK_EQ(field_type, DataType::Type::kReference);
Roland Levillain4d027112015-07-01 15:41:14 +01004953 Register temp = locations->GetTemp(0).AsRegister<Register>();
4954 __ movl(temp, value.AsRegister<Register>());
4955 __ PoisonHeapReference(temp);
4956 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05004957 } else if (value.IsConstant()) {
4958 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4959 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01004960 } else {
Nicolas Geoffray03971632016-03-17 10:44:24 +00004961 DCHECK(value.IsRegister()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01004962 __ movl(Address(base, offset), value.AsRegister<Register>());
4963 }
Calin Juravle52c48962014-12-16 17:02:57 +00004964 break;
4965 }
4966
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004967 case DataType::Type::kInt64: {
Calin Juravle52c48962014-12-16 17:02:57 +00004968 if (is_volatile) {
4969 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4970 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
4971 __ movd(temp1, value.AsRegisterPairLow<Register>());
4972 __ movd(temp2, value.AsRegisterPairHigh<Register>());
4973 __ punpckldq(temp1, temp2);
4974 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00004975 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05004976 } else if (value.IsConstant()) {
4977 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4978 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4979 codegen_->MaybeRecordImplicitNullCheck(instruction);
4980 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00004981 } else {
4982 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004983 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004984 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
4985 }
Mark Mendell81489372015-11-04 11:30:41 -05004986 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00004987 break;
4988 }
4989
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004990 case DataType::Type::kFloat32: {
Mark Mendell81489372015-11-04 11:30:41 -05004991 if (value.IsConstant()) {
4992 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4993 __ movl(Address(base, offset), Immediate(v));
4994 } else {
4995 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4996 }
Calin Juravle52c48962014-12-16 17:02:57 +00004997 break;
4998 }
4999
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005000 case DataType::Type::kFloat64: {
Mark Mendell81489372015-11-04 11:30:41 -05005001 if (value.IsConstant()) {
5002 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
5003 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
5004 codegen_->MaybeRecordImplicitNullCheck(instruction);
5005 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
5006 maybe_record_implicit_null_check_done = true;
5007 } else {
5008 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
5009 }
Calin Juravle52c48962014-12-16 17:02:57 +00005010 break;
5011 }
5012
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005013 case DataType::Type::kVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00005014 LOG(FATAL) << "Unreachable type " << field_type;
5015 UNREACHABLE();
5016 }
5017
Mark Mendell81489372015-11-04 11:30:41 -05005018 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005019 codegen_->MaybeRecordImplicitNullCheck(instruction);
5020 }
5021
Roland Levillain4d027112015-07-01 15:41:14 +01005022 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005023 Register temp = locations->GetTemp(0).AsRegister<Register>();
5024 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005025 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00005026 }
5027
Calin Juravle52c48962014-12-16 17:02:57 +00005028 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005029 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00005030 }
5031}
5032
5033void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5034 HandleFieldGet(instruction, instruction->GetFieldInfo());
5035}
5036
5037void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5038 HandleFieldGet(instruction, instruction->GetFieldInfo());
5039}
5040
5041void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5042 HandleFieldSet(instruction, instruction->GetFieldInfo());
5043}
5044
5045void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005046 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005047}
5048
5049void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5050 HandleFieldSet(instruction, instruction->GetFieldInfo());
5051}
5052
5053void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005054 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005055}
5056
5057void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5058 HandleFieldGet(instruction, instruction->GetFieldInfo());
5059}
5060
5061void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5062 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005063}
5064
Calin Juravlee460d1d2015-09-29 04:52:17 +01005065void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
5066 HUnresolvedInstanceFieldGet* instruction) {
5067 FieldAccessCallingConventionX86 calling_convention;
5068 codegen_->CreateUnresolvedFieldLocationSummary(
5069 instruction, instruction->GetFieldType(), calling_convention);
5070}
5071
5072void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
5073 HUnresolvedInstanceFieldGet* instruction) {
5074 FieldAccessCallingConventionX86 calling_convention;
5075 codegen_->GenerateUnresolvedFieldAccess(instruction,
5076 instruction->GetFieldType(),
5077 instruction->GetFieldIndex(),
5078 instruction->GetDexPc(),
5079 calling_convention);
5080}
5081
5082void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
5083 HUnresolvedInstanceFieldSet* instruction) {
5084 FieldAccessCallingConventionX86 calling_convention;
5085 codegen_->CreateUnresolvedFieldLocationSummary(
5086 instruction, instruction->GetFieldType(), calling_convention);
5087}
5088
5089void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
5090 HUnresolvedInstanceFieldSet* instruction) {
5091 FieldAccessCallingConventionX86 calling_convention;
5092 codegen_->GenerateUnresolvedFieldAccess(instruction,
5093 instruction->GetFieldType(),
5094 instruction->GetFieldIndex(),
5095 instruction->GetDexPc(),
5096 calling_convention);
5097}
5098
5099void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
5100 HUnresolvedStaticFieldGet* instruction) {
5101 FieldAccessCallingConventionX86 calling_convention;
5102 codegen_->CreateUnresolvedFieldLocationSummary(
5103 instruction, instruction->GetFieldType(), calling_convention);
5104}
5105
5106void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
5107 HUnresolvedStaticFieldGet* instruction) {
5108 FieldAccessCallingConventionX86 calling_convention;
5109 codegen_->GenerateUnresolvedFieldAccess(instruction,
5110 instruction->GetFieldType(),
5111 instruction->GetFieldIndex(),
5112 instruction->GetDexPc(),
5113 calling_convention);
5114}
5115
5116void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
5117 HUnresolvedStaticFieldSet* instruction) {
5118 FieldAccessCallingConventionX86 calling_convention;
5119 codegen_->CreateUnresolvedFieldLocationSummary(
5120 instruction, instruction->GetFieldType(), calling_convention);
5121}
5122
5123void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
5124 HUnresolvedStaticFieldSet* instruction) {
5125 FieldAccessCallingConventionX86 calling_convention;
5126 codegen_->GenerateUnresolvedFieldAccess(instruction,
5127 instruction->GetFieldType(),
5128 instruction->GetFieldIndex(),
5129 instruction->GetDexPc(),
5130 calling_convention);
5131}
5132
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005133void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005134 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5135 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
5136 ? Location::RequiresRegister()
5137 : Location::Any();
5138 locations->SetInAt(0, loc);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005139}
5140
Calin Juravle2ae48182016-03-16 14:05:09 +00005141void CodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
5142 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005143 return;
5144 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005145 LocationSummary* locations = instruction->GetLocations();
5146 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005147
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005148 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00005149 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005150}
5151
Calin Juravle2ae48182016-03-16 14:05:09 +00005152void CodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005153 SlowPathCode* slow_path = new (GetGraph()->GetAllocator()) NullCheckSlowPathX86(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005154 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005155
5156 LocationSummary* locations = instruction->GetLocations();
5157 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005158
5159 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04005160 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005161 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005162 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005163 } else {
5164 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00005165 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005166 __ jmp(slow_path->GetEntryLabel());
5167 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005168 }
5169 __ j(kEqual, slow_path->GetEntryLabel());
5170}
5171
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005172void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005173 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005174}
5175
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005176void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005177 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005178 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005179 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005180 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
5181 object_array_get_with_read_barrier
5182 ? LocationSummary::kCallOnSlowPath
5183 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005184 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005185 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005186 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005187 locations->SetInAt(0, Location::RequiresRegister());
5188 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005189 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005190 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5191 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005192 // The output overlaps in case of long: we don't want the low move
5193 // to overwrite the array's location. Likewise, in the case of an
5194 // object array get with read barriers enabled, we do not want the
5195 // move to overwrite the array's location, as we need it to emit
5196 // the read barrier.
5197 locations->SetOut(
5198 Location::RequiresRegister(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005199 (instruction->GetType() == DataType::Type::kInt64 || object_array_get_with_read_barrier)
5200 ? Location::kOutputOverlap
5201 : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005202 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005203}
5204
5205void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
5206 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005207 Location obj_loc = locations->InAt(0);
5208 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005209 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005210 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005211 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005212
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005213 DataType::Type type = instruction->GetType();
Calin Juravle77520bc2015-01-12 18:45:46 +00005214 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005215 case DataType::Type::kBool:
5216 case DataType::Type::kUint8: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005217 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005218 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005219 break;
5220 }
5221
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005222 case DataType::Type::kInt8: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005223 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005224 __ movsxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005225 break;
5226 }
5227
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005228 case DataType::Type::kUint16: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005229 Register out = out_loc.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07005230 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5231 // Branch cases into compressed and uncompressed for each index's type.
5232 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
5233 NearLabel done, not_compressed;
Vladimir Marko3c89d422017-02-17 11:30:23 +00005234 __ testb(Address(obj, count_offset), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005235 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005236 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
5237 "Expecting 0=compressed, 1=uncompressed");
5238 __ j(kNotZero, &not_compressed);
jessicahandojo4877b792016-09-08 19:49:13 -07005239 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
5240 __ jmp(&done);
5241 __ Bind(&not_compressed);
5242 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5243 __ Bind(&done);
5244 } else {
5245 // Common case for charAt of array of char or when string compression's
5246 // feature is turned off.
5247 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5248 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005249 break;
5250 }
5251
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005252 case DataType::Type::kInt16: {
5253 Register out = out_loc.AsRegister<Register>();
5254 __ movsxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5255 break;
5256 }
5257
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005258 case DataType::Type::kInt32: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005259 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005260 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005261 break;
5262 }
5263
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005264 case DataType::Type::kReference: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005265 static_assert(
5266 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5267 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00005268 // /* HeapReference<Object> */ out =
5269 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5270 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005271 // Note that a potential implicit null check is handled in this
5272 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
5273 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00005274 instruction, out_loc, obj, data_offset, index, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005275 } else {
5276 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005277 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
5278 codegen_->MaybeRecordImplicitNullCheck(instruction);
5279 // If read barriers are enabled, emit read barriers other than
5280 // Baker's using a slow path (and also unpoison the loaded
5281 // reference, if heap poisoning is enabled).
Roland Levillain7c1559a2015-12-15 10:55:36 +00005282 if (index.IsConstant()) {
5283 uint32_t offset =
5284 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005285 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5286 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005287 codegen_->MaybeGenerateReadBarrierSlow(
5288 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5289 }
5290 }
5291 break;
5292 }
5293
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005294 case DataType::Type::kInt64: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005295 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005296 __ movl(out_loc.AsRegisterPairLow<Register>(),
5297 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
5298 codegen_->MaybeRecordImplicitNullCheck(instruction);
5299 __ movl(out_loc.AsRegisterPairHigh<Register>(),
5300 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005301 break;
5302 }
5303
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005304 case DataType::Type::kFloat32: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005305 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005306 __ movss(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005307 break;
5308 }
5309
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005310 case DataType::Type::kFloat64: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005311 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005312 __ movsd(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005313 break;
5314 }
5315
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005316 case DataType::Type::kVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00005317 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005318 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005319 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005320
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005321 if (type == DataType::Type::kReference || type == DataType::Type::kInt64) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005322 // Potential implicit null checks, in the case of reference or
5323 // long arrays, are handled in the previous switch statement.
5324 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005325 codegen_->MaybeRecordImplicitNullCheck(instruction);
5326 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005327}
5328
5329void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005330 DataType::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005331
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005332 bool needs_write_barrier =
5333 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005334 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005335
Vladimir Markoca6fff82017-10-03 14:49:14 +01005336 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Nicolas Geoffray39468442014-09-02 15:17:15 +01005337 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01005338 may_need_runtime_call_for_type_check ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00005339 LocationSummary::kCallOnSlowPath :
5340 LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005341
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005342 bool is_byte_type = DataType::Size(value_type) == 1u;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005343 // We need the inputs to be different than the output in case of long operation.
5344 // In case of a byte operation, the register allocator does not support multiple
5345 // inputs that die at entry with one in a specific register.
5346 locations->SetInAt(0, Location::RequiresRegister());
5347 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5348 if (is_byte_type) {
5349 // Ensure the value is in a byte register.
5350 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005351 } else if (DataType::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05005352 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005353 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005354 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
5355 }
5356 if (needs_write_barrier) {
5357 // Temporary registers for the write barrier.
5358 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5359 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005360 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005361 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005362}
5363
5364void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
5365 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005366 Location array_loc = locations->InAt(0);
5367 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005368 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005369 Location value = locations->InAt(2);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005370 DataType::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005371 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5372 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5373 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005374 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005375 bool needs_write_barrier =
5376 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005377
5378 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005379 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005380 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005381 case DataType::Type::kInt8: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005382 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005383 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_1, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005384 if (value.IsRegister()) {
5385 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005386 } else {
Nicolas Geoffray78612082017-07-24 14:18:53 +01005387 __ movb(address, Immediate(CodeGenerator::GetInt8ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005388 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005389 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005390 break;
5391 }
5392
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005393 case DataType::Type::kUint16:
5394 case DataType::Type::kInt16: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005395 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005396 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_2, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005397 if (value.IsRegister()) {
5398 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005399 } else {
Nicolas Geoffray78612082017-07-24 14:18:53 +01005400 __ movw(address, Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005401 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005402 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005403 break;
5404 }
5405
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005406 case DataType::Type::kReference: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005407 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005408 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005409
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005410 if (!value.IsRegister()) {
5411 // Just setting null.
5412 DCHECK(instruction->InputAt(2)->IsNullConstant());
5413 DCHECK(value.IsConstant()) << value;
5414 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00005415 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005416 DCHECK(!needs_write_barrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005417 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005418 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005419 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005420
5421 DCHECK(needs_write_barrier);
5422 Register register_value = value.AsRegister<Register>();
Roland Levillain16d9f942016-08-25 17:27:56 +01005423 // We cannot use a NearLabel for `done`, as its range may be too
5424 // short when Baker read barriers are enabled.
5425 Label done;
5426 NearLabel not_null, do_put;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005427 SlowPathCode* slow_path = nullptr;
Roland Levillain16d9f942016-08-25 17:27:56 +01005428 Location temp_loc = locations->GetTemp(0);
5429 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005430 if (may_need_runtime_call_for_type_check) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005431 slow_path = new (GetGraph()->GetAllocator()) ArraySetSlowPathX86(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005432 codegen_->AddSlowPath(slow_path);
5433 if (instruction->GetValueCanBeNull()) {
5434 __ testl(register_value, register_value);
5435 __ j(kNotEqual, &not_null);
5436 __ movl(address, Immediate(0));
5437 codegen_->MaybeRecordImplicitNullCheck(instruction);
5438 __ jmp(&done);
5439 __ Bind(&not_null);
5440 }
5441
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005442 // Note that when Baker read barriers are enabled, the type
5443 // checks are performed without read barriers. This is fine,
5444 // even in the case where a class object is in the from-space
5445 // after the flip, as a comparison involving such a type would
5446 // not produce a false positive; it may of course produce a
5447 // false negative, in which case we would take the ArraySet
5448 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01005449
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005450 // /* HeapReference<Class> */ temp = array->klass_
5451 __ movl(temp, Address(array, class_offset));
5452 codegen_->MaybeRecordImplicitNullCheck(instruction);
5453 __ MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01005454
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005455 // /* HeapReference<Class> */ temp = temp->component_type_
5456 __ movl(temp, Address(temp, component_offset));
5457 // If heap poisoning is enabled, no need to unpoison `temp`
5458 // nor the object reference in `register_value->klass`, as
5459 // we are comparing two poisoned references.
5460 __ cmpl(temp, Address(register_value, class_offset));
Roland Levillain16d9f942016-08-25 17:27:56 +01005461
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005462 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5463 __ j(kEqual, &do_put);
5464 // If heap poisoning is enabled, the `temp` reference has
5465 // not been unpoisoned yet; unpoison it now.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005466 __ MaybeUnpoisonHeapReference(temp);
5467
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005468 // If heap poisoning is enabled, no need to unpoison the
5469 // heap reference loaded below, as it is only used for a
5470 // comparison with null.
5471 __ cmpl(Address(temp, super_offset), Immediate(0));
5472 __ j(kNotEqual, slow_path->GetEntryLabel());
5473 __ Bind(&do_put);
5474 } else {
5475 __ j(kNotEqual, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005476 }
5477 }
5478
5479 if (kPoisonHeapReferences) {
5480 __ movl(temp, register_value);
5481 __ PoisonHeapReference(temp);
5482 __ movl(address, temp);
5483 } else {
5484 __ movl(address, register_value);
5485 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005486 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005487 codegen_->MaybeRecordImplicitNullCheck(instruction);
5488 }
5489
5490 Register card = locations->GetTemp(1).AsRegister<Register>();
5491 codegen_->MarkGCCard(
5492 temp, card, array, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
5493 __ Bind(&done);
5494
5495 if (slow_path != nullptr) {
5496 __ Bind(slow_path->GetExitLabel());
5497 }
5498
5499 break;
5500 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005501
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005502 case DataType::Type::kInt32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005503 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005504 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005505 if (value.IsRegister()) {
5506 __ movl(address, value.AsRegister<Register>());
5507 } else {
5508 DCHECK(value.IsConstant()) << value;
5509 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5510 __ movl(address, Immediate(v));
5511 }
5512 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005513 break;
5514 }
5515
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005516 case DataType::Type::kInt64: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005517 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005518 if (value.IsRegisterPair()) {
5519 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
5520 value.AsRegisterPairLow<Register>());
5521 codegen_->MaybeRecordImplicitNullCheck(instruction);
5522 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
5523 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005524 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005525 DCHECK(value.IsConstant());
5526 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
5527 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
5528 Immediate(Low32Bits(val)));
5529 codegen_->MaybeRecordImplicitNullCheck(instruction);
5530 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
5531 Immediate(High32Bits(val)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005532 }
5533 break;
5534 }
5535
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005536 case DataType::Type::kFloat32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005537 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005538 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005539 if (value.IsFpuRegister()) {
5540 __ movss(address, value.AsFpuRegister<XmmRegister>());
5541 } else {
5542 DCHECK(value.IsConstant());
5543 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
5544 __ movl(address, Immediate(v));
5545 }
5546 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005547 break;
5548 }
5549
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005550 case DataType::Type::kFloat64: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005551 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005552 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005553 if (value.IsFpuRegister()) {
5554 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5555 } else {
5556 DCHECK(value.IsConstant());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005557 Address address_hi =
5558 CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset + kX86WordSize);
Mark Mendell81489372015-11-04 11:30:41 -05005559 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5560 __ movl(address, Immediate(Low32Bits(v)));
5561 codegen_->MaybeRecordImplicitNullCheck(instruction);
5562 __ movl(address_hi, Immediate(High32Bits(v)));
5563 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005564 break;
5565 }
5566
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005567 case DataType::Type::kVoid:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005568 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005569 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005570 }
5571}
5572
5573void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005574 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005575 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04005576 if (!instruction->IsEmittedAtUseSite()) {
5577 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5578 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005579}
5580
5581void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04005582 if (instruction->IsEmittedAtUseSite()) {
5583 return;
5584 }
5585
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005586 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005587 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005588 Register obj = locations->InAt(0).AsRegister<Register>();
5589 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005590 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005591 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo4877b792016-09-08 19:49:13 -07005592 // Mask out most significant bit in case the array is String's array of char.
5593 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005594 __ shrl(out, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005595 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005596}
5597
5598void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005599 RegisterSet caller_saves = RegisterSet::Empty();
5600 InvokeRuntimeCallingConvention calling_convention;
5601 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5602 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5603 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005604 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04005605 HInstruction* length = instruction->InputAt(1);
5606 if (!length->IsEmittedAtUseSite()) {
5607 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5608 }
jessicahandojo4877b792016-09-08 19:49:13 -07005609 // Need register to see array's length.
5610 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5611 locations->AddTemp(Location::RequiresRegister());
5612 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005613}
5614
5615void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
jessicahandojo4877b792016-09-08 19:49:13 -07005616 const bool is_string_compressed_char_at =
5617 mirror::kUseStringCompression && instruction->IsStringCharAt();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005618 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005619 Location index_loc = locations->InAt(0);
5620 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07005621 SlowPathCode* slow_path =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005622 new (GetGraph()->GetAllocator()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005623
Mark Mendell99dbd682015-04-22 16:18:52 -04005624 if (length_loc.IsConstant()) {
5625 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5626 if (index_loc.IsConstant()) {
5627 // BCE will remove the bounds check if we are guarenteed to pass.
5628 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5629 if (index < 0 || index >= length) {
5630 codegen_->AddSlowPath(slow_path);
5631 __ jmp(slow_path->GetEntryLabel());
5632 } else {
5633 // Some optimization after BCE may have generated this, and we should not
5634 // generate a bounds check if it is a valid range.
5635 }
5636 return;
5637 }
5638
5639 // We have to reverse the jump condition because the length is the constant.
5640 Register index_reg = index_loc.AsRegister<Register>();
5641 __ cmpl(index_reg, Immediate(length));
5642 codegen_->AddSlowPath(slow_path);
5643 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005644 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04005645 HInstruction* array_length = instruction->InputAt(1);
5646 if (array_length->IsEmittedAtUseSite()) {
5647 // Address the length field in the array.
5648 DCHECK(array_length->IsArrayLength());
5649 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
5650 Location array_loc = array_length->GetLocations()->InAt(0);
5651 Address array_len(array_loc.AsRegister<Register>(), len_offset);
jessicahandojo4877b792016-09-08 19:49:13 -07005652 if (is_string_compressed_char_at) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005653 // TODO: if index_loc.IsConstant(), compare twice the index (to compensate for
5654 // the string compression flag) with the in-memory length and avoid the temporary.
jessicahandojo4877b792016-09-08 19:49:13 -07005655 Register length_reg = locations->GetTemp(0).AsRegister<Register>();
5656 __ movl(length_reg, array_len);
5657 codegen_->MaybeRecordImplicitNullCheck(array_length);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005658 __ shrl(length_reg, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005659 codegen_->GenerateIntCompare(length_reg, index_loc);
Mark Mendellee8d9712016-07-12 11:13:15 -04005660 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07005661 // Checking bounds for general case:
5662 // Array of char or string's array with feature compression off.
5663 if (index_loc.IsConstant()) {
5664 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5665 __ cmpl(array_len, Immediate(value));
5666 } else {
5667 __ cmpl(array_len, index_loc.AsRegister<Register>());
5668 }
5669 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendellee8d9712016-07-12 11:13:15 -04005670 }
Mark Mendell99dbd682015-04-22 16:18:52 -04005671 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005672 codegen_->GenerateIntCompare(length_loc, index_loc);
Mark Mendell99dbd682015-04-22 16:18:52 -04005673 }
5674 codegen_->AddSlowPath(slow_path);
5675 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005676 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005677}
5678
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005679void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005680 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005681}
5682
5683void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01005684 if (instruction->GetNext()->IsSuspendCheck() &&
5685 instruction->GetBlock()->GetLoopInformation() != nullptr) {
5686 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
5687 // The back edge will generate the suspend check.
5688 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
5689 }
5690
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005691 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5692}
5693
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005694void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005695 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5696 instruction, LocationSummary::kCallOnSlowPath);
Aart Bikb13c65b2017-03-21 20:14:07 -07005697 // In suspend check slow path, usually there are no caller-save registers at all.
5698 // If SIMD instructions are present, however, we force spilling all live SIMD
5699 // registers in full width (since the runtime only saves/restores lower part).
Aart Bik5576f372017-03-23 16:17:37 -07005700 locations->SetCustomSlowPathCallerSaves(
5701 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005702}
5703
5704void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005705 HBasicBlock* block = instruction->GetBlock();
5706 if (block->GetLoopInformation() != nullptr) {
5707 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5708 // The back edge will generate the suspend check.
5709 return;
5710 }
5711 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5712 // The goto will generate the suspend check.
5713 return;
5714 }
5715 GenerateSuspendCheck(instruction, nullptr);
5716}
5717
5718void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
5719 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005720 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005721 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
5722 if (slow_path == nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005723 slow_path = new (GetGraph()->GetAllocator()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005724 instruction->SetSlowPath(slow_path);
5725 codegen_->AddSlowPath(slow_path);
5726 if (successor != nullptr) {
5727 DCHECK(successor->IsLoopHeader());
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005728 }
5729 } else {
5730 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5731 }
5732
Andreas Gampe542451c2016-07-26 09:02:02 -07005733 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86PointerSize>().Int32Value()),
Roland Levillain7c1559a2015-12-15 10:55:36 +00005734 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005735 if (successor == nullptr) {
5736 __ j(kNotEqual, slow_path->GetEntryLabel());
5737 __ Bind(slow_path->GetReturnLabel());
5738 } else {
5739 __ j(kEqual, codegen_->GetLabelOf(successor));
5740 __ jmp(slow_path->GetEntryLabel());
5741 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005742}
5743
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005744X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
5745 return codegen_->GetAssembler();
5746}
5747
Mark Mendell7c8d0092015-01-26 11:21:33 -05005748void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005749 ScratchRegisterScope ensure_scratch(
5750 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5751 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5752 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5753 __ movl(temp_reg, Address(ESP, src + stack_offset));
5754 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005755}
5756
5757void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005758 ScratchRegisterScope ensure_scratch(
5759 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5760 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5761 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5762 __ movl(temp_reg, Address(ESP, src + stack_offset));
5763 __ movl(Address(ESP, dst + stack_offset), temp_reg);
5764 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
5765 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005766}
5767
5768void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005769 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005770 Location source = move->GetSource();
5771 Location destination = move->GetDestination();
5772
5773 if (source.IsRegister()) {
5774 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005775 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005776 } else if (destination.IsFpuRegister()) {
5777 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005778 } else {
5779 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005780 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005781 }
David Brazdil74eb1b22015-12-14 11:44:01 +00005782 } else if (source.IsRegisterPair()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005783 size_t elem_size = DataType::Size(DataType::Type::kInt32);
David Brazdil74eb1b22015-12-14 11:44:01 +00005784 // Create stack space for 2 elements.
5785 __ subl(ESP, Immediate(2 * elem_size));
5786 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
5787 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
5788 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
5789 // And remove the temporary stack space we allocated.
5790 __ addl(ESP, Immediate(2 * elem_size));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005791 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005792 if (destination.IsRegister()) {
5793 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
5794 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005795 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005796 } else if (destination.IsRegisterPair()) {
5797 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
5798 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
5799 __ psrlq(src_reg, Immediate(32));
5800 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005801 } else if (destination.IsStackSlot()) {
5802 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07005803 } else if (destination.IsDoubleStackSlot()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005804 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07005805 } else {
5806 DCHECK(destination.IsSIMDStackSlot());
5807 __ movups(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005808 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005809 } else if (source.IsStackSlot()) {
5810 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005811 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005812 } else if (destination.IsFpuRegister()) {
5813 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005814 } else {
5815 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005816 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
5817 }
5818 } else if (source.IsDoubleStackSlot()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005819 if (destination.IsRegisterPair()) {
5820 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
5821 __ movl(destination.AsRegisterPairHigh<Register>(),
5822 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
5823 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005824 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
5825 } else {
5826 DCHECK(destination.IsDoubleStackSlot()) << destination;
5827 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005828 }
Aart Bik5576f372017-03-23 16:17:37 -07005829 } else if (source.IsSIMDStackSlot()) {
5830 DCHECK(destination.IsFpuRegister());
5831 __ movups(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005832 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005833 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005834 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005835 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005836 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005837 if (value == 0) {
5838 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
5839 } else {
5840 __ movl(destination.AsRegister<Register>(), Immediate(value));
5841 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005842 } else {
5843 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05005844 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005845 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005846 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005847 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005848 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005849 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005850 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005851 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5852 if (value == 0) {
5853 // Easy handling of 0.0.
5854 __ xorps(dest, dest);
5855 } else {
5856 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005857 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5858 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
5859 __ movl(temp, Immediate(value));
5860 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005861 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005862 } else {
5863 DCHECK(destination.IsStackSlot()) << destination;
5864 __ movl(Address(ESP, destination.GetStackIndex()), imm);
5865 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005866 } else if (constant->IsLongConstant()) {
5867 int64_t value = constant->AsLongConstant()->GetValue();
5868 int32_t low_value = Low32Bits(value);
5869 int32_t high_value = High32Bits(value);
5870 Immediate low(low_value);
5871 Immediate high(high_value);
5872 if (destination.IsDoubleStackSlot()) {
5873 __ movl(Address(ESP, destination.GetStackIndex()), low);
5874 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5875 } else {
5876 __ movl(destination.AsRegisterPairLow<Register>(), low);
5877 __ movl(destination.AsRegisterPairHigh<Register>(), high);
5878 }
5879 } else {
5880 DCHECK(constant->IsDoubleConstant());
5881 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005882 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005883 int32_t low_value = Low32Bits(value);
5884 int32_t high_value = High32Bits(value);
5885 Immediate low(low_value);
5886 Immediate high(high_value);
5887 if (destination.IsFpuRegister()) {
5888 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5889 if (value == 0) {
5890 // Easy handling of 0.0.
5891 __ xorpd(dest, dest);
5892 } else {
5893 __ pushl(high);
5894 __ pushl(low);
5895 __ movsd(dest, Address(ESP, 0));
5896 __ addl(ESP, Immediate(8));
5897 }
5898 } else {
5899 DCHECK(destination.IsDoubleStackSlot()) << destination;
5900 __ movl(Address(ESP, destination.GetStackIndex()), low);
5901 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5902 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005903 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005904 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005905 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005906 }
5907}
5908
Mark Mendella5c19ce2015-04-01 12:51:05 -04005909void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005910 Register suggested_scratch = reg == EAX ? EBX : EAX;
5911 ScratchRegisterScope ensure_scratch(
5912 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
5913
5914 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5915 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
5916 __ movl(Address(ESP, mem + stack_offset), reg);
5917 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005918}
5919
Mark Mendell7c8d0092015-01-26 11:21:33 -05005920void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005921 ScratchRegisterScope ensure_scratch(
5922 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5923
5924 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5925 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5926 __ movl(temp_reg, Address(ESP, mem + stack_offset));
5927 __ movss(Address(ESP, mem + stack_offset), reg);
5928 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005929}
5930
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005931void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005932 ScratchRegisterScope ensure_scratch1(
5933 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005934
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005935 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
5936 ScratchRegisterScope ensure_scratch2(
5937 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005938
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005939 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
5940 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
5941 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
5942 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
5943 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
5944 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005945}
5946
5947void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005948 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005949 Location source = move->GetSource();
5950 Location destination = move->GetDestination();
5951
5952 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04005953 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
5954 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
5955 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
5956 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
5957 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005958 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005959 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005960 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005961 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005962 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5963 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005964 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
5965 // Use XOR Swap algorithm to avoid a temporary.
5966 DCHECK_NE(source.reg(), destination.reg());
5967 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5968 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5969 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5970 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
5971 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
5972 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
5973 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005974 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
5975 // Take advantage of the 16 bytes in the XMM register.
5976 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
5977 Address stack(ESP, destination.GetStackIndex());
5978 // Load the double into the high doubleword.
5979 __ movhpd(reg, stack);
5980
5981 // Store the low double into the destination.
5982 __ movsd(stack, reg);
5983
5984 // Move the high double to the low double.
5985 __ psrldq(reg, Immediate(8));
5986 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
5987 // Take advantage of the 16 bytes in the XMM register.
5988 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
5989 Address stack(ESP, source.GetStackIndex());
5990 // Load the double into the high doubleword.
5991 __ movhpd(reg, stack);
5992
5993 // Store the low double into the destination.
5994 __ movsd(stack, reg);
5995
5996 // Move the high double to the low double.
5997 __ psrldq(reg, Immediate(8));
5998 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
5999 Exchange(destination.GetStackIndex(), source.GetStackIndex());
6000 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006001 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006002 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006003 }
6004}
6005
6006void ParallelMoveResolverX86::SpillScratch(int reg) {
6007 __ pushl(static_cast<Register>(reg));
6008}
6009
6010void ParallelMoveResolverX86::RestoreScratch(int reg) {
6011 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006012}
6013
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006014HLoadClass::LoadKind CodeGeneratorX86::GetSupportedLoadClassKind(
6015 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006016 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006017 case HLoadClass::LoadKind::kInvalid:
6018 LOG(FATAL) << "UNREACHABLE";
6019 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006020 case HLoadClass::LoadKind::kReferrersClass:
6021 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006022 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006023 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006024 case HLoadClass::LoadKind::kBssEntry:
Vladimir Marko764d4542017-05-16 10:31:41 +01006025 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006026 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006027 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006028 DCHECK(Runtime::Current()->UseJitCompilation());
6029 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01006030 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006031 case HLoadClass::LoadKind::kRuntimeCall:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006032 break;
6033 }
6034 return desired_class_load_kind;
6035}
6036
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006037void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00006038 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006039 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006040 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00006041 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006042 cls,
6043 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00006044 Location::RegisterLocation(EAX));
Vladimir Markoea4c1262017-02-06 19:59:33 +00006045 DCHECK_EQ(calling_convention.GetRegisterAt(0), EAX);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006046 return;
6047 }
Vladimir Marko41559982017-01-06 14:04:23 +00006048 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006049
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006050 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
6051 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006052 ? LocationSummary::kCallOnSlowPath
6053 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01006054 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006055 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006056 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006057 }
6058
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006059 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006060 load_kind == HLoadClass::LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006061 load_kind == HLoadClass::LoadKind::kBootImageClassTable ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006062 load_kind == HLoadClass::LoadKind::kBssEntry) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006063 locations->SetInAt(0, Location::RequiresRegister());
6064 }
6065 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006066 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
6067 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6068 // Rely on the type resolution and/or initialization to save everything.
6069 RegisterSet caller_saves = RegisterSet::Empty();
6070 InvokeRuntimeCallingConvention calling_convention;
6071 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6072 locations->SetCustomSlowPathCallerSaves(caller_saves);
6073 } else {
6074 // For non-Baker read barrier we have a temp-clobbering call.
6075 }
6076 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006077}
6078
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006079Label* CodeGeneratorX86::NewJitRootClassPatch(const DexFile& dex_file,
6080 dex::TypeIndex dex_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006081 Handle<mirror::Class> handle) {
6082 jit_class_roots_.Overwrite(TypeReference(&dex_file, dex_index),
6083 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006084 // Add a patch entry and return the label.
6085 jit_class_patches_.emplace_back(dex_file, dex_index.index_);
6086 PatchInfo<Label>* info = &jit_class_patches_.back();
6087 return &info->label;
6088}
6089
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006090// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6091// move.
6092void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00006093 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006094 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00006095 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01006096 return;
6097 }
Vladimir Marko41559982017-01-06 14:04:23 +00006098 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01006099
Vladimir Marko41559982017-01-06 14:04:23 +00006100 LocationSummary* locations = cls->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006101 Location out_loc = locations->Out();
6102 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006103
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006104 bool generate_null_check = false;
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006105 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
6106 ? kWithoutReadBarrier
6107 : kCompilerReadBarrierOption;
Vladimir Marko41559982017-01-06 14:04:23 +00006108 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006109 case HLoadClass::LoadKind::kReferrersClass: {
6110 DCHECK(!cls->CanCallRuntime());
6111 DCHECK(!cls->MustGenerateClinitCheck());
6112 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6113 Register current_method = locations->InAt(0).AsRegister<Register>();
6114 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006115 cls,
6116 out_loc,
6117 Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()),
Roland Levillain00468f32016-10-27 18:02:48 +01006118 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006119 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006120 break;
6121 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006122 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006123 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006124 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006125 Register method_address = locations->InAt(0).AsRegister<Register>();
6126 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Marko1998cd02017-01-13 13:02:58 +00006127 codegen_->RecordBootTypePatch(cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006128 break;
6129 }
6130 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006131 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006132 uint32_t address = dchecked_integral_cast<uint32_t>(
6133 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
6134 DCHECK_NE(address, 0u);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006135 __ movl(out, Immediate(address));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006136 break;
6137 }
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006138 case HLoadClass::LoadKind::kBootImageClassTable: {
6139 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6140 Register method_address = locations->InAt(0).AsRegister<Register>();
6141 __ movl(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
6142 codegen_->RecordBootTypePatch(cls);
6143 // Extract the reference from the slot data, i.e. clear the hash bits.
6144 int32_t masked_hash = ClassTable::TableSlot::MaskHash(
6145 ComputeModifiedUtf8Hash(cls->GetDexFile().StringByTypeIdx(cls->GetTypeIndex())));
6146 if (masked_hash != 0) {
6147 __ subl(out, Immediate(masked_hash));
6148 }
6149 break;
6150 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006151 case HLoadClass::LoadKind::kBssEntry: {
6152 Register method_address = locations->InAt(0).AsRegister<Register>();
6153 Address address(method_address, CodeGeneratorX86::kDummy32BitOffset);
6154 Label* fixup_label = codegen_->NewTypeBssEntryPatch(cls);
6155 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
6156 generate_null_check = true;
6157 break;
6158 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006159 case HLoadClass::LoadKind::kJitTableAddress: {
6160 Address address = Address::Absolute(CodeGeneratorX86::kDummy32BitOffset);
6161 Label* fixup_label = codegen_->NewJitRootClassPatch(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006162 cls->GetDexFile(), cls->GetTypeIndex(), cls->GetClass());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006163 // /* GcRoot<mirror::Class> */ out = *address
Vladimir Markoea4c1262017-02-06 19:59:33 +00006164 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006165 break;
6166 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006167 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006168 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00006169 LOG(FATAL) << "UNREACHABLE";
6170 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006171 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006172
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006173 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6174 DCHECK(cls->CanCallRuntime());
Vladimir Markoca6fff82017-10-03 14:49:14 +01006175 SlowPathCode* slow_path = new (GetGraph()->GetAllocator()) LoadClassSlowPathX86(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006176 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
6177 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006178
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006179 if (generate_null_check) {
6180 __ testl(out, out);
6181 __ j(kEqual, slow_path->GetEntryLabel());
6182 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006183
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006184 if (cls->MustGenerateClinitCheck()) {
6185 GenerateClassInitializationCheck(slow_path, out);
6186 } else {
6187 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006188 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006189 }
6190}
6191
6192void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
6193 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006194 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006195 locations->SetInAt(0, Location::RequiresRegister());
6196 if (check->HasUses()) {
6197 locations->SetOut(Location::SameAsFirstInput());
6198 }
6199}
6200
6201void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006202 // We assume the class to not be null.
Vladimir Markoca6fff82017-10-03 14:49:14 +01006203 SlowPathCode* slow_path = new (GetGraph()->GetAllocator()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006204 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006205 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00006206 GenerateClassInitializationCheck(slow_path,
6207 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006208}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006209
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006210void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07006211 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006212 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
6213 Immediate(mirror::Class::kStatusInitialized));
6214 __ j(kLess, slow_path->GetEntryLabel());
6215 __ Bind(slow_path->GetExitLabel());
6216 // No need for memory fence, thanks to the X86 memory model.
6217}
6218
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006219HLoadString::LoadKind CodeGeneratorX86::GetSupportedLoadStringKind(
6220 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006221 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006222 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006223 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00006224 case HLoadString::LoadKind::kBssEntry:
Vladimir Marko764d4542017-05-16 10:31:41 +01006225 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006226 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006227 case HLoadString::LoadKind::kJitTableAddress:
6228 DCHECK(Runtime::Current()->UseJitCompilation());
6229 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01006230 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006231 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006232 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006233 }
6234 return desired_string_load_kind;
6235}
6236
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006237void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006238 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01006239 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006240 HLoadString::LoadKind load_kind = load->GetLoadKind();
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006241 if (load_kind == HLoadString::LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006242 load_kind == HLoadString::LoadKind::kBootImageInternTable ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00006243 load_kind == HLoadString::LoadKind::kBssEntry) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006244 locations->SetInAt(0, Location::RequiresRegister());
6245 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006246 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006247 locations->SetOut(Location::RegisterLocation(EAX));
6248 } else {
6249 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006250 if (load_kind == HLoadString::LoadKind::kBssEntry) {
6251 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00006252 // Rely on the pResolveString to save everything.
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006253 RegisterSet caller_saves = RegisterSet::Empty();
6254 InvokeRuntimeCallingConvention calling_convention;
6255 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6256 locations->SetCustomSlowPathCallerSaves(caller_saves);
6257 } else {
6258 // For non-Baker read barrier we have a temp-clobbering call.
6259 }
6260 }
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006261 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006262}
6263
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006264Label* CodeGeneratorX86::NewJitRootStringPatch(const DexFile& dex_file,
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006265 dex::StringIndex dex_index,
6266 Handle<mirror::String> handle) {
6267 jit_string_roots_.Overwrite(
6268 StringReference(&dex_file, dex_index), reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006269 // Add a patch entry and return the label.
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006270 jit_string_patches_.emplace_back(dex_file, dex_index.index_);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006271 PatchInfo<Label>* info = &jit_string_patches_.back();
6272 return &info->label;
6273}
6274
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006275// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6276// move.
6277void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006278 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006279 Location out_loc = locations->Out();
6280 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006281
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006282 switch (load->GetLoadKind()) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006283 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006284 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006285 Register method_address = locations->InAt(0).AsRegister<Register>();
6286 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Markoaad75c62016-10-03 08:46:48 +00006287 codegen_->RecordBootStringPatch(load);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006288 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006289 }
6290 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006291 uint32_t address = dchecked_integral_cast<uint32_t>(
6292 reinterpret_cast<uintptr_t>(load->GetString().Get()));
6293 DCHECK_NE(address, 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006294 __ movl(out, Immediate(address));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006295 return;
6296 }
6297 case HLoadString::LoadKind::kBootImageInternTable: {
6298 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6299 Register method_address = locations->InAt(0).AsRegister<Register>();
6300 __ movl(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
6301 codegen_->RecordBootStringPatch(load);
6302 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006303 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00006304 case HLoadString::LoadKind::kBssEntry: {
6305 Register method_address = locations->InAt(0).AsRegister<Register>();
6306 Address address = Address(method_address, CodeGeneratorX86::kDummy32BitOffset);
6307 Label* fixup_label = codegen_->NewStringBssEntryPatch(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006308 // /* GcRoot<mirror::String> */ out = *address /* PC-relative */
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006309 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
Vladimir Markoca6fff82017-10-03 14:49:14 +01006310 SlowPathCode* slow_path = new (GetGraph()->GetAllocator()) LoadStringSlowPathX86(load);
Vladimir Markoaad75c62016-10-03 08:46:48 +00006311 codegen_->AddSlowPath(slow_path);
6312 __ testl(out, out);
6313 __ j(kEqual, slow_path->GetEntryLabel());
6314 __ Bind(slow_path->GetExitLabel());
6315 return;
6316 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006317 case HLoadString::LoadKind::kJitTableAddress: {
6318 Address address = Address::Absolute(CodeGeneratorX86::kDummy32BitOffset);
6319 Label* fixup_label = codegen_->NewJitRootStringPatch(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006320 load->GetDexFile(), load->GetStringIndex(), load->GetString());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006321 // /* GcRoot<mirror::String> */ out = *address
6322 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
6323 return;
6324 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006325 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006326 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006327 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006328
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006329 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006330 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006331 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006332 __ movl(calling_convention.GetRegisterAt(0), Immediate(load->GetStringIndex().index_));
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006333 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6334 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006335}
6336
David Brazdilcb1c0552015-08-04 16:22:25 +01006337static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006338 return Address::Absolute(Thread::ExceptionOffset<kX86PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01006339}
6340
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006341void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
6342 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006343 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006344 locations->SetOut(Location::RequiresRegister());
6345}
6346
6347void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01006348 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
6349}
6350
6351void LocationsBuilderX86::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006352 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
David Brazdilcb1c0552015-08-04 16:22:25 +01006353}
6354
6355void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6356 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006357}
6358
6359void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006360 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6361 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006362 InvokeRuntimeCallingConvention calling_convention;
6363 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6364}
6365
6366void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006367 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006368 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006369}
6370
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006371// Temp is used for read barrier.
6372static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
6373 if (kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00006374 !kUseBakerReadBarrier &&
6375 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain7c1559a2015-12-15 10:55:36 +00006376 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006377 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
6378 return 1;
6379 }
6380 return 0;
6381}
6382
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006383// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006384// interface pointer, one for loading the current interface.
6385// The other checks have one temp for loading the object's class.
6386static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
6387 if (type_check_kind == TypeCheckKind::kInterfaceCheck && !kPoisonHeapReferences) {
6388 return 2;
6389 }
6390 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006391}
6392
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006393void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006394 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006395 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01006396 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006397 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006398 case TypeCheckKind::kExactCheck:
6399 case TypeCheckKind::kAbstractClassCheck:
6400 case TypeCheckKind::kClassHierarchyCheck:
6401 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006402 call_kind =
6403 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01006404 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006405 break;
6406 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006407 case TypeCheckKind::kUnresolvedCheck:
6408 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006409 call_kind = LocationSummary::kCallOnSlowPath;
6410 break;
6411 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006412
Vladimir Markoca6fff82017-10-03 14:49:14 +01006413 LocationSummary* locations =
6414 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01006415 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006416 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006417 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006418 locations->SetInAt(0, Location::RequiresRegister());
6419 locations->SetInAt(1, Location::Any());
6420 // Note that TypeCheckSlowPathX86 uses this "out" register too.
6421 locations->SetOut(Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006422 // When read barriers are enabled, we need a temporary register for some cases.
6423 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006424}
6425
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006426void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006427 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006428 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006429 Location obj_loc = locations->InAt(0);
6430 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006431 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006432 Location out_loc = locations->Out();
6433 Register out = out_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006434 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
6435 DCHECK_LE(num_temps, 1u);
6436 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006437 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006438 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6439 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6440 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07006441 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006442 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006443
6444 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006445 // Avoid null check if we know obj is not null.
6446 if (instruction->MustDoNullCheck()) {
6447 __ testl(obj, obj);
6448 __ j(kEqual, &zero);
6449 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006450
Roland Levillain7c1559a2015-12-15 10:55:36 +00006451 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006452 case TypeCheckKind::kExactCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006453 // /* HeapReference<Class> */ out = obj->klass_
6454 GenerateReferenceLoadTwoRegisters(instruction,
6455 out_loc,
6456 obj_loc,
6457 class_offset,
6458 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006459 if (cls.IsRegister()) {
6460 __ cmpl(out, cls.AsRegister<Register>());
6461 } else {
6462 DCHECK(cls.IsStackSlot()) << cls;
6463 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6464 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006465
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006466 // Classes must be equal for the instanceof to succeed.
6467 __ j(kNotEqual, &zero);
6468 __ movl(out, Immediate(1));
6469 __ jmp(&done);
6470 break;
6471 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006472
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006473 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006474 // /* HeapReference<Class> */ out = obj->klass_
6475 GenerateReferenceLoadTwoRegisters(instruction,
6476 out_loc,
6477 obj_loc,
6478 class_offset,
6479 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006480 // If the class is abstract, we eagerly fetch the super class of the
6481 // object to avoid doing a comparison we know will fail.
6482 NearLabel loop;
6483 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006484 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006485 GenerateReferenceLoadOneRegister(instruction,
6486 out_loc,
6487 super_offset,
6488 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006489 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006490 __ testl(out, out);
6491 // If `out` is null, we use it for the result, and jump to `done`.
6492 __ j(kEqual, &done);
6493 if (cls.IsRegister()) {
6494 __ cmpl(out, cls.AsRegister<Register>());
6495 } else {
6496 DCHECK(cls.IsStackSlot()) << cls;
6497 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6498 }
6499 __ j(kNotEqual, &loop);
6500 __ movl(out, Immediate(1));
6501 if (zero.IsLinked()) {
6502 __ jmp(&done);
6503 }
6504 break;
6505 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006506
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006507 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006508 // /* HeapReference<Class> */ out = obj->klass_
6509 GenerateReferenceLoadTwoRegisters(instruction,
6510 out_loc,
6511 obj_loc,
6512 class_offset,
6513 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006514 // Walk over the class hierarchy to find a match.
6515 NearLabel loop, success;
6516 __ Bind(&loop);
6517 if (cls.IsRegister()) {
6518 __ cmpl(out, cls.AsRegister<Register>());
6519 } else {
6520 DCHECK(cls.IsStackSlot()) << cls;
6521 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6522 }
6523 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006524 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006525 GenerateReferenceLoadOneRegister(instruction,
6526 out_loc,
6527 super_offset,
6528 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006529 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006530 __ testl(out, out);
6531 __ j(kNotEqual, &loop);
6532 // If `out` is null, we use it for the result, and jump to `done`.
6533 __ jmp(&done);
6534 __ Bind(&success);
6535 __ movl(out, Immediate(1));
6536 if (zero.IsLinked()) {
6537 __ jmp(&done);
6538 }
6539 break;
6540 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006541
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006542 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006543 // /* HeapReference<Class> */ out = obj->klass_
6544 GenerateReferenceLoadTwoRegisters(instruction,
6545 out_loc,
6546 obj_loc,
6547 class_offset,
6548 kCompilerReadBarrierOption);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006549 // Do an exact check.
6550 NearLabel exact_check;
6551 if (cls.IsRegister()) {
6552 __ cmpl(out, cls.AsRegister<Register>());
6553 } else {
6554 DCHECK(cls.IsStackSlot()) << cls;
6555 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6556 }
6557 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006558 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006559 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006560 GenerateReferenceLoadOneRegister(instruction,
6561 out_loc,
6562 component_offset,
6563 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006564 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006565 __ testl(out, out);
6566 // If `out` is null, we use it for the result, and jump to `done`.
6567 __ j(kEqual, &done);
6568 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
6569 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006570 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006571 __ movl(out, Immediate(1));
6572 __ jmp(&done);
6573 break;
6574 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006575
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006576 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006577 // No read barrier since the slow path will retry upon failure.
6578 // /* HeapReference<Class> */ out = obj->klass_
6579 GenerateReferenceLoadTwoRegisters(instruction,
6580 out_loc,
6581 obj_loc,
6582 class_offset,
6583 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006584 if (cls.IsRegister()) {
6585 __ cmpl(out, cls.AsRegister<Register>());
6586 } else {
6587 DCHECK(cls.IsStackSlot()) << cls;
6588 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6589 }
6590 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Markoca6fff82017-10-03 14:49:14 +01006591 slow_path = new (GetGraph()->GetAllocator()) TypeCheckSlowPathX86(instruction,
6592 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006593 codegen_->AddSlowPath(slow_path);
6594 __ j(kNotEqual, slow_path->GetEntryLabel());
6595 __ movl(out, Immediate(1));
6596 if (zero.IsLinked()) {
6597 __ jmp(&done);
6598 }
6599 break;
6600 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006601
Calin Juravle98893e12015-10-02 21:05:03 +01006602 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006603 case TypeCheckKind::kInterfaceCheck: {
6604 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006605 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00006606 // cases.
6607 //
6608 // We cannot directly call the InstanceofNonTrivial runtime
6609 // entry point without resorting to a type checking slow path
6610 // here (i.e. by calling InvokeRuntime directly), as it would
6611 // require to assign fixed registers for the inputs of this
6612 // HInstanceOf instruction (following the runtime calling
6613 // convention), which might be cluttered by the potential first
6614 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006615 //
6616 // TODO: Introduce a new runtime entry point taking the object
6617 // to test (instead of its class) as argument, and let it deal
6618 // with the read barrier issues. This will let us refactor this
6619 // case of the `switch` code as it was previously (with a direct
6620 // call to the runtime not using a type checking slow path).
6621 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006622 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Markoca6fff82017-10-03 14:49:14 +01006623 slow_path = new (GetGraph()->GetAllocator()) TypeCheckSlowPathX86(instruction,
6624 /* is_fatal */ false);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006625 codegen_->AddSlowPath(slow_path);
6626 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006627 if (zero.IsLinked()) {
6628 __ jmp(&done);
6629 }
6630 break;
6631 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006632 }
6633
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006634 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006635 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006636 __ xorl(out, out);
6637 }
6638
6639 if (done.IsLinked()) {
6640 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006641 }
6642
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006643 if (slow_path != nullptr) {
6644 __ Bind(slow_path->GetExitLabel());
6645 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006646}
6647
Mathieu Chartier5ac321b2016-11-09 16:33:54 -08006648static bool IsTypeCheckSlowPathFatal(TypeCheckKind type_check_kind, bool throws_into_catch) {
6649 switch (type_check_kind) {
6650 case TypeCheckKind::kExactCheck:
6651 case TypeCheckKind::kAbstractClassCheck:
6652 case TypeCheckKind::kClassHierarchyCheck:
6653 case TypeCheckKind::kArrayObjectCheck:
6654 return !throws_into_catch && !kEmitCompilerReadBarrier;
6655 case TypeCheckKind::kInterfaceCheck:
6656 return !throws_into_catch && !kEmitCompilerReadBarrier && !kPoisonHeapReferences;
6657 case TypeCheckKind::kArrayCheck:
6658 case TypeCheckKind::kUnresolvedCheck:
6659 return false;
6660 }
6661 LOG(FATAL) << "Unreachable";
6662 UNREACHABLE();
6663}
6664
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006665void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006666 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006667 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Mathieu Chartier5ac321b2016-11-09 16:33:54 -08006668 LocationSummary::CallKind call_kind =
6669 IsTypeCheckSlowPathFatal(type_check_kind, throws_into_catch)
6670 ? LocationSummary::kNoCall
6671 : LocationSummary::kCallOnSlowPath;
Vladimir Markoca6fff82017-10-03 14:49:14 +01006672 LocationSummary* locations =
6673 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006674 locations->SetInAt(0, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006675 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
6676 // Require a register for the interface check since there is a loop that compares the class to
6677 // a memory address.
6678 locations->SetInAt(1, Location::RequiresRegister());
6679 } else {
6680 locations->SetInAt(1, Location::Any());
6681 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006682 // Note that TypeCheckSlowPathX86 uses this "temp" register too.
6683 locations->AddTemp(Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006684 // When read barriers are enabled, we need an additional temporary register for some cases.
6685 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
6686}
6687
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006688void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006689 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006690 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006691 Location obj_loc = locations->InAt(0);
6692 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006693 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006694 Location temp_loc = locations->GetTemp(0);
6695 Register temp = temp_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006696 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
6697 DCHECK_GE(num_temps, 1u);
6698 DCHECK_LE(num_temps, 2u);
6699 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
6700 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6701 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6702 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6703 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
6704 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
6705 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
6706 const uint32_t object_array_data_offset =
6707 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006708
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006709 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
6710 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
6711 // read barriers is done for performance and code size reasons.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006712 bool is_type_check_slow_path_fatal =
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006713 IsTypeCheckSlowPathFatal(type_check_kind, instruction->CanThrowIntoCatchBlock());
6714
Roland Levillain0d5a2812015-11-13 10:07:31 +00006715 SlowPathCode* type_check_slow_path =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006716 new (GetGraph()->GetAllocator()) TypeCheckSlowPathX86(instruction,
6717 is_type_check_slow_path_fatal);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006718 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006719
Roland Levillain0d5a2812015-11-13 10:07:31 +00006720 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006721 // Avoid null check if we know obj is not null.
6722 if (instruction->MustDoNullCheck()) {
6723 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006724 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006725 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006726
Roland Levillain0d5a2812015-11-13 10:07:31 +00006727 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006728 case TypeCheckKind::kExactCheck:
6729 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006730 // /* HeapReference<Class> */ temp = obj->klass_
6731 GenerateReferenceLoadTwoRegisters(instruction,
6732 temp_loc,
6733 obj_loc,
6734 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006735 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006736
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006737 if (cls.IsRegister()) {
6738 __ cmpl(temp, cls.AsRegister<Register>());
6739 } else {
6740 DCHECK(cls.IsStackSlot()) << cls;
6741 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6742 }
6743 // Jump to slow path for throwing the exception or doing a
6744 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006745 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006746 break;
6747 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006748
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006749 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006750 // /* HeapReference<Class> */ temp = obj->klass_
6751 GenerateReferenceLoadTwoRegisters(instruction,
6752 temp_loc,
6753 obj_loc,
6754 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006755 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006756
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006757 // If the class is abstract, we eagerly fetch the super class of the
6758 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006759 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006760 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006761 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006762 GenerateReferenceLoadOneRegister(instruction,
6763 temp_loc,
6764 super_offset,
6765 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006766 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006767
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006768 // If the class reference currently in `temp` is null, jump to the slow path to throw the
6769 // exception.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006770 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006771 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006772
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006773 // Otherwise, compare the classes
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006774 if (cls.IsRegister()) {
6775 __ cmpl(temp, cls.AsRegister<Register>());
6776 } else {
6777 DCHECK(cls.IsStackSlot()) << cls;
6778 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6779 }
6780 __ j(kNotEqual, &loop);
6781 break;
6782 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006783
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006784 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006785 // /* HeapReference<Class> */ temp = obj->klass_
6786 GenerateReferenceLoadTwoRegisters(instruction,
6787 temp_loc,
6788 obj_loc,
6789 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006790 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006791
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006792 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006793 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006794 __ Bind(&loop);
6795 if (cls.IsRegister()) {
6796 __ cmpl(temp, cls.AsRegister<Register>());
6797 } else {
6798 DCHECK(cls.IsStackSlot()) << cls;
6799 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6800 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006801 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006802
Roland Levillain0d5a2812015-11-13 10:07:31 +00006803 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006804 GenerateReferenceLoadOneRegister(instruction,
6805 temp_loc,
6806 super_offset,
6807 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006808 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006809
6810 // If the class reference currently in `temp` is not null, jump
6811 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006812 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006813 __ j(kNotZero, &loop);
6814 // Otherwise, jump to the slow path to throw the exception.;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006815 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006816 break;
6817 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006818
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006819 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006820 // /* HeapReference<Class> */ temp = obj->klass_
6821 GenerateReferenceLoadTwoRegisters(instruction,
6822 temp_loc,
6823 obj_loc,
6824 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006825 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006826
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006827 // Do an exact check.
6828 if (cls.IsRegister()) {
6829 __ cmpl(temp, cls.AsRegister<Register>());
6830 } else {
6831 DCHECK(cls.IsStackSlot()) << cls;
6832 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6833 }
6834 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006835
6836 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006837 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006838 GenerateReferenceLoadOneRegister(instruction,
6839 temp_loc,
6840 component_offset,
6841 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006842 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006843
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006844 // If the component type is null (i.e. the object not an array), jump to the slow path to
6845 // throw the exception. Otherwise proceed with the check.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006846 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006847 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006848
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006849 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006850 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006851 break;
6852 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006853
Calin Juravle98893e12015-10-02 21:05:03 +01006854 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006855 // We always go into the type check slow path for the unresolved check case.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006856 // We cannot directly call the CheckCast runtime entry point
6857 // without resorting to a type checking slow path here (i.e. by
6858 // calling InvokeRuntime directly), as it would require to
6859 // assign fixed registers for the inputs of this HInstanceOf
6860 // instruction (following the runtime calling convention), which
6861 // might be cluttered by the potential first read barrier
6862 // emission at the beginning of this method.
6863 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006864 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006865
6866 case TypeCheckKind::kInterfaceCheck: {
6867 // Fast path for the interface check. Since we compare with a memory location in the inner
6868 // loop we would need to have cls poisoned. However unpoisoning cls would reset the
6869 // conditional flags and cause the conditional jump to be incorrect. Therefore we just jump
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006870 // to the slow path if we are running under poisoning.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006871 if (!kPoisonHeapReferences) {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006872 // Try to avoid read barriers to improve the fast path. We can not get false positives by
6873 // doing this.
6874 // /* HeapReference<Class> */ temp = obj->klass_
6875 GenerateReferenceLoadTwoRegisters(instruction,
6876 temp_loc,
6877 obj_loc,
6878 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006879 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006880
6881 // /* HeapReference<Class> */ temp = temp->iftable_
6882 GenerateReferenceLoadTwoRegisters(instruction,
6883 temp_loc,
6884 temp_loc,
6885 iftable_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006886 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08006887 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006888 __ movl(maybe_temp2_loc.AsRegister<Register>(), Address(temp, array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08006889 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006890 NearLabel start_loop;
6891 __ Bind(&start_loop);
Mathieu Chartier6beced42016-11-15 15:51:31 -08006892 // Need to subtract first to handle the empty array case.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006893 __ subl(maybe_temp2_loc.AsRegister<Register>(), Immediate(2));
Mathieu Chartier6beced42016-11-15 15:51:31 -08006894 __ j(kNegative, type_check_slow_path->GetEntryLabel());
6895 // Go to next interface if the classes do not match.
6896 __ cmpl(cls.AsRegister<Register>(),
6897 CodeGeneratorX86::ArrayAddress(temp,
6898 maybe_temp2_loc,
6899 TIMES_4,
6900 object_array_data_offset));
6901 __ j(kNotEqual, &start_loop);
6902 } else {
6903 __ jmp(type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006904 }
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006905 break;
6906 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006907 }
6908 __ Bind(&done);
6909
Roland Levillain0d5a2812015-11-13 10:07:31 +00006910 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006911}
6912
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006913void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006914 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6915 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006916 InvokeRuntimeCallingConvention calling_convention;
6917 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6918}
6919
6920void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006921 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject
6922 : kQuickUnlockObject,
Alexandre Rames8158f282015-08-07 10:26:17 +01006923 instruction,
Serban Constantinescuba45db02016-07-12 22:53:02 +01006924 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006925 if (instruction->IsEnter()) {
6926 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6927 } else {
6928 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6929 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006930}
6931
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006932void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6933void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6934void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6935
6936void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6937 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006938 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006939 DCHECK(instruction->GetResultType() == DataType::Type::kInt32
6940 || instruction->GetResultType() == DataType::Type::kInt64);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006941 locations->SetInAt(0, Location::RequiresRegister());
6942 locations->SetInAt(1, Location::Any());
6943 locations->SetOut(Location::SameAsFirstInput());
6944}
6945
6946void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
6947 HandleBitwiseOperation(instruction);
6948}
6949
6950void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
6951 HandleBitwiseOperation(instruction);
6952}
6953
6954void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
6955 HandleBitwiseOperation(instruction);
6956}
6957
6958void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6959 LocationSummary* locations = instruction->GetLocations();
6960 Location first = locations->InAt(0);
6961 Location second = locations->InAt(1);
6962 DCHECK(first.Equals(locations->Out()));
6963
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006964 if (instruction->GetResultType() == DataType::Type::kInt32) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006965 if (second.IsRegister()) {
6966 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006967 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006968 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006969 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006970 } else {
6971 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006972 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006973 }
6974 } else if (second.IsConstant()) {
6975 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006976 __ andl(first.AsRegister<Register>(),
6977 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006978 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006979 __ orl(first.AsRegister<Register>(),
6980 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006981 } else {
6982 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00006983 __ xorl(first.AsRegister<Register>(),
6984 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006985 }
6986 } else {
6987 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006988 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006989 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006990 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006991 } else {
6992 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006993 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006994 }
6995 }
6996 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006997 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006998 if (second.IsRegisterPair()) {
6999 if (instruction->IsAnd()) {
7000 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7001 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7002 } else if (instruction->IsOr()) {
7003 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7004 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7005 } else {
7006 DCHECK(instruction->IsXor());
7007 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7008 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7009 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007010 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007011 if (instruction->IsAnd()) {
7012 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7013 __ andl(first.AsRegisterPairHigh<Register>(),
7014 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7015 } else if (instruction->IsOr()) {
7016 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7017 __ orl(first.AsRegisterPairHigh<Register>(),
7018 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7019 } else {
7020 DCHECK(instruction->IsXor());
7021 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7022 __ xorl(first.AsRegisterPairHigh<Register>(),
7023 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7024 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007025 } else {
7026 DCHECK(second.IsConstant()) << second;
7027 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007028 int32_t low_value = Low32Bits(value);
7029 int32_t high_value = High32Bits(value);
7030 Immediate low(low_value);
7031 Immediate high(high_value);
7032 Register first_low = first.AsRegisterPairLow<Register>();
7033 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007034 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007035 if (low_value == 0) {
7036 __ xorl(first_low, first_low);
7037 } else if (low_value != -1) {
7038 __ andl(first_low, low);
7039 }
7040 if (high_value == 0) {
7041 __ xorl(first_high, first_high);
7042 } else if (high_value != -1) {
7043 __ andl(first_high, high);
7044 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007045 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007046 if (low_value != 0) {
7047 __ orl(first_low, low);
7048 }
7049 if (high_value != 0) {
7050 __ orl(first_high, high);
7051 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007052 } else {
7053 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007054 if (low_value != 0) {
7055 __ xorl(first_low, low);
7056 }
7057 if (high_value != 0) {
7058 __ xorl(first_high, high);
7059 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007060 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007061 }
7062 }
7063}
7064
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007065void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(
7066 HInstruction* instruction,
7067 Location out,
7068 uint32_t offset,
7069 Location maybe_temp,
7070 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007071 Register out_reg = out.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007072 if (read_barrier_option == kWithReadBarrier) {
7073 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007074 if (kUseBakerReadBarrier) {
7075 // Load with fast path based Baker's read barrier.
7076 // /* HeapReference<Object> */ out = *(out + offset)
7077 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00007078 instruction, out, out_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007079 } else {
7080 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007081 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain7c1559a2015-12-15 10:55:36 +00007082 // in the following move operation, as we will need it for the
7083 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00007084 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007085 __ movl(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007086 // /* HeapReference<Object> */ out = *(out + offset)
7087 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007088 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007089 }
7090 } else {
7091 // Plain load with no read barrier.
7092 // /* HeapReference<Object> */ out = *(out + offset)
7093 __ movl(out_reg, Address(out_reg, offset));
7094 __ MaybeUnpoisonHeapReference(out_reg);
7095 }
7096}
7097
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007098void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(
7099 HInstruction* instruction,
7100 Location out,
7101 Location obj,
7102 uint32_t offset,
7103 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007104 Register out_reg = out.AsRegister<Register>();
7105 Register obj_reg = obj.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007106 if (read_barrier_option == kWithReadBarrier) {
7107 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007108 if (kUseBakerReadBarrier) {
7109 // Load with fast path based Baker's read barrier.
7110 // /* HeapReference<Object> */ out = *(obj + offset)
7111 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00007112 instruction, out, obj_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007113 } else {
7114 // Load with slow path based read barrier.
7115 // /* HeapReference<Object> */ out = *(obj + offset)
7116 __ movl(out_reg, Address(obj_reg, offset));
7117 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
7118 }
7119 } else {
7120 // Plain load with no read barrier.
7121 // /* HeapReference<Object> */ out = *(obj + offset)
7122 __ movl(out_reg, Address(obj_reg, offset));
7123 __ MaybeUnpoisonHeapReference(out_reg);
7124 }
7125}
7126
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007127void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(
7128 HInstruction* instruction,
7129 Location root,
7130 const Address& address,
7131 Label* fixup_label,
7132 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007133 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007134 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07007135 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007136 if (kUseBakerReadBarrier) {
7137 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
7138 // Baker's read barrier are used:
7139 //
Roland Levillaind966ce72017-02-09 16:20:14 +00007140 // root = obj.field;
7141 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
7142 // if (temp != null) {
7143 // root = temp(root)
Roland Levillain7c1559a2015-12-15 10:55:36 +00007144 // }
7145
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007146 // /* GcRoot<mirror::Object> */ root = *address
7147 __ movl(root_reg, address);
7148 if (fixup_label != nullptr) {
7149 __ Bind(fixup_label);
7150 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007151 static_assert(
7152 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
7153 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
7154 "have different sizes.");
7155 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
7156 "art::mirror::CompressedReference<mirror::Object> and int32_t "
7157 "have different sizes.");
7158
Vladimir Marko953437b2016-08-24 08:30:46 +00007159 // Slow path marking the GC root `root`.
Vladimir Markoca6fff82017-10-03 14:49:14 +01007160 SlowPathCode* slow_path = new (GetGraph()->GetAllocator()) ReadBarrierMarkSlowPathX86(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007161 instruction, root, /* unpoison_ref_before_marking */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007162 codegen_->AddSlowPath(slow_path);
7163
Roland Levillaind966ce72017-02-09 16:20:14 +00007164 // Test the entrypoint (`Thread::Current()->pReadBarrierMarkReg ## root.reg()`).
7165 const int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +01007166 Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(root.reg());
Roland Levillaind966ce72017-02-09 16:20:14 +00007167 __ fs()->cmpl(Address::Absolute(entry_point_offset), Immediate(0));
7168 // The entrypoint is null when the GC is not marking.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007169 __ j(kNotEqual, slow_path->GetEntryLabel());
7170 __ Bind(slow_path->GetExitLabel());
7171 } else {
7172 // GC root loaded through a slow path for read barriers other
7173 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007174 // /* GcRoot<mirror::Object>* */ root = address
7175 __ leal(root_reg, address);
7176 if (fixup_label != nullptr) {
7177 __ Bind(fixup_label);
7178 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007179 // /* mirror::Object* */ root = root->Read()
7180 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
7181 }
7182 } else {
7183 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007184 // /* GcRoot<mirror::Object> */ root = *address
7185 __ movl(root_reg, address);
7186 if (fixup_label != nullptr) {
7187 __ Bind(fixup_label);
7188 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007189 // Note that GC roots are not affected by heap poisoning, thus we
7190 // do not have to unpoison `root_reg` here.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007191 }
7192}
7193
7194void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
7195 Location ref,
7196 Register obj,
7197 uint32_t offset,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007198 bool needs_null_check) {
7199 DCHECK(kEmitCompilerReadBarrier);
7200 DCHECK(kUseBakerReadBarrier);
7201
7202 // /* HeapReference<Object> */ ref = *(obj + offset)
7203 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007204 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007205}
7206
7207void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
7208 Location ref,
7209 Register obj,
7210 uint32_t data_offset,
7211 Location index,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007212 bool needs_null_check) {
7213 DCHECK(kEmitCompilerReadBarrier);
7214 DCHECK(kUseBakerReadBarrier);
7215
Roland Levillain3d312422016-06-23 13:53:42 +01007216 static_assert(
7217 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
7218 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00007219 // /* HeapReference<Object> */ ref =
7220 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007221 Address src = CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007222 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007223}
7224
7225void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
7226 Location ref,
7227 Register obj,
7228 const Address& src,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007229 bool needs_null_check,
7230 bool always_update_field,
7231 Register* temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007232 DCHECK(kEmitCompilerReadBarrier);
7233 DCHECK(kUseBakerReadBarrier);
7234
7235 // In slow path based read barriers, the read barrier call is
7236 // inserted after the original load. However, in fast path based
7237 // Baker's read barriers, we need to perform the load of
7238 // mirror::Object::monitor_ *before* the original reference load.
7239 // This load-load ordering is required by the read barrier.
7240 // The fast path/slow path (for Baker's algorithm) should look like:
7241 //
7242 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7243 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7244 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007245 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain7c1559a2015-12-15 10:55:36 +00007246 // if (is_gray) {
7247 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7248 // }
7249 //
7250 // Note: the original implementation in ReadBarrier::Barrier is
7251 // slightly more complex as:
7252 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007253 // the high-bits of rb_state, which are expected to be all zeroes
7254 // (we use CodeGeneratorX86::GenerateMemoryBarrier instead here,
7255 // which is a no-op thanks to the x86 memory model);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007256 // - it performs additional checks that we do not do here for
7257 // performance reasons.
7258
7259 Register ref_reg = ref.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +00007260 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7261
Vladimir Marko953437b2016-08-24 08:30:46 +00007262 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007263 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
7264 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko953437b2016-08-24 08:30:46 +00007265 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
7266 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
7267 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
7268
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007269 // if (rb_state == ReadBarrier::GrayState())
Vladimir Marko953437b2016-08-24 08:30:46 +00007270 // ref = ReadBarrier::Mark(ref);
7271 // At this point, just do the "if" and make sure that flags are preserved until the branch.
7272 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain7c1559a2015-12-15 10:55:36 +00007273 if (needs_null_check) {
7274 MaybeRecordImplicitNullCheck(instruction);
7275 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007276
7277 // Load fence to prevent load-load reordering.
7278 // Note that this is a no-op, thanks to the x86 memory model.
7279 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
7280
7281 // The actual reference load.
7282 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00007283 __ movl(ref_reg, src); // Flags are unaffected.
7284
7285 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
7286 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007287 SlowPathCode* slow_path;
7288 if (always_update_field) {
7289 DCHECK(temp != nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01007290 slow_path = new (GetGraph()->GetAllocator()) ReadBarrierMarkAndUpdateFieldSlowPathX86(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007291 instruction, ref, obj, src, /* unpoison_ref_before_marking */ true, *temp);
7292 } else {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007293 slow_path = new (GetGraph()->GetAllocator()) ReadBarrierMarkSlowPathX86(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007294 instruction, ref, /* unpoison_ref_before_marking */ true);
7295 }
Vladimir Marko953437b2016-08-24 08:30:46 +00007296 AddSlowPath(slow_path);
7297
7298 // We have done the "if" of the gray bit check above, now branch based on the flags.
7299 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain7c1559a2015-12-15 10:55:36 +00007300
7301 // Object* ref = ref_addr->AsMirrorPtr()
7302 __ MaybeUnpoisonHeapReference(ref_reg);
7303
Roland Levillain7c1559a2015-12-15 10:55:36 +00007304 __ Bind(slow_path->GetExitLabel());
7305}
7306
7307void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
7308 Location out,
7309 Location ref,
7310 Location obj,
7311 uint32_t offset,
7312 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007313 DCHECK(kEmitCompilerReadBarrier);
7314
Roland Levillain7c1559a2015-12-15 10:55:36 +00007315 // Insert a slow path based read barrier *after* the reference load.
7316 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007317 // If heap poisoning is enabled, the unpoisoning of the loaded
7318 // reference will be carried out by the runtime within the slow
7319 // path.
7320 //
7321 // Note that `ref` currently does not get unpoisoned (when heap
7322 // poisoning is enabled), which is alright as the `ref` argument is
7323 // not used by the artReadBarrierSlow entry point.
7324 //
7325 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Markoca6fff82017-10-03 14:49:14 +01007326 SlowPathCode* slow_path = new (GetGraph()->GetAllocator())
Roland Levillain0d5a2812015-11-13 10:07:31 +00007327 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
7328 AddSlowPath(slow_path);
7329
Roland Levillain0d5a2812015-11-13 10:07:31 +00007330 __ jmp(slow_path->GetEntryLabel());
7331 __ Bind(slow_path->GetExitLabel());
7332}
7333
Roland Levillain7c1559a2015-12-15 10:55:36 +00007334void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7335 Location out,
7336 Location ref,
7337 Location obj,
7338 uint32_t offset,
7339 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007340 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007341 // Baker's read barriers shall be handled by the fast path
7342 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
7343 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007344 // If heap poisoning is enabled, unpoisoning will be taken care of
7345 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007346 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007347 } else if (kPoisonHeapReferences) {
7348 __ UnpoisonHeapReference(out.AsRegister<Register>());
7349 }
7350}
7351
Roland Levillain7c1559a2015-12-15 10:55:36 +00007352void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7353 Location out,
7354 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007355 DCHECK(kEmitCompilerReadBarrier);
7356
Roland Levillain7c1559a2015-12-15 10:55:36 +00007357 // Insert a slow path based read barrier *after* the GC root load.
7358 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007359 // Note that GC roots are not affected by heap poisoning, so we do
7360 // not need to do anything special for this here.
7361 SlowPathCode* slow_path =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007362 new (GetGraph()->GetAllocator()) ReadBarrierForRootSlowPathX86(instruction, out, root);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007363 AddSlowPath(slow_path);
7364
Roland Levillain0d5a2812015-11-13 10:07:31 +00007365 __ jmp(slow_path->GetEntryLabel());
7366 __ Bind(slow_path->GetExitLabel());
7367}
7368
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007369void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007370 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007371 LOG(FATAL) << "Unreachable";
7372}
7373
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007374void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007375 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007376 LOG(FATAL) << "Unreachable";
7377}
7378
Mark Mendellfe57faa2015-09-18 09:26:15 -04007379// Simple implementation of packed switch - generate cascaded compare/jumps.
7380void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7381 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007382 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendellfe57faa2015-09-18 09:26:15 -04007383 locations->SetInAt(0, Location::RequiresRegister());
7384}
7385
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007386void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
7387 int32_t lower_bound,
7388 uint32_t num_entries,
7389 HBasicBlock* switch_block,
7390 HBasicBlock* default_block) {
7391 // Figure out the correct compare values and jump conditions.
7392 // Handle the first compare/branch as a special case because it might
7393 // jump to the default case.
7394 DCHECK_GT(num_entries, 2u);
7395 Condition first_condition;
7396 uint32_t index;
7397 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
7398 if (lower_bound != 0) {
7399 first_condition = kLess;
7400 __ cmpl(value_reg, Immediate(lower_bound));
7401 __ j(first_condition, codegen_->GetLabelOf(default_block));
7402 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007403
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007404 index = 1;
7405 } else {
7406 // Handle all the compare/jumps below.
7407 first_condition = kBelow;
7408 index = 0;
7409 }
7410
7411 // Handle the rest of the compare/jumps.
7412 for (; index + 1 < num_entries; index += 2) {
7413 int32_t compare_to_value = lower_bound + index + 1;
7414 __ cmpl(value_reg, Immediate(compare_to_value));
7415 // Jump to successors[index] if value < case_value[index].
7416 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
7417 // Jump to successors[index + 1] if value == case_value[index + 1].
7418 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
7419 }
7420
7421 if (index != num_entries) {
7422 // There are an odd number of entries. Handle the last one.
7423 DCHECK_EQ(index + 1, num_entries);
7424 __ cmpl(value_reg, Immediate(lower_bound + index));
7425 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007426 }
7427
7428 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007429 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
7430 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007431 }
7432}
7433
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007434void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7435 int32_t lower_bound = switch_instr->GetStartValue();
7436 uint32_t num_entries = switch_instr->GetNumEntries();
7437 LocationSummary* locations = switch_instr->GetLocations();
7438 Register value_reg = locations->InAt(0).AsRegister<Register>();
7439
7440 GenPackedSwitchWithCompares(value_reg,
7441 lower_bound,
7442 num_entries,
7443 switch_instr->GetBlock(),
7444 switch_instr->GetDefaultBlock());
7445}
7446
Mark Mendell805b3b52015-09-18 14:10:29 -04007447void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7448 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007449 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendell805b3b52015-09-18 14:10:29 -04007450 locations->SetInAt(0, Location::RequiresRegister());
7451
7452 // Constant area pointer.
7453 locations->SetInAt(1, Location::RequiresRegister());
7454
7455 // And the temporary we need.
7456 locations->AddTemp(Location::RequiresRegister());
7457}
7458
7459void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7460 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007461 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04007462 LocationSummary* locations = switch_instr->GetLocations();
7463 Register value_reg = locations->InAt(0).AsRegister<Register>();
7464 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7465
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007466 if (num_entries <= kPackedSwitchJumpTableThreshold) {
7467 GenPackedSwitchWithCompares(value_reg,
7468 lower_bound,
7469 num_entries,
7470 switch_instr->GetBlock(),
7471 default_block);
7472 return;
7473 }
7474
Mark Mendell805b3b52015-09-18 14:10:29 -04007475 // Optimizing has a jump area.
7476 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7477 Register constant_area = locations->InAt(1).AsRegister<Register>();
7478
7479 // Remove the bias, if needed.
7480 if (lower_bound != 0) {
7481 __ leal(temp_reg, Address(value_reg, -lower_bound));
7482 value_reg = temp_reg;
7483 }
7484
7485 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007486 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04007487 __ cmpl(value_reg, Immediate(num_entries - 1));
7488 __ j(kAbove, codegen_->GetLabelOf(default_block));
7489
7490 // We are in the range of the table.
7491 // Load (target-constant_area) from the jump table, indexing by the value.
7492 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
7493
7494 // Compute the actual target address by adding in constant_area.
7495 __ addl(temp_reg, constant_area);
7496
7497 // And jump.
7498 __ jmp(temp_reg);
7499}
7500
Mark Mendell0616ae02015-04-17 12:49:27 -04007501void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
7502 HX86ComputeBaseMethodAddress* insn) {
7503 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007504 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Mark Mendell0616ae02015-04-17 12:49:27 -04007505 locations->SetOut(Location::RequiresRegister());
7506}
7507
7508void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
7509 HX86ComputeBaseMethodAddress* insn) {
7510 LocationSummary* locations = insn->GetLocations();
7511 Register reg = locations->Out().AsRegister<Register>();
7512
7513 // Generate call to next instruction.
7514 Label next_instruction;
7515 __ call(&next_instruction);
7516 __ Bind(&next_instruction);
7517
7518 // Remember this offset for later use with constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007519 codegen_->AddMethodAddressOffset(insn, GetAssembler()->CodeSize());
Mark Mendell0616ae02015-04-17 12:49:27 -04007520
7521 // Grab the return address off the stack.
7522 __ popl(reg);
7523}
7524
7525void LocationsBuilderX86::VisitX86LoadFromConstantTable(
7526 HX86LoadFromConstantTable* insn) {
7527 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007528 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Mark Mendell0616ae02015-04-17 12:49:27 -04007529
7530 locations->SetInAt(0, Location::RequiresRegister());
7531 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
7532
7533 // If we don't need to be materialized, we only need the inputs to be set.
David Brazdilb3e773e2016-01-26 11:28:37 +00007534 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007535 return;
7536 }
7537
7538 switch (insn->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007539 case DataType::Type::kFloat32:
7540 case DataType::Type::kFloat64:
Mark Mendell0616ae02015-04-17 12:49:27 -04007541 locations->SetOut(Location::RequiresFpuRegister());
7542 break;
7543
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007544 case DataType::Type::kInt32:
Mark Mendell0616ae02015-04-17 12:49:27 -04007545 locations->SetOut(Location::RequiresRegister());
7546 break;
7547
7548 default:
7549 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7550 }
7551}
7552
7553void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
David Brazdilb3e773e2016-01-26 11:28:37 +00007554 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007555 return;
7556 }
7557
7558 LocationSummary* locations = insn->GetLocations();
7559 Location out = locations->Out();
7560 Register const_area = locations->InAt(0).AsRegister<Register>();
7561 HConstant *value = insn->GetConstant();
7562
7563 switch (insn->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007564 case DataType::Type::kFloat32:
Mark Mendell0616ae02015-04-17 12:49:27 -04007565 __ movss(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007566 codegen_->LiteralFloatAddress(
7567 value->AsFloatConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04007568 break;
7569
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007570 case DataType::Type::kFloat64:
Mark Mendell0616ae02015-04-17 12:49:27 -04007571 __ movsd(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007572 codegen_->LiteralDoubleAddress(
7573 value->AsDoubleConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04007574 break;
7575
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007576 case DataType::Type::kInt32:
Mark Mendell0616ae02015-04-17 12:49:27 -04007577 __ movl(out.AsRegister<Register>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007578 codegen_->LiteralInt32Address(
7579 value->AsIntConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04007580 break;
7581
7582 default:
7583 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7584 }
7585}
7586
Mark Mendell0616ae02015-04-17 12:49:27 -04007587/**
7588 * Class to handle late fixup of offsets into constant area.
7589 */
Vladimir Marko5233f932015-09-29 19:01:15 +01007590class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04007591 public:
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007592 RIPFixup(CodeGeneratorX86& codegen,
7593 HX86ComputeBaseMethodAddress* base_method_address,
7594 size_t offset)
7595 : codegen_(&codegen),
7596 base_method_address_(base_method_address),
7597 offset_into_constant_area_(offset) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04007598
7599 protected:
7600 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
7601
7602 CodeGeneratorX86* codegen_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007603 HX86ComputeBaseMethodAddress* base_method_address_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007604
7605 private:
7606 void Process(const MemoryRegion& region, int pos) OVERRIDE {
7607 // Patch the correct offset for the instruction. The place to patch is the
7608 // last 4 bytes of the instruction.
7609 // The value to patch is the distance from the offset in the constant area
7610 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04007611 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007612 int32_t relative_position =
7613 constant_offset - codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell0616ae02015-04-17 12:49:27 -04007614
7615 // Patch in the right value.
7616 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
7617 }
7618
Mark Mendell0616ae02015-04-17 12:49:27 -04007619 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04007620 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007621};
7622
Mark Mendell805b3b52015-09-18 14:10:29 -04007623/**
7624 * Class to handle late fixup of offsets to a jump table that will be created in the
7625 * constant area.
7626 */
7627class JumpTableRIPFixup : public RIPFixup {
7628 public:
7629 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007630 : RIPFixup(codegen, switch_instr->GetBaseMethodAddress(), static_cast<size_t>(-1)),
7631 switch_instr_(switch_instr) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04007632
7633 void CreateJumpTable() {
7634 X86Assembler* assembler = codegen_->GetAssembler();
7635
7636 // Ensure that the reference to the jump table has the correct offset.
7637 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
7638 SetOffset(offset_in_constant_table);
7639
7640 // The label values in the jump table are computed relative to the
7641 // instruction addressing the constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007642 const int32_t relative_offset = codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell805b3b52015-09-18 14:10:29 -04007643
7644 // Populate the jump table with the correct values for the jump table.
7645 int32_t num_entries = switch_instr_->GetNumEntries();
7646 HBasicBlock* block = switch_instr_->GetBlock();
7647 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
7648 // The value that we want is the target offset - the position of the table.
7649 for (int32_t i = 0; i < num_entries; i++) {
7650 HBasicBlock* b = successors[i];
7651 Label* l = codegen_->GetLabelOf(b);
7652 DCHECK(l->IsBound());
7653 int32_t offset_to_block = l->Position() - relative_offset;
7654 assembler->AppendInt32(offset_to_block);
7655 }
7656 }
7657
7658 private:
7659 const HX86PackedSwitch* switch_instr_;
7660};
7661
7662void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
7663 // Generate the constant area if needed.
7664 X86Assembler* assembler = GetAssembler();
7665 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
7666 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
7667 // byte values.
7668 assembler->Align(4, 0);
7669 constant_area_start_ = assembler->CodeSize();
7670
7671 // Populate any jump tables.
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007672 for (JumpTableRIPFixup* jump_table : fixups_to_jump_tables_) {
Mark Mendell805b3b52015-09-18 14:10:29 -04007673 jump_table->CreateJumpTable();
7674 }
7675
7676 // And now add the constant area to the generated code.
7677 assembler->AddConstantArea();
7678 }
7679
7680 // And finish up.
7681 CodeGenerator::Finalize(allocator);
7682}
7683
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007684Address CodeGeneratorX86::LiteralDoubleAddress(double v,
7685 HX86ComputeBaseMethodAddress* method_base,
7686 Register reg) {
7687 AssemblerFixup* fixup =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007688 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddDouble(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04007689 return Address(reg, kDummy32BitOffset, fixup);
7690}
7691
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007692Address CodeGeneratorX86::LiteralFloatAddress(float v,
7693 HX86ComputeBaseMethodAddress* method_base,
7694 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007695 AssemblerFixup* fixup =
7696 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddFloat(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04007697 return Address(reg, kDummy32BitOffset, fixup);
7698}
7699
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007700Address CodeGeneratorX86::LiteralInt32Address(int32_t v,
7701 HX86ComputeBaseMethodAddress* method_base,
7702 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007703 AssemblerFixup* fixup =
7704 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddInt32(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04007705 return Address(reg, kDummy32BitOffset, fixup);
7706}
7707
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007708Address CodeGeneratorX86::LiteralInt64Address(int64_t v,
7709 HX86ComputeBaseMethodAddress* method_base,
7710 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007711 AssemblerFixup* fixup =
7712 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddInt64(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04007713 return Address(reg, kDummy32BitOffset, fixup);
7714}
7715
Aart Bika19616e2016-02-01 18:57:58 -08007716void CodeGeneratorX86::Load32BitValue(Register dest, int32_t value) {
7717 if (value == 0) {
7718 __ xorl(dest, dest);
7719 } else {
7720 __ movl(dest, Immediate(value));
7721 }
7722}
7723
7724void CodeGeneratorX86::Compare32BitValue(Register dest, int32_t value) {
7725 if (value == 0) {
7726 __ testl(dest, dest);
7727 } else {
7728 __ cmpl(dest, Immediate(value));
7729 }
7730}
7731
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007732void CodeGeneratorX86::GenerateIntCompare(Location lhs, Location rhs) {
7733 Register lhs_reg = lhs.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07007734 GenerateIntCompare(lhs_reg, rhs);
7735}
7736
7737void CodeGeneratorX86::GenerateIntCompare(Register lhs, Location rhs) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007738 if (rhs.IsConstant()) {
7739 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
jessicahandojo4877b792016-09-08 19:49:13 -07007740 Compare32BitValue(lhs, value);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007741 } else if (rhs.IsStackSlot()) {
jessicahandojo4877b792016-09-08 19:49:13 -07007742 __ cmpl(lhs, Address(ESP, rhs.GetStackIndex()));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007743 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07007744 __ cmpl(lhs, rhs.AsRegister<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007745 }
7746}
7747
7748Address CodeGeneratorX86::ArrayAddress(Register obj,
7749 Location index,
7750 ScaleFactor scale,
7751 uint32_t data_offset) {
7752 return index.IsConstant() ?
7753 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << scale) + data_offset) :
7754 Address(obj, index.AsRegister<Register>(), scale, data_offset);
7755}
7756
Mark Mendell805b3b52015-09-18 14:10:29 -04007757Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
7758 Register reg,
7759 Register value) {
7760 // Create a fixup to be used to create and address the jump table.
7761 JumpTableRIPFixup* table_fixup =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007762 new (GetGraph()->GetAllocator()) JumpTableRIPFixup(*this, switch_instr);
Mark Mendell805b3b52015-09-18 14:10:29 -04007763
7764 // We have to populate the jump tables.
7765 fixups_to_jump_tables_.push_back(table_fixup);
7766
7767 // We want a scaled address, as we are extracting the correct offset from the table.
7768 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
7769}
7770
Andreas Gampe85b62f22015-09-09 13:15:38 -07007771// TODO: target as memory.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007772void CodeGeneratorX86::MoveFromReturnRegister(Location target, DataType::Type type) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07007773 if (!target.IsValid()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007774 DCHECK_EQ(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007775 return;
7776 }
7777
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007778 DCHECK_NE(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007779
7780 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
7781 if (target.Equals(return_loc)) {
7782 return;
7783 }
7784
7785 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7786 // with the else branch.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007787 if (type == DataType::Type::kInt64) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007788 HParallelMove parallel_move(GetGraph()->GetAllocator());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007789 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), DataType::Type::kInt32, nullptr);
7790 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), DataType::Type::kInt32, nullptr);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007791 GetMoveResolver()->EmitNativeCode(&parallel_move);
7792 } else {
7793 // Let the parallel move resolver take care of all of this.
Vladimir Markoca6fff82017-10-03 14:49:14 +01007794 HParallelMove parallel_move(GetGraph()->GetAllocator());
Andreas Gampe85b62f22015-09-09 13:15:38 -07007795 parallel_move.AddMove(return_loc, target, type, nullptr);
7796 GetMoveResolver()->EmitNativeCode(&parallel_move);
7797 }
7798}
7799
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007800void CodeGeneratorX86::PatchJitRootUse(uint8_t* code,
7801 const uint8_t* roots_data,
7802 const PatchInfo<Label>& info,
7803 uint64_t index_in_table) const {
7804 uint32_t code_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
7805 uintptr_t address =
7806 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
7807 typedef __attribute__((__aligned__(1))) uint32_t unaligned_uint32_t;
7808 reinterpret_cast<unaligned_uint32_t*>(code + code_offset)[0] =
7809 dchecked_integral_cast<uint32_t>(address);
7810}
7811
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007812void CodeGeneratorX86::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
7813 for (const PatchInfo<Label>& info : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007814 const auto it = jit_string_roots_.find(
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007815 StringReference(&info.dex_file, dex::StringIndex(info.index)));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007816 DCHECK(it != jit_string_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007817 uint64_t index_in_table = it->second;
7818 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007819 }
7820
7821 for (const PatchInfo<Label>& info : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007822 const auto it = jit_class_roots_.find(
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007823 TypeReference(&info.dex_file, dex::TypeIndex(info.index)));
7824 DCHECK(it != jit_class_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007825 uint64_t index_in_table = it->second;
7826 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007827 }
7828}
7829
Roland Levillain4d027112015-07-01 15:41:14 +01007830#undef __
7831
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007832} // namespace x86
7833} // namespace art