blob: 2ab43592ec2dafcb519aa35886ccfcdf19dcea06 [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 Marko174b2e22017-10-12 13:34:49 +01003585 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) 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 Marko174b2e22017-10-12 13:34:49 +01003821 SlowPathCode* slow_path =
3822 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003823 codegen_->AddSlowPath(slow_path);
3824
3825 LocationSummary* locations = instruction->GetLocations();
3826 Location value = locations->InAt(0);
3827
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003828 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003829 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003830 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003831 case DataType::Type::kInt8:
3832 case DataType::Type::kUint16:
3833 case DataType::Type::kInt16:
3834 case DataType::Type::kInt32: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003835 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003836 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003837 __ j(kEqual, slow_path->GetEntryLabel());
3838 } else if (value.IsStackSlot()) {
3839 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
3840 __ j(kEqual, slow_path->GetEntryLabel());
3841 } else {
3842 DCHECK(value.IsConstant()) << value;
3843 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01003844 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003845 }
3846 }
3847 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003848 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003849 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003850 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003851 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003852 __ movl(temp, value.AsRegisterPairLow<Register>());
3853 __ orl(temp, value.AsRegisterPairHigh<Register>());
3854 __ j(kEqual, slow_path->GetEntryLabel());
3855 } else {
3856 DCHECK(value.IsConstant()) << value;
3857 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3858 __ jmp(slow_path->GetEntryLabel());
3859 }
3860 }
3861 break;
3862 }
3863 default:
3864 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003865 }
Calin Juravled0d48522014-11-04 16:40:20 +00003866}
3867
Calin Juravle9aec02f2014-11-18 23:06:35 +00003868void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3869 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3870
3871 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003872 new (GetGraph()->GetAllocator()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003873
3874 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003875 case DataType::Type::kInt32:
3876 case DataType::Type::kInt64: {
Mark P Mendell73945692015-04-29 14:56:17 +00003877 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003878 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003879 // The shift count needs to be in CL or a constant.
3880 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003881 locations->SetOut(Location::SameAsFirstInput());
3882 break;
3883 }
3884 default:
3885 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3886 }
3887}
3888
3889void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3890 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3891
3892 LocationSummary* locations = op->GetLocations();
3893 Location first = locations->InAt(0);
3894 Location second = locations->InAt(1);
3895 DCHECK(first.Equals(locations->Out()));
3896
3897 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003898 case DataType::Type::kInt32: {
Mark P Mendell73945692015-04-29 14:56:17 +00003899 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003900 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003901 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003902 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003903 DCHECK_EQ(ECX, second_reg);
3904 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003905 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003906 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003907 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003908 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003909 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003910 }
3911 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003912 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003913 if (shift == 0) {
3914 return;
3915 }
3916 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003917 if (op->IsShl()) {
3918 __ shll(first_reg, imm);
3919 } else if (op->IsShr()) {
3920 __ sarl(first_reg, imm);
3921 } else {
3922 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003923 }
3924 }
3925 break;
3926 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003927 case DataType::Type::kInt64: {
Mark P Mendell73945692015-04-29 14:56:17 +00003928 if (second.IsRegister()) {
3929 Register second_reg = second.AsRegister<Register>();
3930 DCHECK_EQ(ECX, second_reg);
3931 if (op->IsShl()) {
3932 GenerateShlLong(first, second_reg);
3933 } else if (op->IsShr()) {
3934 GenerateShrLong(first, second_reg);
3935 } else {
3936 GenerateUShrLong(first, second_reg);
3937 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003938 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003939 // Shift by a constant.
Roland Levillain5b5b9312016-03-22 14:57:31 +00003940 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003941 // Nothing to do if the shift is 0, as the input is already the output.
3942 if (shift != 0) {
3943 if (op->IsShl()) {
3944 GenerateShlLong(first, shift);
3945 } else if (op->IsShr()) {
3946 GenerateShrLong(first, shift);
3947 } else {
3948 GenerateUShrLong(first, shift);
3949 }
3950 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003951 }
3952 break;
3953 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003954 default:
3955 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3956 }
3957}
3958
Mark P Mendell73945692015-04-29 14:56:17 +00003959void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3960 Register low = loc.AsRegisterPairLow<Register>();
3961 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003962 if (shift == 1) {
3963 // This is just an addition.
3964 __ addl(low, low);
3965 __ adcl(high, high);
3966 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003967 // Shift by 32 is easy. High gets low, and low gets 0.
3968 codegen_->EmitParallelMoves(
3969 loc.ToLow(),
3970 loc.ToHigh(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003971 DataType::Type::kInt32,
Mark P Mendell73945692015-04-29 14:56:17 +00003972 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3973 loc.ToLow(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003974 DataType::Type::kInt32);
Mark P Mendell73945692015-04-29 14:56:17 +00003975 } else if (shift > 32) {
3976 // Low part becomes 0. High part is low part << (shift-32).
3977 __ movl(high, low);
3978 __ shll(high, Immediate(shift - 32));
3979 __ xorl(low, low);
3980 } else {
3981 // Between 1 and 31.
3982 __ shld(high, low, Immediate(shift));
3983 __ shll(low, Immediate(shift));
3984 }
3985}
3986
Calin Juravle9aec02f2014-11-18 23:06:35 +00003987void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003988 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003989 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
3990 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
3991 __ testl(shifter, Immediate(32));
3992 __ j(kEqual, &done);
3993 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
3994 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
3995 __ Bind(&done);
3996}
3997
Mark P Mendell73945692015-04-29 14:56:17 +00003998void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
3999 Register low = loc.AsRegisterPairLow<Register>();
4000 Register high = loc.AsRegisterPairHigh<Register>();
4001 if (shift == 32) {
4002 // Need to copy the sign.
4003 DCHECK_NE(low, high);
4004 __ movl(low, high);
4005 __ sarl(high, Immediate(31));
4006 } else if (shift > 32) {
4007 DCHECK_NE(low, high);
4008 // High part becomes sign. Low part is shifted by shift - 32.
4009 __ movl(low, high);
4010 __ sarl(high, Immediate(31));
4011 __ sarl(low, Immediate(shift - 32));
4012 } else {
4013 // Between 1 and 31.
4014 __ shrd(low, high, Immediate(shift));
4015 __ sarl(high, Immediate(shift));
4016 }
4017}
4018
Calin Juravle9aec02f2014-11-18 23:06:35 +00004019void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004020 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004021 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4022 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
4023 __ testl(shifter, Immediate(32));
4024 __ j(kEqual, &done);
4025 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4026 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
4027 __ Bind(&done);
4028}
4029
Mark P Mendell73945692015-04-29 14:56:17 +00004030void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
4031 Register low = loc.AsRegisterPairLow<Register>();
4032 Register high = loc.AsRegisterPairHigh<Register>();
4033 if (shift == 32) {
4034 // Shift by 32 is easy. Low gets high, and high gets 0.
4035 codegen_->EmitParallelMoves(
4036 loc.ToHigh(),
4037 loc.ToLow(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004038 DataType::Type::kInt32,
Mark P Mendell73945692015-04-29 14:56:17 +00004039 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
4040 loc.ToHigh(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004041 DataType::Type::kInt32);
Mark P Mendell73945692015-04-29 14:56:17 +00004042 } else if (shift > 32) {
4043 // Low part is high >> (shift - 32). High part becomes 0.
4044 __ movl(low, high);
4045 __ shrl(low, Immediate(shift - 32));
4046 __ xorl(high, high);
4047 } else {
4048 // Between 1 and 31.
4049 __ shrd(low, high, Immediate(shift));
4050 __ shrl(high, Immediate(shift));
4051 }
4052}
4053
Calin Juravle9aec02f2014-11-18 23:06:35 +00004054void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004055 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004056 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4057 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
4058 __ testl(shifter, Immediate(32));
4059 __ j(kEqual, &done);
4060 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4061 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
4062 __ Bind(&done);
4063}
4064
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004065void LocationsBuilderX86::VisitRor(HRor* ror) {
4066 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004067 new (GetGraph()->GetAllocator()) LocationSummary(ror, LocationSummary::kNoCall);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004068
4069 switch (ror->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004070 case DataType::Type::kInt64:
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004071 // Add the temporary needed.
4072 locations->AddTemp(Location::RequiresRegister());
4073 FALLTHROUGH_INTENDED;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004074 case DataType::Type::kInt32:
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004075 locations->SetInAt(0, Location::RequiresRegister());
4076 // The shift count needs to be in CL (unless it is a constant).
4077 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
4078 locations->SetOut(Location::SameAsFirstInput());
4079 break;
4080 default:
4081 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4082 UNREACHABLE();
4083 }
4084}
4085
4086void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
4087 LocationSummary* locations = ror->GetLocations();
4088 Location first = locations->InAt(0);
4089 Location second = locations->InAt(1);
4090
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004091 if (ror->GetResultType() == DataType::Type::kInt32) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004092 Register first_reg = first.AsRegister<Register>();
4093 if (second.IsRegister()) {
4094 Register second_reg = second.AsRegister<Register>();
4095 __ rorl(first_reg, second_reg);
4096 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004097 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004098 __ rorl(first_reg, imm);
4099 }
4100 return;
4101 }
4102
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004103 DCHECK_EQ(ror->GetResultType(), DataType::Type::kInt64);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004104 Register first_reg_lo = first.AsRegisterPairLow<Register>();
4105 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
4106 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
4107 if (second.IsRegister()) {
4108 Register second_reg = second.AsRegister<Register>();
4109 DCHECK_EQ(second_reg, ECX);
4110 __ movl(temp_reg, first_reg_hi);
4111 __ shrd(first_reg_hi, first_reg_lo, second_reg);
4112 __ shrd(first_reg_lo, temp_reg, second_reg);
4113 __ movl(temp_reg, first_reg_hi);
4114 __ testl(second_reg, Immediate(32));
4115 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
4116 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
4117 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004118 int32_t shift_amt = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004119 if (shift_amt == 0) {
4120 // Already fine.
4121 return;
4122 }
4123 if (shift_amt == 32) {
4124 // Just swap.
4125 __ movl(temp_reg, first_reg_lo);
4126 __ movl(first_reg_lo, first_reg_hi);
4127 __ movl(first_reg_hi, temp_reg);
4128 return;
4129 }
4130
4131 Immediate imm(shift_amt);
4132 // Save the constents of the low value.
4133 __ movl(temp_reg, first_reg_lo);
4134
4135 // Shift right into low, feeding bits from high.
4136 __ shrd(first_reg_lo, first_reg_hi, imm);
4137
4138 // Shift right into high, feeding bits from the original low.
4139 __ shrd(first_reg_hi, temp_reg, imm);
4140
4141 // Swap if needed.
4142 if (shift_amt > 32) {
4143 __ movl(temp_reg, first_reg_lo);
4144 __ movl(first_reg_lo, first_reg_hi);
4145 __ movl(first_reg_hi, temp_reg);
4146 }
4147 }
4148}
4149
Calin Juravle9aec02f2014-11-18 23:06:35 +00004150void LocationsBuilderX86::VisitShl(HShl* shl) {
4151 HandleShift(shl);
4152}
4153
4154void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
4155 HandleShift(shl);
4156}
4157
4158void LocationsBuilderX86::VisitShr(HShr* shr) {
4159 HandleShift(shr);
4160}
4161
4162void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
4163 HandleShift(shr);
4164}
4165
4166void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
4167 HandleShift(ushr);
4168}
4169
4170void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
4171 HandleShift(ushr);
4172}
4173
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004174void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004175 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
4176 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004177 locations->SetOut(Location::RegisterLocation(EAX));
David Brazdil6de19382016-01-08 17:37:10 +00004178 if (instruction->IsStringAlloc()) {
4179 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4180 } else {
4181 InvokeRuntimeCallingConvention calling_convention;
4182 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00004183 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004184}
4185
4186void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004187 // Note: if heap poisoning is enabled, the entry point takes cares
4188 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004189 if (instruction->IsStringAlloc()) {
4190 // String is allocated through StringFactory. Call NewEmptyString entry point.
4191 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07004192 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004193 __ fs()->movl(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString)));
4194 __ call(Address(temp, code_offset.Int32Value()));
4195 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4196 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01004197 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00004198 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00004199 DCHECK(!codegen_->IsLeafMethod());
4200 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004201}
4202
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004203void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004204 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
4205 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004206 locations->SetOut(Location::RegisterLocation(EAX));
4207 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004208 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4209 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004210}
4211
4212void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004213 // Note: if heap poisoning is enabled, the entry point takes cares
4214 // of poisoning the reference.
Nicolas Geoffrayd0958442017-01-30 14:57:16 +00004215 QuickEntrypointEnum entrypoint =
4216 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
4217 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004218 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004219 DCHECK(!codegen_->IsLeafMethod());
4220}
4221
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004222void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004223 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004224 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004225 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4226 if (location.IsStackSlot()) {
4227 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4228 } else if (location.IsDoubleStackSlot()) {
4229 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004230 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004231 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004232}
4233
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004234void InstructionCodeGeneratorX86::VisitParameterValue(
4235 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4236}
4237
4238void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
4239 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004240 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004241 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4242}
4243
4244void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004245}
4246
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004247void LocationsBuilderX86::VisitClassTableGet(HClassTableGet* instruction) {
4248 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004249 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004250 locations->SetInAt(0, Location::RequiresRegister());
4251 locations->SetOut(Location::RequiresRegister());
4252}
4253
4254void InstructionCodeGeneratorX86::VisitClassTableGet(HClassTableGet* instruction) {
4255 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004256 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004257 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004258 instruction->GetIndex(), kX86PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004259 __ movl(locations->Out().AsRegister<Register>(),
4260 Address(locations->InAt(0).AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004261 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004262 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004263 instruction->GetIndex(), kX86PointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004264 __ movl(locations->Out().AsRegister<Register>(),
4265 Address(locations->InAt(0).AsRegister<Register>(),
4266 mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
4267 // temp = temp->GetImtEntryAt(method_offset);
4268 __ movl(locations->Out().AsRegister<Register>(),
4269 Address(locations->Out().AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004270 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004271}
4272
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004273void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004274 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004275 new (GetGraph()->GetAllocator()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004276 locations->SetInAt(0, Location::RequiresRegister());
4277 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004278}
4279
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004280void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
4281 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01004282 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004283 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01004284 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004285 switch (not_->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004286 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004287 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004288 break;
4289
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004290 case DataType::Type::kInt64:
Roland Levillain70566432014-10-24 16:20:17 +01004291 __ notl(out.AsRegisterPairLow<Register>());
4292 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004293 break;
4294
4295 default:
4296 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4297 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004298}
4299
David Brazdil66d126e2015-04-03 16:02:44 +01004300void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
4301 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004302 new (GetGraph()->GetAllocator()) LocationSummary(bool_not, LocationSummary::kNoCall);
David Brazdil66d126e2015-04-03 16:02:44 +01004303 locations->SetInAt(0, Location::RequiresRegister());
4304 locations->SetOut(Location::SameAsFirstInput());
4305}
4306
4307void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004308 LocationSummary* locations = bool_not->GetLocations();
4309 Location in = locations->InAt(0);
4310 Location out = locations->Out();
4311 DCHECK(in.Equals(out));
4312 __ xorl(out.AsRegister<Register>(), Immediate(1));
4313}
4314
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004315void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004316 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004317 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004318 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004319 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004320 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004321 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004322 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004323 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004324 case DataType::Type::kInt32:
4325 case DataType::Type::kInt64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00004326 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00004327 locations->SetInAt(1, Location::Any());
4328 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4329 break;
4330 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004331 case DataType::Type::kFloat32:
4332 case DataType::Type::kFloat64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00004333 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004334 if (compare->InputAt(1)->IsX86LoadFromConstantTable()) {
4335 DCHECK(compare->InputAt(1)->IsEmittedAtUseSite());
4336 } else if (compare->InputAt(1)->IsConstant()) {
4337 locations->SetInAt(1, Location::RequiresFpuRegister());
4338 } else {
4339 locations->SetInAt(1, Location::Any());
4340 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004341 locations->SetOut(Location::RequiresRegister());
4342 break;
4343 }
4344 default:
4345 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4346 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004347}
4348
4349void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004350 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004351 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004352 Location left = locations->InAt(0);
4353 Location right = locations->InAt(1);
4354
Mark Mendell0c9497d2015-08-21 09:30:05 -04004355 NearLabel less, greater, done;
Aart Bika19616e2016-02-01 18:57:58 -08004356 Condition less_cond = kLess;
4357
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004358 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004359 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004360 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004361 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004362 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004363 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004364 case DataType::Type::kInt32: {
Roland Levillain0b671c02016-08-19 12:02:34 +01004365 codegen_->GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08004366 break;
4367 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004368 case DataType::Type::kInt64: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004369 Register left_low = left.AsRegisterPairLow<Register>();
4370 Register left_high = left.AsRegisterPairHigh<Register>();
4371 int32_t val_low = 0;
4372 int32_t val_high = 0;
4373 bool right_is_const = false;
4374
4375 if (right.IsConstant()) {
4376 DCHECK(right.GetConstant()->IsLongConstant());
4377 right_is_const = true;
4378 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
4379 val_low = Low32Bits(val);
4380 val_high = High32Bits(val);
4381 }
4382
Calin Juravleddb7df22014-11-25 20:56:51 +00004383 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004384 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004385 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004386 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004387 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004388 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004389 codegen_->Compare32BitValue(left_high, val_high);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004390 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004391 __ j(kLess, &less); // Signed compare.
4392 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004393 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004394 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004395 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004396 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004397 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004398 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004399 codegen_->Compare32BitValue(left_low, val_low);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004400 }
Aart Bika19616e2016-02-01 18:57:58 -08004401 less_cond = kBelow; // for CF (unsigned).
Calin Juravleddb7df22014-11-25 20:56:51 +00004402 break;
4403 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004404 case DataType::Type::kFloat32: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004405 GenerateFPCompare(left, right, compare, false);
Calin Juravleddb7df22014-11-25 20:56:51 +00004406 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004407 less_cond = kBelow; // for CF (floats).
Calin Juravleddb7df22014-11-25 20:56:51 +00004408 break;
4409 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004410 case DataType::Type::kFloat64: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004411 GenerateFPCompare(left, right, compare, true);
Calin Juravleddb7df22014-11-25 20:56:51 +00004412 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004413 less_cond = kBelow; // for CF (floats).
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004414 break;
4415 }
4416 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004417 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004418 }
Aart Bika19616e2016-02-01 18:57:58 -08004419
Calin Juravleddb7df22014-11-25 20:56:51 +00004420 __ movl(out, Immediate(0));
4421 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08004422 __ j(less_cond, &less);
Calin Juravleddb7df22014-11-25 20:56:51 +00004423
4424 __ Bind(&greater);
4425 __ movl(out, Immediate(1));
4426 __ jmp(&done);
4427
4428 __ Bind(&less);
4429 __ movl(out, Immediate(-1));
4430
4431 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004432}
4433
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004434void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004435 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004436 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004437 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004438 locations->SetInAt(i, Location::Any());
4439 }
4440 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004441}
4442
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004443void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004444 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004445}
4446
Roland Levillain7c1559a2015-12-15 10:55:36 +00004447void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004448 /*
4449 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
4450 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
4451 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4452 */
4453 switch (kind) {
4454 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004455 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004456 break;
4457 }
4458 case MemBarrierKind::kAnyStore:
4459 case MemBarrierKind::kLoadAny:
4460 case MemBarrierKind::kStoreStore: {
4461 // nop
4462 break;
4463 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004464 case MemBarrierKind::kNTStoreStore:
4465 // Non-Temporal Store/Store needs an explicit fence.
4466 MemoryFence(/* non-temporal */ true);
4467 break;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004468 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004469}
4470
Vladimir Markodc151b22015-10-15 18:02:30 +01004471HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
4472 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004473 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004474 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004475}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004476
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004477Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4478 Register temp) {
4479 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Markoc53c0792015-11-19 15:48:33 +00004480 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004481 if (!invoke->GetLocations()->Intrinsified()) {
4482 return location.AsRegister<Register>();
4483 }
4484 // For intrinsics we allow any location, so it may be on the stack.
4485 if (!location.IsRegister()) {
4486 __ movl(temp, Address(ESP, location.GetStackIndex()));
4487 return temp;
4488 }
4489 // For register locations, check if the register was saved. If so, get it from the stack.
4490 // Note: There is a chance that the register was saved but not overwritten, so we could
4491 // save one load. However, since this is just an intrinsic slow path we prefer this
4492 // simple and more robust approach rather that trying to determine if that's the case.
4493 SlowPathCode* slow_path = GetCurrentSlowPath();
Vladimir Marko4ee8e292017-06-02 15:39:30 +00004494 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
4495 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4496 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4497 __ movl(temp, Address(ESP, stack_offset));
4498 return temp;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004499 }
4500 return location.AsRegister<Register>();
4501}
4502
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004503void CodeGeneratorX86::GenerateStaticOrDirectCall(
4504 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Vladimir Marko58155012015-08-19 12:49:41 +00004505 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4506 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004507 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00004508 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004509 uint32_t offset =
4510 GetThreadOffset<kX86PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
4511 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004512 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004513 }
Vladimir Marko58155012015-08-19 12:49:41 +00004514 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004515 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004516 break;
Vladimir Marko65979462017-05-19 17:25:12 +01004517 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
4518 DCHECK(GetCompilerOptions().IsBootImage());
4519 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4520 temp.AsRegister<Register>());
4521 __ leal(temp.AsRegister<Register>(), Address(base_reg, CodeGeneratorX86::kDummy32BitOffset));
4522 RecordBootMethodPatch(invoke);
4523 break;
4524 }
Vladimir Marko58155012015-08-19 12:49:41 +00004525 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4526 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
4527 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004528 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004529 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4530 temp.AsRegister<Register>());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004531 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004532 // Bind a new fixup label at the end of the "movl" insn.
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004533 __ Bind(NewMethodBssEntryPatch(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004534 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress(),
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004535 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex())));
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004536 break;
4537 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004538 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
4539 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
4540 return; // No code pointer retrieval; the runtime performs the call directly.
Vladimir Marko9b688a02015-05-06 14:12:42 +01004541 }
Vladimir Marko58155012015-08-19 12:49:41 +00004542 }
4543
4544 switch (invoke->GetCodePtrLocation()) {
4545 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4546 __ call(GetFrameEntryLabel());
4547 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004548 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4549 // (callee_method + offset_of_quick_compiled_code)()
4550 __ call(Address(callee_method.AsRegister<Register>(),
4551 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07004552 kX86PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004553 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04004554 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004555 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Mark Mendell09ed1a32015-03-25 08:30:06 -04004556
4557 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04004558}
4559
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004560void CodeGeneratorX86::GenerateVirtualCall(
4561 HInvokeVirtual* invoke, Location temp_in, SlowPathCode* slow_path) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004562 Register temp = temp_in.AsRegister<Register>();
4563 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4564 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004565
4566 // Use the calling convention instead of the location of the receiver, as
4567 // intrinsics may have put the receiver in a different register. In the intrinsics
4568 // slow path, the arguments have been moved to the right place, so here we are
4569 // guaranteed that the receiver is the first register of the calling convention.
4570 InvokeDexCallingConvention calling_convention;
4571 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004572 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004573 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004574 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004575 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004576 // Instead of simply (possibly) unpoisoning `temp` here, we should
4577 // emit a read barrier for the previous class reference load.
4578 // However this is not required in practice, as this is an
4579 // intermediate/temporary reference and because the current
4580 // concurrent copying collector keeps the from-space memory
4581 // intact/accessible until the end of the marking phase (the
4582 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004583 __ MaybeUnpoisonHeapReference(temp);
4584 // temp = temp->GetMethodAt(method_offset);
4585 __ movl(temp, Address(temp, method_offset));
4586 // call temp->GetEntryPoint();
4587 __ call(Address(
Andreas Gampe542451c2016-07-26 09:02:02 -07004588 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004589 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004590}
4591
Vladimir Marko65979462017-05-19 17:25:12 +01004592void CodeGeneratorX86::RecordBootMethodPatch(HInvokeStaticOrDirect* invoke) {
4593 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
4594 HX86ComputeBaseMethodAddress* address =
4595 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
4596 boot_image_method_patches_.emplace_back(address,
4597 *invoke->GetTargetMethod().dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07004598 invoke->GetTargetMethod().index);
Vladimir Marko65979462017-05-19 17:25:12 +01004599 __ Bind(&boot_image_method_patches_.back().label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004600}
4601
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004602Label* CodeGeneratorX86::NewMethodBssEntryPatch(
4603 HX86ComputeBaseMethodAddress* method_address,
4604 MethodReference target_method) {
4605 // Add the patch entry and bind its label at the end of the instruction.
4606 method_bss_entry_patches_.emplace_back(method_address,
4607 *target_method.dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07004608 target_method.index);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004609 return &method_bss_entry_patches_.back().label;
4610}
4611
Vladimir Marko1998cd02017-01-13 13:02:58 +00004612void CodeGeneratorX86::RecordBootTypePatch(HLoadClass* load_class) {
Vladimir Marko00916b92017-05-17 17:45:45 +01004613 HX86ComputeBaseMethodAddress* address = load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004614 boot_image_type_patches_.emplace_back(address,
4615 load_class->GetDexFile(),
Vladimir Marko1998cd02017-01-13 13:02:58 +00004616 load_class->GetTypeIndex().index_);
4617 __ Bind(&boot_image_type_patches_.back().label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004618}
4619
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004620Label* CodeGeneratorX86::NewTypeBssEntryPatch(HLoadClass* load_class) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004621 HX86ComputeBaseMethodAddress* address =
4622 load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
4623 type_bss_entry_patches_.emplace_back(
4624 address, load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00004625 return &type_bss_entry_patches_.back().label;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004626}
4627
Vladimir Marko65979462017-05-19 17:25:12 +01004628void CodeGeneratorX86::RecordBootStringPatch(HLoadString* load_string) {
Vladimir Marko65979462017-05-19 17:25:12 +01004629 HX86ComputeBaseMethodAddress* address = load_string->InputAt(0)->AsX86ComputeBaseMethodAddress();
4630 string_patches_.emplace_back(address,
4631 load_string->GetDexFile(),
4632 load_string->GetStringIndex().index_);
4633 __ Bind(&string_patches_.back().label);
4634}
4635
Vladimir Markoaad75c62016-10-03 08:46:48 +00004636Label* CodeGeneratorX86::NewStringBssEntryPatch(HLoadString* load_string) {
4637 DCHECK(!GetCompilerOptions().IsBootImage());
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004638 HX86ComputeBaseMethodAddress* address =
4639 load_string->InputAt(0)->AsX86ComputeBaseMethodAddress();
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004640 string_bss_entry_patches_.emplace_back(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004641 address, load_string->GetDexFile(), load_string->GetStringIndex().index_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004642 return &string_bss_entry_patches_.back().label;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004643}
4644
Vladimir Markoaad75c62016-10-03 08:46:48 +00004645// The label points to the end of the "movl" or another instruction but the literal offset
4646// for method patch needs to point to the embedded constant which occupies the last 4 bytes.
4647constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
4648
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004649template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +00004650inline void CodeGeneratorX86::EmitPcRelativeLinkerPatches(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004651 const ArenaDeque<X86PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004652 ArenaVector<linker::LinkerPatch>* linker_patches) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004653 for (const X86PcRelativePatchInfo& info : infos) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00004654 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004655 linker_patches->push_back(Factory(
4656 literal_offset, &info.dex_file, GetMethodAddressOffset(info.method_address), info.index));
Vladimir Markoaad75c62016-10-03 08:46:48 +00004657 }
4658}
4659
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004660void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Marko58155012015-08-19 12:49:41 +00004661 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004662 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01004663 boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004664 method_bss_entry_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +00004665 boot_image_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01004666 type_bss_entry_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004667 string_patches_.size() +
4668 string_bss_entry_patches_.size();
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004669 linker_patches->reserve(size);
Vladimir Marko764d4542017-05-16 10:31:41 +01004670 if (GetCompilerOptions().IsBootImage()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004671 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
4672 boot_image_method_patches_, linker_patches);
4673 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
4674 boot_image_type_patches_, linker_patches);
4675 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
4676 string_patches_, linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004677 } else {
Vladimir Marko65979462017-05-19 17:25:12 +01004678 DCHECK(boot_image_method_patches_.empty());
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004679 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeClassTablePatch>(
4680 boot_image_type_patches_, linker_patches);
4681 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringInternTablePatch>(
4682 string_patches_, linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004683 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004684 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
4685 method_bss_entry_patches_, linker_patches);
4686 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
4687 type_bss_entry_patches_, linker_patches);
4688 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
4689 string_bss_entry_patches_, linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00004690 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00004691}
4692
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004693void CodeGeneratorX86::MarkGCCard(Register temp,
4694 Register card,
4695 Register object,
4696 Register value,
4697 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004698 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004699 if (value_can_be_null) {
4700 __ testl(value, value);
4701 __ j(kEqual, &is_null);
4702 }
Andreas Gampe542451c2016-07-26 09:02:02 -07004703 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86PointerSize>().Int32Value()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004704 __ movl(temp, object);
4705 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004706 __ movb(Address(temp, card, TIMES_1, 0),
4707 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004708 if (value_can_be_null) {
4709 __ Bind(&is_null);
4710 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004711}
4712
Calin Juravle52c48962014-12-16 17:02:57 +00004713void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4714 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00004715
4716 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004717 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004718 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004719 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
4720 kEmitCompilerReadBarrier
4721 ? LocationSummary::kCallOnSlowPath
4722 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004723 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004724 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004725 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004726 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004727
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004728 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004729 locations->SetOut(Location::RequiresFpuRegister());
4730 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004731 // The output overlaps in case of long: we don't want the low move
4732 // to overwrite the object's location. Likewise, in the case of
4733 // an object field get with read barriers enabled, we do not want
4734 // the move to overwrite the object's location, as we need it to emit
4735 // the read barrier.
4736 locations->SetOut(
4737 Location::RequiresRegister(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004738 (object_field_get_with_read_barrier || instruction->GetType() == DataType::Type::kInt64) ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00004739 Location::kOutputOverlap :
4740 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004741 }
Calin Juravle52c48962014-12-16 17:02:57 +00004742
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004743 if (field_info.IsVolatile() && (field_info.GetFieldType() == DataType::Type::kInt64)) {
Calin Juravle52c48962014-12-16 17:02:57 +00004744 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00004745 // So we use an XMM register as a temp to achieve atomicity (first
4746 // load the temp into the XMM and then copy the XMM into the
4747 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00004748 locations->AddTemp(Location::RequiresFpuRegister());
4749 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004750}
4751
Calin Juravle52c48962014-12-16 17:02:57 +00004752void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
4753 const FieldInfo& field_info) {
4754 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004755
Calin Juravle52c48962014-12-16 17:02:57 +00004756 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004757 Location base_loc = locations->InAt(0);
4758 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004759 Location out = locations->Out();
4760 bool is_volatile = field_info.IsVolatile();
Vladimir Marko61b92282017-10-11 13:23:17 +01004761 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
4762 DataType::Type load_type = instruction->GetType();
Calin Juravle52c48962014-12-16 17:02:57 +00004763 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4764
Vladimir Marko61b92282017-10-11 13:23:17 +01004765 switch (load_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004766 case DataType::Type::kBool:
4767 case DataType::Type::kUint8: {
Calin Juravle52c48962014-12-16 17:02:57 +00004768 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004769 break;
4770 }
4771
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004772 case DataType::Type::kInt8: {
Calin Juravle52c48962014-12-16 17:02:57 +00004773 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004774 break;
4775 }
4776
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004777 case DataType::Type::kUint16: {
4778 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004779 break;
4780 }
4781
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004782 case DataType::Type::kInt16: {
4783 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004784 break;
4785 }
4786
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004787 case DataType::Type::kInt32:
Calin Juravle52c48962014-12-16 17:02:57 +00004788 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004789 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00004790
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004791 case DataType::Type::kReference: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004792 // /* HeapReference<Object> */ out = *(base + offset)
4793 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004794 // Note that a potential implicit null check is handled in this
4795 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4796 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00004797 instruction, out, base, offset, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00004798 if (is_volatile) {
4799 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4800 }
4801 } else {
4802 __ movl(out.AsRegister<Register>(), Address(base, offset));
4803 codegen_->MaybeRecordImplicitNullCheck(instruction);
4804 if (is_volatile) {
4805 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4806 }
4807 // If read barriers are enabled, emit read barriers other than
4808 // Baker's using a slow path (and also unpoison the loaded
4809 // reference, if heap poisoning is enabled).
4810 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4811 }
4812 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004813 }
4814
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004815 case DataType::Type::kInt64: {
Calin Juravle52c48962014-12-16 17:02:57 +00004816 if (is_volatile) {
4817 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4818 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004819 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004820 __ movd(out.AsRegisterPairLow<Register>(), temp);
4821 __ psrlq(temp, Immediate(32));
4822 __ movd(out.AsRegisterPairHigh<Register>(), temp);
4823 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004824 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00004825 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004826 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004827 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
4828 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004829 break;
4830 }
4831
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004832 case DataType::Type::kFloat32: {
Calin Juravle52c48962014-12-16 17:02:57 +00004833 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004834 break;
4835 }
4836
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004837 case DataType::Type::kFloat64: {
Calin Juravle52c48962014-12-16 17:02:57 +00004838 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004839 break;
4840 }
4841
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004842 case DataType::Type::kVoid:
Vladimir Marko61b92282017-10-11 13:23:17 +01004843 LOG(FATAL) << "Unreachable type " << load_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004844 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004845 }
Calin Juravle52c48962014-12-16 17:02:57 +00004846
Vladimir Marko61b92282017-10-11 13:23:17 +01004847 if (load_type == DataType::Type::kReference || load_type == DataType::Type::kInt64) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004848 // Potential implicit null checks, in the case of reference or
4849 // long fields, are handled in the previous switch statement.
4850 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004851 codegen_->MaybeRecordImplicitNullCheck(instruction);
4852 }
4853
Calin Juravle52c48962014-12-16 17:02:57 +00004854 if (is_volatile) {
Vladimir Marko61b92282017-10-11 13:23:17 +01004855 if (load_type == DataType::Type::kReference) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004856 // Memory barriers, in the case of references, are also handled
4857 // in the previous switch statement.
4858 } else {
4859 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4860 }
Roland Levillain4d027112015-07-01 15:41:14 +01004861 }
Calin Juravle52c48962014-12-16 17:02:57 +00004862}
4863
4864void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4865 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4866
4867 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004868 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00004869 locations->SetInAt(0, Location::RequiresRegister());
4870 bool is_volatile = field_info.IsVolatile();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004871 DataType::Type field_type = field_info.GetFieldType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004872 bool is_byte_type = DataType::Size(field_type) == 1u;
Calin Juravle52c48962014-12-16 17:02:57 +00004873
4874 // The register allocator does not support multiple
4875 // inputs that die at entry with one in a specific register.
4876 if (is_byte_type) {
4877 // Ensure the value is in a byte register.
4878 locations->SetInAt(1, Location::RegisterLocation(EAX));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004879 } else if (DataType::IsFloatingPointType(field_type)) {
4880 if (is_volatile && field_type == DataType::Type::kFloat64) {
Mark Mendell81489372015-11-04 11:30:41 -05004881 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4882 locations->SetInAt(1, Location::RequiresFpuRegister());
4883 } else {
4884 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4885 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004886 } else if (is_volatile && field_type == DataType::Type::kInt64) {
Mark Mendell81489372015-11-04 11:30:41 -05004887 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00004888 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004889
Calin Juravle52c48962014-12-16 17:02:57 +00004890 // 64bits value can be atomically written to an address with movsd and an XMM register.
4891 // We need two XMM registers because there's no easier way to (bit) copy a register pair
4892 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
4893 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
4894 // isolated cases when we need this it isn't worth adding the extra complexity.
4895 locations->AddTemp(Location::RequiresFpuRegister());
4896 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004897 } else {
4898 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4899
4900 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4901 // Temporary registers for the write barrier.
4902 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
4903 // Ensure the card is in a byte register.
4904 locations->AddTemp(Location::RegisterLocation(ECX));
4905 }
Calin Juravle52c48962014-12-16 17:02:57 +00004906 }
4907}
4908
4909void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004910 const FieldInfo& field_info,
4911 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004912 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4913
4914 LocationSummary* locations = instruction->GetLocations();
4915 Register base = locations->InAt(0).AsRegister<Register>();
4916 Location value = locations->InAt(1);
4917 bool is_volatile = field_info.IsVolatile();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004918 DataType::Type field_type = field_info.GetFieldType();
Calin Juravle52c48962014-12-16 17:02:57 +00004919 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004920 bool needs_write_barrier =
4921 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004922
4923 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004924 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004925 }
4926
Mark Mendell81489372015-11-04 11:30:41 -05004927 bool maybe_record_implicit_null_check_done = false;
4928
Calin Juravle52c48962014-12-16 17:02:57 +00004929 switch (field_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004930 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004931 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004932 case DataType::Type::kInt8: {
Calin Juravle52c48962014-12-16 17:02:57 +00004933 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
4934 break;
4935 }
4936
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004937 case DataType::Type::kUint16:
4938 case DataType::Type::kInt16: {
Mark Mendell81489372015-11-04 11:30:41 -05004939 if (value.IsConstant()) {
Nicolas Geoffray78612082017-07-24 14:18:53 +01004940 __ movw(Address(base, offset),
4941 Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Mark Mendell81489372015-11-04 11:30:41 -05004942 } else {
4943 __ movw(Address(base, offset), value.AsRegister<Register>());
4944 }
Calin Juravle52c48962014-12-16 17:02:57 +00004945 break;
4946 }
4947
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004948 case DataType::Type::kInt32:
4949 case DataType::Type::kReference: {
Roland Levillain4d027112015-07-01 15:41:14 +01004950 if (kPoisonHeapReferences && needs_write_barrier) {
4951 // Note that in the case where `value` is a null reference,
4952 // we do not enter this block, as the reference does not
4953 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004954 DCHECK_EQ(field_type, DataType::Type::kReference);
Roland Levillain4d027112015-07-01 15:41:14 +01004955 Register temp = locations->GetTemp(0).AsRegister<Register>();
4956 __ movl(temp, value.AsRegister<Register>());
4957 __ PoisonHeapReference(temp);
4958 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05004959 } else if (value.IsConstant()) {
4960 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4961 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01004962 } else {
Nicolas Geoffray03971632016-03-17 10:44:24 +00004963 DCHECK(value.IsRegister()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01004964 __ movl(Address(base, offset), value.AsRegister<Register>());
4965 }
Calin Juravle52c48962014-12-16 17:02:57 +00004966 break;
4967 }
4968
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004969 case DataType::Type::kInt64: {
Calin Juravle52c48962014-12-16 17:02:57 +00004970 if (is_volatile) {
4971 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4972 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
4973 __ movd(temp1, value.AsRegisterPairLow<Register>());
4974 __ movd(temp2, value.AsRegisterPairHigh<Register>());
4975 __ punpckldq(temp1, temp2);
4976 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00004977 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05004978 } else if (value.IsConstant()) {
4979 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4980 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4981 codegen_->MaybeRecordImplicitNullCheck(instruction);
4982 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00004983 } else {
4984 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004985 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004986 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
4987 }
Mark Mendell81489372015-11-04 11:30:41 -05004988 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00004989 break;
4990 }
4991
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004992 case DataType::Type::kFloat32: {
Mark Mendell81489372015-11-04 11:30:41 -05004993 if (value.IsConstant()) {
4994 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4995 __ movl(Address(base, offset), Immediate(v));
4996 } else {
4997 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4998 }
Calin Juravle52c48962014-12-16 17:02:57 +00004999 break;
5000 }
5001
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005002 case DataType::Type::kFloat64: {
Mark Mendell81489372015-11-04 11:30:41 -05005003 if (value.IsConstant()) {
5004 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
5005 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
5006 codegen_->MaybeRecordImplicitNullCheck(instruction);
5007 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
5008 maybe_record_implicit_null_check_done = true;
5009 } else {
5010 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
5011 }
Calin Juravle52c48962014-12-16 17:02:57 +00005012 break;
5013 }
5014
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005015 case DataType::Type::kVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00005016 LOG(FATAL) << "Unreachable type " << field_type;
5017 UNREACHABLE();
5018 }
5019
Mark Mendell81489372015-11-04 11:30:41 -05005020 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005021 codegen_->MaybeRecordImplicitNullCheck(instruction);
5022 }
5023
Roland Levillain4d027112015-07-01 15:41:14 +01005024 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005025 Register temp = locations->GetTemp(0).AsRegister<Register>();
5026 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005027 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00005028 }
5029
Calin Juravle52c48962014-12-16 17:02:57 +00005030 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005031 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00005032 }
5033}
5034
5035void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5036 HandleFieldGet(instruction, instruction->GetFieldInfo());
5037}
5038
5039void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5040 HandleFieldGet(instruction, instruction->GetFieldInfo());
5041}
5042
5043void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5044 HandleFieldSet(instruction, instruction->GetFieldInfo());
5045}
5046
5047void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005048 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005049}
5050
5051void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5052 HandleFieldSet(instruction, instruction->GetFieldInfo());
5053}
5054
5055void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005056 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005057}
5058
5059void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5060 HandleFieldGet(instruction, instruction->GetFieldInfo());
5061}
5062
5063void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5064 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005065}
5066
Calin Juravlee460d1d2015-09-29 04:52:17 +01005067void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
5068 HUnresolvedInstanceFieldGet* instruction) {
5069 FieldAccessCallingConventionX86 calling_convention;
5070 codegen_->CreateUnresolvedFieldLocationSummary(
5071 instruction, instruction->GetFieldType(), calling_convention);
5072}
5073
5074void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
5075 HUnresolvedInstanceFieldGet* instruction) {
5076 FieldAccessCallingConventionX86 calling_convention;
5077 codegen_->GenerateUnresolvedFieldAccess(instruction,
5078 instruction->GetFieldType(),
5079 instruction->GetFieldIndex(),
5080 instruction->GetDexPc(),
5081 calling_convention);
5082}
5083
5084void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
5085 HUnresolvedInstanceFieldSet* instruction) {
5086 FieldAccessCallingConventionX86 calling_convention;
5087 codegen_->CreateUnresolvedFieldLocationSummary(
5088 instruction, instruction->GetFieldType(), calling_convention);
5089}
5090
5091void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
5092 HUnresolvedInstanceFieldSet* instruction) {
5093 FieldAccessCallingConventionX86 calling_convention;
5094 codegen_->GenerateUnresolvedFieldAccess(instruction,
5095 instruction->GetFieldType(),
5096 instruction->GetFieldIndex(),
5097 instruction->GetDexPc(),
5098 calling_convention);
5099}
5100
5101void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
5102 HUnresolvedStaticFieldGet* instruction) {
5103 FieldAccessCallingConventionX86 calling_convention;
5104 codegen_->CreateUnresolvedFieldLocationSummary(
5105 instruction, instruction->GetFieldType(), calling_convention);
5106}
5107
5108void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
5109 HUnresolvedStaticFieldGet* instruction) {
5110 FieldAccessCallingConventionX86 calling_convention;
5111 codegen_->GenerateUnresolvedFieldAccess(instruction,
5112 instruction->GetFieldType(),
5113 instruction->GetFieldIndex(),
5114 instruction->GetDexPc(),
5115 calling_convention);
5116}
5117
5118void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
5119 HUnresolvedStaticFieldSet* instruction) {
5120 FieldAccessCallingConventionX86 calling_convention;
5121 codegen_->CreateUnresolvedFieldLocationSummary(
5122 instruction, instruction->GetFieldType(), calling_convention);
5123}
5124
5125void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
5126 HUnresolvedStaticFieldSet* instruction) {
5127 FieldAccessCallingConventionX86 calling_convention;
5128 codegen_->GenerateUnresolvedFieldAccess(instruction,
5129 instruction->GetFieldType(),
5130 instruction->GetFieldIndex(),
5131 instruction->GetDexPc(),
5132 calling_convention);
5133}
5134
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005135void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005136 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5137 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
5138 ? Location::RequiresRegister()
5139 : Location::Any();
5140 locations->SetInAt(0, loc);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005141}
5142
Calin Juravle2ae48182016-03-16 14:05:09 +00005143void CodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
5144 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005145 return;
5146 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005147 LocationSummary* locations = instruction->GetLocations();
5148 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005149
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005150 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00005151 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005152}
5153
Calin Juravle2ae48182016-03-16 14:05:09 +00005154void CodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005155 SlowPathCode* slow_path = new (GetScopedAllocator()) NullCheckSlowPathX86(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005156 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005157
5158 LocationSummary* locations = instruction->GetLocations();
5159 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005160
5161 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04005162 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005163 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005164 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005165 } else {
5166 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00005167 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005168 __ jmp(slow_path->GetEntryLabel());
5169 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005170 }
5171 __ j(kEqual, slow_path->GetEntryLabel());
5172}
5173
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005174void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005175 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005176}
5177
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005178void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005179 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005180 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005181 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005182 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
5183 object_array_get_with_read_barrier
5184 ? LocationSummary::kCallOnSlowPath
5185 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005186 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005187 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005188 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005189 locations->SetInAt(0, Location::RequiresRegister());
5190 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005191 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005192 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5193 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005194 // The output overlaps in case of long: we don't want the low move
5195 // to overwrite the array's location. Likewise, in the case of an
5196 // object array get with read barriers enabled, we do not want the
5197 // move to overwrite the array's location, as we need it to emit
5198 // the read barrier.
5199 locations->SetOut(
5200 Location::RequiresRegister(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005201 (instruction->GetType() == DataType::Type::kInt64 || object_array_get_with_read_barrier)
5202 ? Location::kOutputOverlap
5203 : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005204 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005205}
5206
5207void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
5208 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005209 Location obj_loc = locations->InAt(0);
5210 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005211 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005212 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005213 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005214
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005215 DataType::Type type = instruction->GetType();
Calin Juravle77520bc2015-01-12 18:45:46 +00005216 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005217 case DataType::Type::kBool:
5218 case DataType::Type::kUint8: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005219 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005220 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005221 break;
5222 }
5223
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005224 case DataType::Type::kInt8: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005225 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005226 __ movsxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005227 break;
5228 }
5229
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005230 case DataType::Type::kUint16: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005231 Register out = out_loc.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07005232 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5233 // Branch cases into compressed and uncompressed for each index's type.
5234 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
5235 NearLabel done, not_compressed;
Vladimir Marko3c89d422017-02-17 11:30:23 +00005236 __ testb(Address(obj, count_offset), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005237 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005238 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
5239 "Expecting 0=compressed, 1=uncompressed");
5240 __ j(kNotZero, &not_compressed);
jessicahandojo4877b792016-09-08 19:49:13 -07005241 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
5242 __ jmp(&done);
5243 __ Bind(&not_compressed);
5244 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5245 __ Bind(&done);
5246 } else {
5247 // Common case for charAt of array of char or when string compression's
5248 // feature is turned off.
5249 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5250 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005251 break;
5252 }
5253
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005254 case DataType::Type::kInt16: {
5255 Register out = out_loc.AsRegister<Register>();
5256 __ movsxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5257 break;
5258 }
5259
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005260 case DataType::Type::kInt32: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005261 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005262 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005263 break;
5264 }
5265
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005266 case DataType::Type::kReference: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005267 static_assert(
5268 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5269 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00005270 // /* HeapReference<Object> */ out =
5271 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5272 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005273 // Note that a potential implicit null check is handled in this
5274 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
5275 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00005276 instruction, out_loc, obj, data_offset, index, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005277 } else {
5278 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005279 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
5280 codegen_->MaybeRecordImplicitNullCheck(instruction);
5281 // If read barriers are enabled, emit read barriers other than
5282 // Baker's using a slow path (and also unpoison the loaded
5283 // reference, if heap poisoning is enabled).
Roland Levillain7c1559a2015-12-15 10:55:36 +00005284 if (index.IsConstant()) {
5285 uint32_t offset =
5286 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005287 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5288 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005289 codegen_->MaybeGenerateReadBarrierSlow(
5290 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5291 }
5292 }
5293 break;
5294 }
5295
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005296 case DataType::Type::kInt64: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005297 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005298 __ movl(out_loc.AsRegisterPairLow<Register>(),
5299 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
5300 codegen_->MaybeRecordImplicitNullCheck(instruction);
5301 __ movl(out_loc.AsRegisterPairHigh<Register>(),
5302 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005303 break;
5304 }
5305
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005306 case DataType::Type::kFloat32: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005307 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005308 __ movss(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005309 break;
5310 }
5311
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005312 case DataType::Type::kFloat64: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005313 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005314 __ movsd(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005315 break;
5316 }
5317
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005318 case DataType::Type::kVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00005319 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005320 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005321 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005322
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005323 if (type == DataType::Type::kReference || type == DataType::Type::kInt64) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005324 // Potential implicit null checks, in the case of reference or
5325 // long arrays, are handled in the previous switch statement.
5326 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005327 codegen_->MaybeRecordImplicitNullCheck(instruction);
5328 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005329}
5330
5331void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005332 DataType::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005333
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005334 bool needs_write_barrier =
5335 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005336 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005337
Vladimir Markoca6fff82017-10-03 14:49:14 +01005338 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Nicolas Geoffray39468442014-09-02 15:17:15 +01005339 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01005340 may_need_runtime_call_for_type_check ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00005341 LocationSummary::kCallOnSlowPath :
5342 LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005343
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005344 bool is_byte_type = DataType::Size(value_type) == 1u;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005345 // We need the inputs to be different than the output in case of long operation.
5346 // In case of a byte operation, the register allocator does not support multiple
5347 // inputs that die at entry with one in a specific register.
5348 locations->SetInAt(0, Location::RequiresRegister());
5349 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5350 if (is_byte_type) {
5351 // Ensure the value is in a byte register.
5352 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005353 } else if (DataType::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05005354 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005355 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005356 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
5357 }
5358 if (needs_write_barrier) {
5359 // Temporary registers for the write barrier.
5360 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5361 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005362 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005363 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005364}
5365
5366void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
5367 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005368 Location array_loc = locations->InAt(0);
5369 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005370 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005371 Location value = locations->InAt(2);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005372 DataType::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005373 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5374 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5375 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005376 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005377 bool needs_write_barrier =
5378 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005379
5380 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005381 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005382 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005383 case DataType::Type::kInt8: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005384 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005385 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_1, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005386 if (value.IsRegister()) {
5387 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005388 } else {
Nicolas Geoffray78612082017-07-24 14:18:53 +01005389 __ movb(address, Immediate(CodeGenerator::GetInt8ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005390 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005391 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005392 break;
5393 }
5394
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005395 case DataType::Type::kUint16:
5396 case DataType::Type::kInt16: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005397 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005398 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_2, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005399 if (value.IsRegister()) {
5400 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005401 } else {
Nicolas Geoffray78612082017-07-24 14:18:53 +01005402 __ movw(address, Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005403 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005404 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005405 break;
5406 }
5407
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005408 case DataType::Type::kReference: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005409 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005410 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005411
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005412 if (!value.IsRegister()) {
5413 // Just setting null.
5414 DCHECK(instruction->InputAt(2)->IsNullConstant());
5415 DCHECK(value.IsConstant()) << value;
5416 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00005417 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005418 DCHECK(!needs_write_barrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005419 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005420 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005421 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005422
5423 DCHECK(needs_write_barrier);
5424 Register register_value = value.AsRegister<Register>();
Roland Levillain16d9f942016-08-25 17:27:56 +01005425 // We cannot use a NearLabel for `done`, as its range may be too
5426 // short when Baker read barriers are enabled.
5427 Label done;
5428 NearLabel not_null, do_put;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005429 SlowPathCode* slow_path = nullptr;
Roland Levillain16d9f942016-08-25 17:27:56 +01005430 Location temp_loc = locations->GetTemp(0);
5431 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005432 if (may_need_runtime_call_for_type_check) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005433 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathX86(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005434 codegen_->AddSlowPath(slow_path);
5435 if (instruction->GetValueCanBeNull()) {
5436 __ testl(register_value, register_value);
5437 __ j(kNotEqual, &not_null);
5438 __ movl(address, Immediate(0));
5439 codegen_->MaybeRecordImplicitNullCheck(instruction);
5440 __ jmp(&done);
5441 __ Bind(&not_null);
5442 }
5443
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005444 // Note that when Baker read barriers are enabled, the type
5445 // checks are performed without read barriers. This is fine,
5446 // even in the case where a class object is in the from-space
5447 // after the flip, as a comparison involving such a type would
5448 // not produce a false positive; it may of course produce a
5449 // false negative, in which case we would take the ArraySet
5450 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01005451
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005452 // /* HeapReference<Class> */ temp = array->klass_
5453 __ movl(temp, Address(array, class_offset));
5454 codegen_->MaybeRecordImplicitNullCheck(instruction);
5455 __ MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01005456
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005457 // /* HeapReference<Class> */ temp = temp->component_type_
5458 __ movl(temp, Address(temp, component_offset));
5459 // If heap poisoning is enabled, no need to unpoison `temp`
5460 // nor the object reference in `register_value->klass`, as
5461 // we are comparing two poisoned references.
5462 __ cmpl(temp, Address(register_value, class_offset));
Roland Levillain16d9f942016-08-25 17:27:56 +01005463
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005464 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5465 __ j(kEqual, &do_put);
5466 // If heap poisoning is enabled, the `temp` reference has
5467 // not been unpoisoned yet; unpoison it now.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005468 __ MaybeUnpoisonHeapReference(temp);
5469
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005470 // If heap poisoning is enabled, no need to unpoison the
5471 // heap reference loaded below, as it is only used for a
5472 // comparison with null.
5473 __ cmpl(Address(temp, super_offset), Immediate(0));
5474 __ j(kNotEqual, slow_path->GetEntryLabel());
5475 __ Bind(&do_put);
5476 } else {
5477 __ j(kNotEqual, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005478 }
5479 }
5480
5481 if (kPoisonHeapReferences) {
5482 __ movl(temp, register_value);
5483 __ PoisonHeapReference(temp);
5484 __ movl(address, temp);
5485 } else {
5486 __ movl(address, register_value);
5487 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005488 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005489 codegen_->MaybeRecordImplicitNullCheck(instruction);
5490 }
5491
5492 Register card = locations->GetTemp(1).AsRegister<Register>();
5493 codegen_->MarkGCCard(
5494 temp, card, array, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
5495 __ Bind(&done);
5496
5497 if (slow_path != nullptr) {
5498 __ Bind(slow_path->GetExitLabel());
5499 }
5500
5501 break;
5502 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005503
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005504 case DataType::Type::kInt32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005505 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005506 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005507 if (value.IsRegister()) {
5508 __ movl(address, value.AsRegister<Register>());
5509 } else {
5510 DCHECK(value.IsConstant()) << value;
5511 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5512 __ movl(address, Immediate(v));
5513 }
5514 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005515 break;
5516 }
5517
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005518 case DataType::Type::kInt64: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005519 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005520 if (value.IsRegisterPair()) {
5521 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
5522 value.AsRegisterPairLow<Register>());
5523 codegen_->MaybeRecordImplicitNullCheck(instruction);
5524 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
5525 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005526 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005527 DCHECK(value.IsConstant());
5528 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
5529 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
5530 Immediate(Low32Bits(val)));
5531 codegen_->MaybeRecordImplicitNullCheck(instruction);
5532 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
5533 Immediate(High32Bits(val)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005534 }
5535 break;
5536 }
5537
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005538 case DataType::Type::kFloat32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005539 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005540 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005541 if (value.IsFpuRegister()) {
5542 __ movss(address, value.AsFpuRegister<XmmRegister>());
5543 } else {
5544 DCHECK(value.IsConstant());
5545 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
5546 __ movl(address, Immediate(v));
5547 }
5548 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005549 break;
5550 }
5551
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005552 case DataType::Type::kFloat64: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005553 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005554 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005555 if (value.IsFpuRegister()) {
5556 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5557 } else {
5558 DCHECK(value.IsConstant());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005559 Address address_hi =
5560 CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset + kX86WordSize);
Mark Mendell81489372015-11-04 11:30:41 -05005561 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5562 __ movl(address, Immediate(Low32Bits(v)));
5563 codegen_->MaybeRecordImplicitNullCheck(instruction);
5564 __ movl(address_hi, Immediate(High32Bits(v)));
5565 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005566 break;
5567 }
5568
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005569 case DataType::Type::kVoid:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005570 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005571 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005572 }
5573}
5574
5575void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005576 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005577 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04005578 if (!instruction->IsEmittedAtUseSite()) {
5579 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5580 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005581}
5582
5583void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04005584 if (instruction->IsEmittedAtUseSite()) {
5585 return;
5586 }
5587
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005588 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005589 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005590 Register obj = locations->InAt(0).AsRegister<Register>();
5591 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005592 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005593 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo4877b792016-09-08 19:49:13 -07005594 // Mask out most significant bit in case the array is String's array of char.
5595 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005596 __ shrl(out, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005597 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005598}
5599
5600void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005601 RegisterSet caller_saves = RegisterSet::Empty();
5602 InvokeRuntimeCallingConvention calling_convention;
5603 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5604 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5605 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005606 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04005607 HInstruction* length = instruction->InputAt(1);
5608 if (!length->IsEmittedAtUseSite()) {
5609 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5610 }
jessicahandojo4877b792016-09-08 19:49:13 -07005611 // Need register to see array's length.
5612 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5613 locations->AddTemp(Location::RequiresRegister());
5614 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005615}
5616
5617void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
jessicahandojo4877b792016-09-08 19:49:13 -07005618 const bool is_string_compressed_char_at =
5619 mirror::kUseStringCompression && instruction->IsStringCharAt();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005620 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005621 Location index_loc = locations->InAt(0);
5622 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07005623 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01005624 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005625
Mark Mendell99dbd682015-04-22 16:18:52 -04005626 if (length_loc.IsConstant()) {
5627 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5628 if (index_loc.IsConstant()) {
5629 // BCE will remove the bounds check if we are guarenteed to pass.
5630 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5631 if (index < 0 || index >= length) {
5632 codegen_->AddSlowPath(slow_path);
5633 __ jmp(slow_path->GetEntryLabel());
5634 } else {
5635 // Some optimization after BCE may have generated this, and we should not
5636 // generate a bounds check if it is a valid range.
5637 }
5638 return;
5639 }
5640
5641 // We have to reverse the jump condition because the length is the constant.
5642 Register index_reg = index_loc.AsRegister<Register>();
5643 __ cmpl(index_reg, Immediate(length));
5644 codegen_->AddSlowPath(slow_path);
5645 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005646 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04005647 HInstruction* array_length = instruction->InputAt(1);
5648 if (array_length->IsEmittedAtUseSite()) {
5649 // Address the length field in the array.
5650 DCHECK(array_length->IsArrayLength());
5651 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
5652 Location array_loc = array_length->GetLocations()->InAt(0);
5653 Address array_len(array_loc.AsRegister<Register>(), len_offset);
jessicahandojo4877b792016-09-08 19:49:13 -07005654 if (is_string_compressed_char_at) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005655 // TODO: if index_loc.IsConstant(), compare twice the index (to compensate for
5656 // the string compression flag) with the in-memory length and avoid the temporary.
jessicahandojo4877b792016-09-08 19:49:13 -07005657 Register length_reg = locations->GetTemp(0).AsRegister<Register>();
5658 __ movl(length_reg, array_len);
5659 codegen_->MaybeRecordImplicitNullCheck(array_length);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005660 __ shrl(length_reg, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005661 codegen_->GenerateIntCompare(length_reg, index_loc);
Mark Mendellee8d9712016-07-12 11:13:15 -04005662 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07005663 // Checking bounds for general case:
5664 // Array of char or string's array with feature compression off.
5665 if (index_loc.IsConstant()) {
5666 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5667 __ cmpl(array_len, Immediate(value));
5668 } else {
5669 __ cmpl(array_len, index_loc.AsRegister<Register>());
5670 }
5671 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendellee8d9712016-07-12 11:13:15 -04005672 }
Mark Mendell99dbd682015-04-22 16:18:52 -04005673 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005674 codegen_->GenerateIntCompare(length_loc, index_loc);
Mark Mendell99dbd682015-04-22 16:18:52 -04005675 }
5676 codegen_->AddSlowPath(slow_path);
5677 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005678 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005679}
5680
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005681void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005682 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005683}
5684
5685void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01005686 if (instruction->GetNext()->IsSuspendCheck() &&
5687 instruction->GetBlock()->GetLoopInformation() != nullptr) {
5688 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
5689 // The back edge will generate the suspend check.
5690 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
5691 }
5692
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005693 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5694}
5695
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005696void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005697 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5698 instruction, LocationSummary::kCallOnSlowPath);
Aart Bikb13c65b2017-03-21 20:14:07 -07005699 // In suspend check slow path, usually there are no caller-save registers at all.
5700 // If SIMD instructions are present, however, we force spilling all live SIMD
5701 // registers in full width (since the runtime only saves/restores lower part).
Aart Bik5576f372017-03-23 16:17:37 -07005702 locations->SetCustomSlowPathCallerSaves(
5703 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005704}
5705
5706void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005707 HBasicBlock* block = instruction->GetBlock();
5708 if (block->GetLoopInformation() != nullptr) {
5709 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5710 // The back edge will generate the suspend check.
5711 return;
5712 }
5713 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5714 // The goto will generate the suspend check.
5715 return;
5716 }
5717 GenerateSuspendCheck(instruction, nullptr);
5718}
5719
5720void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
5721 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005722 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005723 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
5724 if (slow_path == nullptr) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005725 slow_path =
5726 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005727 instruction->SetSlowPath(slow_path);
5728 codegen_->AddSlowPath(slow_path);
5729 if (successor != nullptr) {
5730 DCHECK(successor->IsLoopHeader());
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005731 }
5732 } else {
5733 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5734 }
5735
Andreas Gampe542451c2016-07-26 09:02:02 -07005736 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86PointerSize>().Int32Value()),
Roland Levillain7c1559a2015-12-15 10:55:36 +00005737 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005738 if (successor == nullptr) {
5739 __ j(kNotEqual, slow_path->GetEntryLabel());
5740 __ Bind(slow_path->GetReturnLabel());
5741 } else {
5742 __ j(kEqual, codegen_->GetLabelOf(successor));
5743 __ jmp(slow_path->GetEntryLabel());
5744 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005745}
5746
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005747X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
5748 return codegen_->GetAssembler();
5749}
5750
Mark Mendell7c8d0092015-01-26 11:21:33 -05005751void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005752 ScratchRegisterScope ensure_scratch(
5753 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5754 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5755 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5756 __ movl(temp_reg, Address(ESP, src + stack_offset));
5757 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005758}
5759
5760void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005761 ScratchRegisterScope ensure_scratch(
5762 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5763 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5764 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5765 __ movl(temp_reg, Address(ESP, src + stack_offset));
5766 __ movl(Address(ESP, dst + stack_offset), temp_reg);
5767 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
5768 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005769}
5770
5771void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005772 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005773 Location source = move->GetSource();
5774 Location destination = move->GetDestination();
5775
5776 if (source.IsRegister()) {
5777 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005778 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005779 } else if (destination.IsFpuRegister()) {
5780 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005781 } else {
5782 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005783 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005784 }
David Brazdil74eb1b22015-12-14 11:44:01 +00005785 } else if (source.IsRegisterPair()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005786 size_t elem_size = DataType::Size(DataType::Type::kInt32);
David Brazdil74eb1b22015-12-14 11:44:01 +00005787 // Create stack space for 2 elements.
5788 __ subl(ESP, Immediate(2 * elem_size));
5789 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
5790 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
5791 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
5792 // And remove the temporary stack space we allocated.
5793 __ addl(ESP, Immediate(2 * elem_size));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005794 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005795 if (destination.IsRegister()) {
5796 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
5797 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005798 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005799 } else if (destination.IsRegisterPair()) {
5800 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
5801 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
5802 __ psrlq(src_reg, Immediate(32));
5803 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005804 } else if (destination.IsStackSlot()) {
5805 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07005806 } else if (destination.IsDoubleStackSlot()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005807 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07005808 } else {
5809 DCHECK(destination.IsSIMDStackSlot());
5810 __ movups(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005811 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005812 } else if (source.IsStackSlot()) {
5813 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005814 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005815 } else if (destination.IsFpuRegister()) {
5816 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005817 } else {
5818 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005819 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
5820 }
5821 } else if (source.IsDoubleStackSlot()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005822 if (destination.IsRegisterPair()) {
5823 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
5824 __ movl(destination.AsRegisterPairHigh<Register>(),
5825 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
5826 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005827 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
5828 } else {
5829 DCHECK(destination.IsDoubleStackSlot()) << destination;
5830 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005831 }
Aart Bik5576f372017-03-23 16:17:37 -07005832 } else if (source.IsSIMDStackSlot()) {
5833 DCHECK(destination.IsFpuRegister());
5834 __ movups(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005835 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005836 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005837 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005838 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005839 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005840 if (value == 0) {
5841 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
5842 } else {
5843 __ movl(destination.AsRegister<Register>(), Immediate(value));
5844 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005845 } else {
5846 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05005847 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005848 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005849 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005850 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005851 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005852 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005853 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005854 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5855 if (value == 0) {
5856 // Easy handling of 0.0.
5857 __ xorps(dest, dest);
5858 } else {
5859 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005860 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5861 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
5862 __ movl(temp, Immediate(value));
5863 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005864 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005865 } else {
5866 DCHECK(destination.IsStackSlot()) << destination;
5867 __ movl(Address(ESP, destination.GetStackIndex()), imm);
5868 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005869 } else if (constant->IsLongConstant()) {
5870 int64_t value = constant->AsLongConstant()->GetValue();
5871 int32_t low_value = Low32Bits(value);
5872 int32_t high_value = High32Bits(value);
5873 Immediate low(low_value);
5874 Immediate high(high_value);
5875 if (destination.IsDoubleStackSlot()) {
5876 __ movl(Address(ESP, destination.GetStackIndex()), low);
5877 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5878 } else {
5879 __ movl(destination.AsRegisterPairLow<Register>(), low);
5880 __ movl(destination.AsRegisterPairHigh<Register>(), high);
5881 }
5882 } else {
5883 DCHECK(constant->IsDoubleConstant());
5884 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005885 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005886 int32_t low_value = Low32Bits(value);
5887 int32_t high_value = High32Bits(value);
5888 Immediate low(low_value);
5889 Immediate high(high_value);
5890 if (destination.IsFpuRegister()) {
5891 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5892 if (value == 0) {
5893 // Easy handling of 0.0.
5894 __ xorpd(dest, dest);
5895 } else {
5896 __ pushl(high);
5897 __ pushl(low);
5898 __ movsd(dest, Address(ESP, 0));
5899 __ addl(ESP, Immediate(8));
5900 }
5901 } else {
5902 DCHECK(destination.IsDoubleStackSlot()) << destination;
5903 __ movl(Address(ESP, destination.GetStackIndex()), low);
5904 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5905 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005906 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005907 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005908 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005909 }
5910}
5911
Mark Mendella5c19ce2015-04-01 12:51:05 -04005912void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005913 Register suggested_scratch = reg == EAX ? EBX : EAX;
5914 ScratchRegisterScope ensure_scratch(
5915 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
5916
5917 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5918 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
5919 __ movl(Address(ESP, mem + stack_offset), reg);
5920 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005921}
5922
Mark Mendell7c8d0092015-01-26 11:21:33 -05005923void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005924 ScratchRegisterScope ensure_scratch(
5925 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5926
5927 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5928 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5929 __ movl(temp_reg, Address(ESP, mem + stack_offset));
5930 __ movss(Address(ESP, mem + stack_offset), reg);
5931 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005932}
5933
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005934void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005935 ScratchRegisterScope ensure_scratch1(
5936 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005937
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005938 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
5939 ScratchRegisterScope ensure_scratch2(
5940 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005941
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005942 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
5943 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
5944 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
5945 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
5946 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
5947 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005948}
5949
5950void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005951 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005952 Location source = move->GetSource();
5953 Location destination = move->GetDestination();
5954
5955 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04005956 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
5957 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
5958 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
5959 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
5960 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005961 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005962 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005963 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005964 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005965 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5966 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005967 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
5968 // Use XOR Swap algorithm to avoid a temporary.
5969 DCHECK_NE(source.reg(), destination.reg());
5970 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5971 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5972 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5973 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
5974 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
5975 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
5976 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005977 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
5978 // Take advantage of the 16 bytes in the XMM register.
5979 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
5980 Address stack(ESP, destination.GetStackIndex());
5981 // Load the double into the high doubleword.
5982 __ movhpd(reg, stack);
5983
5984 // Store the low double into the destination.
5985 __ movsd(stack, reg);
5986
5987 // Move the high double to the low double.
5988 __ psrldq(reg, Immediate(8));
5989 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
5990 // Take advantage of the 16 bytes in the XMM register.
5991 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
5992 Address stack(ESP, source.GetStackIndex());
5993 // Load the double into the high doubleword.
5994 __ movhpd(reg, stack);
5995
5996 // Store the low double into the destination.
5997 __ movsd(stack, reg);
5998
5999 // Move the high double to the low double.
6000 __ psrldq(reg, Immediate(8));
6001 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
6002 Exchange(destination.GetStackIndex(), source.GetStackIndex());
6003 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006004 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006005 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006006 }
6007}
6008
6009void ParallelMoveResolverX86::SpillScratch(int reg) {
6010 __ pushl(static_cast<Register>(reg));
6011}
6012
6013void ParallelMoveResolverX86::RestoreScratch(int reg) {
6014 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006015}
6016
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006017HLoadClass::LoadKind CodeGeneratorX86::GetSupportedLoadClassKind(
6018 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006019 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006020 case HLoadClass::LoadKind::kInvalid:
6021 LOG(FATAL) << "UNREACHABLE";
6022 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006023 case HLoadClass::LoadKind::kReferrersClass:
6024 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006025 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006026 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006027 case HLoadClass::LoadKind::kBssEntry:
Vladimir Marko764d4542017-05-16 10:31:41 +01006028 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006029 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006030 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006031 DCHECK(Runtime::Current()->UseJitCompilation());
6032 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01006033 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006034 case HLoadClass::LoadKind::kRuntimeCall:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006035 break;
6036 }
6037 return desired_class_load_kind;
6038}
6039
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006040void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00006041 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006042 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006043 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00006044 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006045 cls,
6046 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00006047 Location::RegisterLocation(EAX));
Vladimir Markoea4c1262017-02-06 19:59:33 +00006048 DCHECK_EQ(calling_convention.GetRegisterAt(0), EAX);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006049 return;
6050 }
Vladimir Marko41559982017-01-06 14:04:23 +00006051 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006052
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006053 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
6054 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006055 ? LocationSummary::kCallOnSlowPath
6056 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01006057 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006058 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006059 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006060 }
6061
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006062 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006063 load_kind == HLoadClass::LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006064 load_kind == HLoadClass::LoadKind::kBootImageClassTable ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006065 load_kind == HLoadClass::LoadKind::kBssEntry) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006066 locations->SetInAt(0, Location::RequiresRegister());
6067 }
6068 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006069 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
6070 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6071 // Rely on the type resolution and/or initialization to save everything.
6072 RegisterSet caller_saves = RegisterSet::Empty();
6073 InvokeRuntimeCallingConvention calling_convention;
6074 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6075 locations->SetCustomSlowPathCallerSaves(caller_saves);
6076 } else {
6077 // For non-Baker read barrier we have a temp-clobbering call.
6078 }
6079 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006080}
6081
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006082Label* CodeGeneratorX86::NewJitRootClassPatch(const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01006083 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006084 Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006085 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006086 // Add a patch entry and return the label.
Vladimir Marko174b2e22017-10-12 13:34:49 +01006087 jit_class_patches_.emplace_back(dex_file, type_index.index_);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006088 PatchInfo<Label>* info = &jit_class_patches_.back();
6089 return &info->label;
6090}
6091
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006092// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6093// move.
6094void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00006095 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006096 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00006097 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01006098 return;
6099 }
Vladimir Marko41559982017-01-06 14:04:23 +00006100 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01006101
Vladimir Marko41559982017-01-06 14:04:23 +00006102 LocationSummary* locations = cls->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006103 Location out_loc = locations->Out();
6104 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006105
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006106 bool generate_null_check = false;
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006107 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
6108 ? kWithoutReadBarrier
6109 : kCompilerReadBarrierOption;
Vladimir Marko41559982017-01-06 14:04:23 +00006110 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006111 case HLoadClass::LoadKind::kReferrersClass: {
6112 DCHECK(!cls->CanCallRuntime());
6113 DCHECK(!cls->MustGenerateClinitCheck());
6114 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6115 Register current_method = locations->InAt(0).AsRegister<Register>();
6116 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006117 cls,
6118 out_loc,
6119 Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()),
Roland Levillain00468f32016-10-27 18:02:48 +01006120 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006121 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006122 break;
6123 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006124 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006125 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006126 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006127 Register method_address = locations->InAt(0).AsRegister<Register>();
6128 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Marko1998cd02017-01-13 13:02:58 +00006129 codegen_->RecordBootTypePatch(cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006130 break;
6131 }
6132 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006133 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006134 uint32_t address = dchecked_integral_cast<uint32_t>(
6135 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
6136 DCHECK_NE(address, 0u);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006137 __ movl(out, Immediate(address));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006138 break;
6139 }
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006140 case HLoadClass::LoadKind::kBootImageClassTable: {
6141 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6142 Register method_address = locations->InAt(0).AsRegister<Register>();
6143 __ movl(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
6144 codegen_->RecordBootTypePatch(cls);
6145 // Extract the reference from the slot data, i.e. clear the hash bits.
6146 int32_t masked_hash = ClassTable::TableSlot::MaskHash(
6147 ComputeModifiedUtf8Hash(cls->GetDexFile().StringByTypeIdx(cls->GetTypeIndex())));
6148 if (masked_hash != 0) {
6149 __ subl(out, Immediate(masked_hash));
6150 }
6151 break;
6152 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006153 case HLoadClass::LoadKind::kBssEntry: {
6154 Register method_address = locations->InAt(0).AsRegister<Register>();
6155 Address address(method_address, CodeGeneratorX86::kDummy32BitOffset);
6156 Label* fixup_label = codegen_->NewTypeBssEntryPatch(cls);
6157 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
6158 generate_null_check = true;
6159 break;
6160 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006161 case HLoadClass::LoadKind::kJitTableAddress: {
6162 Address address = Address::Absolute(CodeGeneratorX86::kDummy32BitOffset);
6163 Label* fixup_label = codegen_->NewJitRootClassPatch(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006164 cls->GetDexFile(), cls->GetTypeIndex(), cls->GetClass());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006165 // /* GcRoot<mirror::Class> */ out = *address
Vladimir Markoea4c1262017-02-06 19:59:33 +00006166 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006167 break;
6168 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006169 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006170 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00006171 LOG(FATAL) << "UNREACHABLE";
6172 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006173 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006174
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006175 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6176 DCHECK(cls->CanCallRuntime());
Vladimir Marko174b2e22017-10-12 13:34:49 +01006177 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathX86(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006178 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
6179 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006180
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006181 if (generate_null_check) {
6182 __ testl(out, out);
6183 __ j(kEqual, slow_path->GetEntryLabel());
6184 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006185
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006186 if (cls->MustGenerateClinitCheck()) {
6187 GenerateClassInitializationCheck(slow_path, out);
6188 } else {
6189 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006190 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006191 }
6192}
6193
6194void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
6195 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006196 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006197 locations->SetInAt(0, Location::RequiresRegister());
6198 if (check->HasUses()) {
6199 locations->SetOut(Location::SameAsFirstInput());
6200 }
6201}
6202
6203void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006204 // We assume the class to not be null.
Vladimir Marko174b2e22017-10-12 13:34:49 +01006205 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006206 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006207 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00006208 GenerateClassInitializationCheck(slow_path,
6209 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006210}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006211
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006212void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07006213 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006214 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
6215 Immediate(mirror::Class::kStatusInitialized));
6216 __ j(kLess, slow_path->GetEntryLabel());
6217 __ Bind(slow_path->GetExitLabel());
6218 // No need for memory fence, thanks to the X86 memory model.
6219}
6220
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006221HLoadString::LoadKind CodeGeneratorX86::GetSupportedLoadStringKind(
6222 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006223 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006224 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006225 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00006226 case HLoadString::LoadKind::kBssEntry:
Vladimir Marko764d4542017-05-16 10:31:41 +01006227 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006228 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006229 case HLoadString::LoadKind::kJitTableAddress:
6230 DCHECK(Runtime::Current()->UseJitCompilation());
6231 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01006232 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006233 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006234 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006235 }
6236 return desired_string_load_kind;
6237}
6238
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006239void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006240 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01006241 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006242 HLoadString::LoadKind load_kind = load->GetLoadKind();
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006243 if (load_kind == HLoadString::LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006244 load_kind == HLoadString::LoadKind::kBootImageInternTable ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00006245 load_kind == HLoadString::LoadKind::kBssEntry) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006246 locations->SetInAt(0, Location::RequiresRegister());
6247 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006248 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006249 locations->SetOut(Location::RegisterLocation(EAX));
6250 } else {
6251 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006252 if (load_kind == HLoadString::LoadKind::kBssEntry) {
6253 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00006254 // Rely on the pResolveString to save everything.
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006255 RegisterSet caller_saves = RegisterSet::Empty();
6256 InvokeRuntimeCallingConvention calling_convention;
6257 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6258 locations->SetCustomSlowPathCallerSaves(caller_saves);
6259 } else {
6260 // For non-Baker read barrier we have a temp-clobbering call.
6261 }
6262 }
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006263 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006264}
6265
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006266Label* CodeGeneratorX86::NewJitRootStringPatch(const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01006267 dex::StringIndex string_index,
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006268 Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006269 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006270 // Add a patch entry and return the label.
Vladimir Marko174b2e22017-10-12 13:34:49 +01006271 jit_string_patches_.emplace_back(dex_file, string_index.index_);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006272 PatchInfo<Label>* info = &jit_string_patches_.back();
6273 return &info->label;
6274}
6275
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006276// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6277// move.
6278void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006279 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006280 Location out_loc = locations->Out();
6281 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006282
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006283 switch (load->GetLoadKind()) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006284 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006285 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006286 Register method_address = locations->InAt(0).AsRegister<Register>();
6287 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Markoaad75c62016-10-03 08:46:48 +00006288 codegen_->RecordBootStringPatch(load);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006289 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006290 }
6291 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006292 uint32_t address = dchecked_integral_cast<uint32_t>(
6293 reinterpret_cast<uintptr_t>(load->GetString().Get()));
6294 DCHECK_NE(address, 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006295 __ movl(out, Immediate(address));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006296 return;
6297 }
6298 case HLoadString::LoadKind::kBootImageInternTable: {
6299 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6300 Register method_address = locations->InAt(0).AsRegister<Register>();
6301 __ movl(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
6302 codegen_->RecordBootStringPatch(load);
6303 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006304 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00006305 case HLoadString::LoadKind::kBssEntry: {
6306 Register method_address = locations->InAt(0).AsRegister<Register>();
6307 Address address = Address(method_address, CodeGeneratorX86::kDummy32BitOffset);
6308 Label* fixup_label = codegen_->NewStringBssEntryPatch(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006309 // /* GcRoot<mirror::String> */ out = *address /* PC-relative */
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006310 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
Vladimir Marko174b2e22017-10-12 13:34:49 +01006311 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadStringSlowPathX86(load);
Vladimir Markoaad75c62016-10-03 08:46:48 +00006312 codegen_->AddSlowPath(slow_path);
6313 __ testl(out, out);
6314 __ j(kEqual, slow_path->GetEntryLabel());
6315 __ Bind(slow_path->GetExitLabel());
6316 return;
6317 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006318 case HLoadString::LoadKind::kJitTableAddress: {
6319 Address address = Address::Absolute(CodeGeneratorX86::kDummy32BitOffset);
6320 Label* fixup_label = codegen_->NewJitRootStringPatch(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006321 load->GetDexFile(), load->GetStringIndex(), load->GetString());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006322 // /* GcRoot<mirror::String> */ out = *address
6323 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
6324 return;
6325 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006326 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006327 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006328 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006329
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006330 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006331 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006332 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006333 __ movl(calling_convention.GetRegisterAt(0), Immediate(load->GetStringIndex().index_));
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006334 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6335 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006336}
6337
David Brazdilcb1c0552015-08-04 16:22:25 +01006338static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006339 return Address::Absolute(Thread::ExceptionOffset<kX86PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01006340}
6341
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006342void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
6343 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006344 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006345 locations->SetOut(Location::RequiresRegister());
6346}
6347
6348void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01006349 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
6350}
6351
6352void LocationsBuilderX86::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006353 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
David Brazdilcb1c0552015-08-04 16:22:25 +01006354}
6355
6356void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6357 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006358}
6359
6360void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006361 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6362 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006363 InvokeRuntimeCallingConvention calling_convention;
6364 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6365}
6366
6367void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006368 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006369 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006370}
6371
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006372// Temp is used for read barrier.
6373static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
6374 if (kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00006375 !kUseBakerReadBarrier &&
6376 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain7c1559a2015-12-15 10:55:36 +00006377 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006378 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
6379 return 1;
6380 }
6381 return 0;
6382}
6383
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006384// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006385// interface pointer, one for loading the current interface.
6386// The other checks have one temp for loading the object's class.
6387static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
6388 if (type_check_kind == TypeCheckKind::kInterfaceCheck && !kPoisonHeapReferences) {
6389 return 2;
6390 }
6391 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006392}
6393
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006394void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006395 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006396 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01006397 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006398 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006399 case TypeCheckKind::kExactCheck:
6400 case TypeCheckKind::kAbstractClassCheck:
6401 case TypeCheckKind::kClassHierarchyCheck:
6402 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006403 call_kind =
6404 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01006405 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006406 break;
6407 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006408 case TypeCheckKind::kUnresolvedCheck:
6409 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006410 call_kind = LocationSummary::kCallOnSlowPath;
6411 break;
6412 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006413
Vladimir Markoca6fff82017-10-03 14:49:14 +01006414 LocationSummary* locations =
6415 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01006416 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006417 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006418 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006419 locations->SetInAt(0, Location::RequiresRegister());
6420 locations->SetInAt(1, Location::Any());
6421 // Note that TypeCheckSlowPathX86 uses this "out" register too.
6422 locations->SetOut(Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006423 // When read barriers are enabled, we need a temporary register for some cases.
6424 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006425}
6426
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006427void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006428 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006429 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006430 Location obj_loc = locations->InAt(0);
6431 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006432 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006433 Location out_loc = locations->Out();
6434 Register out = out_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006435 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
6436 DCHECK_LE(num_temps, 1u);
6437 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006438 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006439 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6440 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6441 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07006442 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006443 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006444
6445 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006446 // Avoid null check if we know obj is not null.
6447 if (instruction->MustDoNullCheck()) {
6448 __ testl(obj, obj);
6449 __ j(kEqual, &zero);
6450 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006451
Roland Levillain7c1559a2015-12-15 10:55:36 +00006452 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006453 case TypeCheckKind::kExactCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006454 // /* HeapReference<Class> */ out = obj->klass_
6455 GenerateReferenceLoadTwoRegisters(instruction,
6456 out_loc,
6457 obj_loc,
6458 class_offset,
6459 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006460 if (cls.IsRegister()) {
6461 __ cmpl(out, cls.AsRegister<Register>());
6462 } else {
6463 DCHECK(cls.IsStackSlot()) << cls;
6464 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6465 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006466
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006467 // Classes must be equal for the instanceof to succeed.
6468 __ j(kNotEqual, &zero);
6469 __ movl(out, Immediate(1));
6470 __ jmp(&done);
6471 break;
6472 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006473
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006474 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006475 // /* HeapReference<Class> */ out = obj->klass_
6476 GenerateReferenceLoadTwoRegisters(instruction,
6477 out_loc,
6478 obj_loc,
6479 class_offset,
6480 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006481 // If the class is abstract, we eagerly fetch the super class of the
6482 // object to avoid doing a comparison we know will fail.
6483 NearLabel loop;
6484 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006485 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006486 GenerateReferenceLoadOneRegister(instruction,
6487 out_loc,
6488 super_offset,
6489 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006490 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006491 __ testl(out, out);
6492 // If `out` is null, we use it for the result, and jump to `done`.
6493 __ j(kEqual, &done);
6494 if (cls.IsRegister()) {
6495 __ cmpl(out, cls.AsRegister<Register>());
6496 } else {
6497 DCHECK(cls.IsStackSlot()) << cls;
6498 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6499 }
6500 __ j(kNotEqual, &loop);
6501 __ movl(out, Immediate(1));
6502 if (zero.IsLinked()) {
6503 __ jmp(&done);
6504 }
6505 break;
6506 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006507
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006508 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006509 // /* HeapReference<Class> */ out = obj->klass_
6510 GenerateReferenceLoadTwoRegisters(instruction,
6511 out_loc,
6512 obj_loc,
6513 class_offset,
6514 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006515 // Walk over the class hierarchy to find a match.
6516 NearLabel loop, success;
6517 __ Bind(&loop);
6518 if (cls.IsRegister()) {
6519 __ cmpl(out, cls.AsRegister<Register>());
6520 } else {
6521 DCHECK(cls.IsStackSlot()) << cls;
6522 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6523 }
6524 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006525 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006526 GenerateReferenceLoadOneRegister(instruction,
6527 out_loc,
6528 super_offset,
6529 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006530 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006531 __ testl(out, out);
6532 __ j(kNotEqual, &loop);
6533 // If `out` is null, we use it for the result, and jump to `done`.
6534 __ jmp(&done);
6535 __ Bind(&success);
6536 __ movl(out, Immediate(1));
6537 if (zero.IsLinked()) {
6538 __ jmp(&done);
6539 }
6540 break;
6541 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006542
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006543 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006544 // /* HeapReference<Class> */ out = obj->klass_
6545 GenerateReferenceLoadTwoRegisters(instruction,
6546 out_loc,
6547 obj_loc,
6548 class_offset,
6549 kCompilerReadBarrierOption);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006550 // Do an exact check.
6551 NearLabel exact_check;
6552 if (cls.IsRegister()) {
6553 __ cmpl(out, cls.AsRegister<Register>());
6554 } else {
6555 DCHECK(cls.IsStackSlot()) << cls;
6556 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6557 }
6558 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006559 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006560 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006561 GenerateReferenceLoadOneRegister(instruction,
6562 out_loc,
6563 component_offset,
6564 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006565 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006566 __ testl(out, out);
6567 // If `out` is null, we use it for the result, and jump to `done`.
6568 __ j(kEqual, &done);
6569 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
6570 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006571 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006572 __ movl(out, Immediate(1));
6573 __ jmp(&done);
6574 break;
6575 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006576
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006577 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006578 // No read barrier since the slow path will retry upon failure.
6579 // /* HeapReference<Class> */ out = obj->klass_
6580 GenerateReferenceLoadTwoRegisters(instruction,
6581 out_loc,
6582 obj_loc,
6583 class_offset,
6584 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006585 if (cls.IsRegister()) {
6586 __ cmpl(out, cls.AsRegister<Register>());
6587 } else {
6588 DCHECK(cls.IsStackSlot()) << cls;
6589 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6590 }
6591 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01006592 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
6593 instruction, /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006594 codegen_->AddSlowPath(slow_path);
6595 __ j(kNotEqual, slow_path->GetEntryLabel());
6596 __ movl(out, Immediate(1));
6597 if (zero.IsLinked()) {
6598 __ jmp(&done);
6599 }
6600 break;
6601 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006602
Calin Juravle98893e12015-10-02 21:05:03 +01006603 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006604 case TypeCheckKind::kInterfaceCheck: {
6605 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006606 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00006607 // cases.
6608 //
6609 // We cannot directly call the InstanceofNonTrivial runtime
6610 // entry point without resorting to a type checking slow path
6611 // here (i.e. by calling InvokeRuntime directly), as it would
6612 // require to assign fixed registers for the inputs of this
6613 // HInstanceOf instruction (following the runtime calling
6614 // convention), which might be cluttered by the potential first
6615 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006616 //
6617 // TODO: Introduce a new runtime entry point taking the object
6618 // to test (instead of its class) as argument, and let it deal
6619 // with the read barrier issues. This will let us refactor this
6620 // case of the `switch` code as it was previously (with a direct
6621 // call to the runtime not using a type checking slow path).
6622 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006623 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01006624 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
6625 instruction, /* is_fatal */ false);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006626 codegen_->AddSlowPath(slow_path);
6627 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006628 if (zero.IsLinked()) {
6629 __ jmp(&done);
6630 }
6631 break;
6632 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006633 }
6634
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006635 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006636 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006637 __ xorl(out, out);
6638 }
6639
6640 if (done.IsLinked()) {
6641 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006642 }
6643
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006644 if (slow_path != nullptr) {
6645 __ Bind(slow_path->GetExitLabel());
6646 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006647}
6648
Mathieu Chartier5ac321b2016-11-09 16:33:54 -08006649static bool IsTypeCheckSlowPathFatal(TypeCheckKind type_check_kind, bool throws_into_catch) {
6650 switch (type_check_kind) {
6651 case TypeCheckKind::kExactCheck:
6652 case TypeCheckKind::kAbstractClassCheck:
6653 case TypeCheckKind::kClassHierarchyCheck:
6654 case TypeCheckKind::kArrayObjectCheck:
6655 return !throws_into_catch && !kEmitCompilerReadBarrier;
6656 case TypeCheckKind::kInterfaceCheck:
6657 return !throws_into_catch && !kEmitCompilerReadBarrier && !kPoisonHeapReferences;
6658 case TypeCheckKind::kArrayCheck:
6659 case TypeCheckKind::kUnresolvedCheck:
6660 return false;
6661 }
6662 LOG(FATAL) << "Unreachable";
6663 UNREACHABLE();
6664}
6665
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006666void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006667 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006668 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Mathieu Chartier5ac321b2016-11-09 16:33:54 -08006669 LocationSummary::CallKind call_kind =
6670 IsTypeCheckSlowPathFatal(type_check_kind, throws_into_catch)
6671 ? LocationSummary::kNoCall
6672 : LocationSummary::kCallOnSlowPath;
Vladimir Markoca6fff82017-10-03 14:49:14 +01006673 LocationSummary* locations =
6674 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006675 locations->SetInAt(0, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006676 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
6677 // Require a register for the interface check since there is a loop that compares the class to
6678 // a memory address.
6679 locations->SetInAt(1, Location::RequiresRegister());
6680 } else {
6681 locations->SetInAt(1, Location::Any());
6682 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006683 // Note that TypeCheckSlowPathX86 uses this "temp" register too.
6684 locations->AddTemp(Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006685 // When read barriers are enabled, we need an additional temporary register for some cases.
6686 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
6687}
6688
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006689void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006690 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006691 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006692 Location obj_loc = locations->InAt(0);
6693 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006694 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006695 Location temp_loc = locations->GetTemp(0);
6696 Register temp = temp_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006697 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
6698 DCHECK_GE(num_temps, 1u);
6699 DCHECK_LE(num_temps, 2u);
6700 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
6701 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6702 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6703 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6704 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
6705 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
6706 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
6707 const uint32_t object_array_data_offset =
6708 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006709
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006710 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
6711 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
6712 // read barriers is done for performance and code size reasons.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006713 bool is_type_check_slow_path_fatal =
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006714 IsTypeCheckSlowPathFatal(type_check_kind, instruction->CanThrowIntoCatchBlock());
6715
Roland Levillain0d5a2812015-11-13 10:07:31 +00006716 SlowPathCode* type_check_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006717 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
6718 instruction, is_type_check_slow_path_fatal);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006719 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006720
Roland Levillain0d5a2812015-11-13 10:07:31 +00006721 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006722 // Avoid null check if we know obj is not null.
6723 if (instruction->MustDoNullCheck()) {
6724 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006725 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006726 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006727
Roland Levillain0d5a2812015-11-13 10:07:31 +00006728 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006729 case TypeCheckKind::kExactCheck:
6730 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006731 // /* HeapReference<Class> */ temp = obj->klass_
6732 GenerateReferenceLoadTwoRegisters(instruction,
6733 temp_loc,
6734 obj_loc,
6735 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006736 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006737
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006738 if (cls.IsRegister()) {
6739 __ cmpl(temp, cls.AsRegister<Register>());
6740 } else {
6741 DCHECK(cls.IsStackSlot()) << cls;
6742 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6743 }
6744 // Jump to slow path for throwing the exception or doing a
6745 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006746 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006747 break;
6748 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006749
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006750 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006751 // /* HeapReference<Class> */ temp = obj->klass_
6752 GenerateReferenceLoadTwoRegisters(instruction,
6753 temp_loc,
6754 obj_loc,
6755 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006756 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006757
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006758 // If the class is abstract, we eagerly fetch the super class of the
6759 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006760 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006761 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006762 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006763 GenerateReferenceLoadOneRegister(instruction,
6764 temp_loc,
6765 super_offset,
6766 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006767 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006768
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006769 // If the class reference currently in `temp` is null, jump to the slow path to throw the
6770 // exception.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006771 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006772 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006773
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006774 // Otherwise, compare the classes
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006775 if (cls.IsRegister()) {
6776 __ cmpl(temp, cls.AsRegister<Register>());
6777 } else {
6778 DCHECK(cls.IsStackSlot()) << cls;
6779 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6780 }
6781 __ j(kNotEqual, &loop);
6782 break;
6783 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006784
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006785 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006786 // /* HeapReference<Class> */ temp = obj->klass_
6787 GenerateReferenceLoadTwoRegisters(instruction,
6788 temp_loc,
6789 obj_loc,
6790 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006791 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006792
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006793 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006794 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006795 __ Bind(&loop);
6796 if (cls.IsRegister()) {
6797 __ cmpl(temp, cls.AsRegister<Register>());
6798 } else {
6799 DCHECK(cls.IsStackSlot()) << cls;
6800 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6801 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006802 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006803
Roland Levillain0d5a2812015-11-13 10:07:31 +00006804 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006805 GenerateReferenceLoadOneRegister(instruction,
6806 temp_loc,
6807 super_offset,
6808 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006809 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006810
6811 // If the class reference currently in `temp` is not null, jump
6812 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006813 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006814 __ j(kNotZero, &loop);
6815 // Otherwise, jump to the slow path to throw the exception.;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006816 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006817 break;
6818 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006819
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006820 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006821 // /* HeapReference<Class> */ temp = obj->klass_
6822 GenerateReferenceLoadTwoRegisters(instruction,
6823 temp_loc,
6824 obj_loc,
6825 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006826 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006827
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006828 // Do an exact check.
6829 if (cls.IsRegister()) {
6830 __ cmpl(temp, cls.AsRegister<Register>());
6831 } else {
6832 DCHECK(cls.IsStackSlot()) << cls;
6833 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6834 }
6835 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006836
6837 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006838 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006839 GenerateReferenceLoadOneRegister(instruction,
6840 temp_loc,
6841 component_offset,
6842 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006843 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006844
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006845 // If the component type is null (i.e. the object not an array), jump to the slow path to
6846 // throw the exception. Otherwise proceed with the check.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006847 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006848 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006849
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006850 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006851 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006852 break;
6853 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006854
Calin Juravle98893e12015-10-02 21:05:03 +01006855 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006856 // We always go into the type check slow path for the unresolved check case.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006857 // We cannot directly call the CheckCast runtime entry point
6858 // without resorting to a type checking slow path here (i.e. by
6859 // calling InvokeRuntime directly), as it would require to
6860 // assign fixed registers for the inputs of this HInstanceOf
6861 // instruction (following the runtime calling convention), which
6862 // might be cluttered by the potential first read barrier
6863 // emission at the beginning of this method.
6864 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006865 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006866
6867 case TypeCheckKind::kInterfaceCheck: {
6868 // Fast path for the interface check. Since we compare with a memory location in the inner
6869 // loop we would need to have cls poisoned. However unpoisoning cls would reset the
6870 // conditional flags and cause the conditional jump to be incorrect. Therefore we just jump
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006871 // to the slow path if we are running under poisoning.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006872 if (!kPoisonHeapReferences) {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006873 // Try to avoid read barriers to improve the fast path. We can not get false positives by
6874 // doing this.
6875 // /* HeapReference<Class> */ temp = obj->klass_
6876 GenerateReferenceLoadTwoRegisters(instruction,
6877 temp_loc,
6878 obj_loc,
6879 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006880 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006881
6882 // /* HeapReference<Class> */ temp = temp->iftable_
6883 GenerateReferenceLoadTwoRegisters(instruction,
6884 temp_loc,
6885 temp_loc,
6886 iftable_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006887 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08006888 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006889 __ movl(maybe_temp2_loc.AsRegister<Register>(), Address(temp, array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08006890 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006891 NearLabel start_loop;
6892 __ Bind(&start_loop);
Mathieu Chartier6beced42016-11-15 15:51:31 -08006893 // Need to subtract first to handle the empty array case.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006894 __ subl(maybe_temp2_loc.AsRegister<Register>(), Immediate(2));
Mathieu Chartier6beced42016-11-15 15:51:31 -08006895 __ j(kNegative, type_check_slow_path->GetEntryLabel());
6896 // Go to next interface if the classes do not match.
6897 __ cmpl(cls.AsRegister<Register>(),
6898 CodeGeneratorX86::ArrayAddress(temp,
6899 maybe_temp2_loc,
6900 TIMES_4,
6901 object_array_data_offset));
6902 __ j(kNotEqual, &start_loop);
6903 } else {
6904 __ jmp(type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006905 }
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006906 break;
6907 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006908 }
6909 __ Bind(&done);
6910
Roland Levillain0d5a2812015-11-13 10:07:31 +00006911 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006912}
6913
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006914void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006915 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6916 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006917 InvokeRuntimeCallingConvention calling_convention;
6918 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6919}
6920
6921void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006922 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject
6923 : kQuickUnlockObject,
Alexandre Rames8158f282015-08-07 10:26:17 +01006924 instruction,
Serban Constantinescuba45db02016-07-12 22:53:02 +01006925 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006926 if (instruction->IsEnter()) {
6927 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6928 } else {
6929 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6930 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006931}
6932
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006933void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6934void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6935void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6936
6937void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6938 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006939 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006940 DCHECK(instruction->GetResultType() == DataType::Type::kInt32
6941 || instruction->GetResultType() == DataType::Type::kInt64);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006942 locations->SetInAt(0, Location::RequiresRegister());
6943 locations->SetInAt(1, Location::Any());
6944 locations->SetOut(Location::SameAsFirstInput());
6945}
6946
6947void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
6948 HandleBitwiseOperation(instruction);
6949}
6950
6951void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
6952 HandleBitwiseOperation(instruction);
6953}
6954
6955void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
6956 HandleBitwiseOperation(instruction);
6957}
6958
6959void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6960 LocationSummary* locations = instruction->GetLocations();
6961 Location first = locations->InAt(0);
6962 Location second = locations->InAt(1);
6963 DCHECK(first.Equals(locations->Out()));
6964
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006965 if (instruction->GetResultType() == DataType::Type::kInt32) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006966 if (second.IsRegister()) {
6967 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006968 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006969 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006970 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006971 } else {
6972 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006973 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006974 }
6975 } else if (second.IsConstant()) {
6976 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006977 __ andl(first.AsRegister<Register>(),
6978 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006979 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006980 __ orl(first.AsRegister<Register>(),
6981 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006982 } else {
6983 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00006984 __ xorl(first.AsRegister<Register>(),
6985 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006986 }
6987 } else {
6988 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006989 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006990 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006991 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006992 } else {
6993 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006994 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006995 }
6996 }
6997 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006998 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006999 if (second.IsRegisterPair()) {
7000 if (instruction->IsAnd()) {
7001 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7002 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7003 } else if (instruction->IsOr()) {
7004 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7005 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7006 } else {
7007 DCHECK(instruction->IsXor());
7008 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7009 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7010 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007011 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007012 if (instruction->IsAnd()) {
7013 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7014 __ andl(first.AsRegisterPairHigh<Register>(),
7015 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7016 } else if (instruction->IsOr()) {
7017 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7018 __ orl(first.AsRegisterPairHigh<Register>(),
7019 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7020 } else {
7021 DCHECK(instruction->IsXor());
7022 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7023 __ xorl(first.AsRegisterPairHigh<Register>(),
7024 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7025 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007026 } else {
7027 DCHECK(second.IsConstant()) << second;
7028 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007029 int32_t low_value = Low32Bits(value);
7030 int32_t high_value = High32Bits(value);
7031 Immediate low(low_value);
7032 Immediate high(high_value);
7033 Register first_low = first.AsRegisterPairLow<Register>();
7034 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007035 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007036 if (low_value == 0) {
7037 __ xorl(first_low, first_low);
7038 } else if (low_value != -1) {
7039 __ andl(first_low, low);
7040 }
7041 if (high_value == 0) {
7042 __ xorl(first_high, first_high);
7043 } else if (high_value != -1) {
7044 __ andl(first_high, high);
7045 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007046 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007047 if (low_value != 0) {
7048 __ orl(first_low, low);
7049 }
7050 if (high_value != 0) {
7051 __ orl(first_high, high);
7052 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007053 } else {
7054 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007055 if (low_value != 0) {
7056 __ xorl(first_low, low);
7057 }
7058 if (high_value != 0) {
7059 __ xorl(first_high, high);
7060 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007061 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007062 }
7063 }
7064}
7065
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007066void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(
7067 HInstruction* instruction,
7068 Location out,
7069 uint32_t offset,
7070 Location maybe_temp,
7071 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007072 Register out_reg = out.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007073 if (read_barrier_option == kWithReadBarrier) {
7074 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007075 if (kUseBakerReadBarrier) {
7076 // Load with fast path based Baker's read barrier.
7077 // /* HeapReference<Object> */ out = *(out + offset)
7078 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00007079 instruction, out, out_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007080 } else {
7081 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007082 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain7c1559a2015-12-15 10:55:36 +00007083 // in the following move operation, as we will need it for the
7084 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00007085 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007086 __ movl(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007087 // /* HeapReference<Object> */ out = *(out + offset)
7088 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007089 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007090 }
7091 } else {
7092 // Plain load with no read barrier.
7093 // /* HeapReference<Object> */ out = *(out + offset)
7094 __ movl(out_reg, Address(out_reg, offset));
7095 __ MaybeUnpoisonHeapReference(out_reg);
7096 }
7097}
7098
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007099void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(
7100 HInstruction* instruction,
7101 Location out,
7102 Location obj,
7103 uint32_t offset,
7104 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007105 Register out_reg = out.AsRegister<Register>();
7106 Register obj_reg = obj.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007107 if (read_barrier_option == kWithReadBarrier) {
7108 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007109 if (kUseBakerReadBarrier) {
7110 // Load with fast path based Baker's read barrier.
7111 // /* HeapReference<Object> */ out = *(obj + offset)
7112 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00007113 instruction, out, obj_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007114 } else {
7115 // Load with slow path based read barrier.
7116 // /* HeapReference<Object> */ out = *(obj + offset)
7117 __ movl(out_reg, Address(obj_reg, offset));
7118 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
7119 }
7120 } else {
7121 // Plain load with no read barrier.
7122 // /* HeapReference<Object> */ out = *(obj + offset)
7123 __ movl(out_reg, Address(obj_reg, offset));
7124 __ MaybeUnpoisonHeapReference(out_reg);
7125 }
7126}
7127
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007128void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(
7129 HInstruction* instruction,
7130 Location root,
7131 const Address& address,
7132 Label* fixup_label,
7133 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007134 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007135 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07007136 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007137 if (kUseBakerReadBarrier) {
7138 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
7139 // Baker's read barrier are used:
7140 //
Roland Levillaind966ce72017-02-09 16:20:14 +00007141 // root = obj.field;
7142 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
7143 // if (temp != null) {
7144 // root = temp(root)
Roland Levillain7c1559a2015-12-15 10:55:36 +00007145 // }
7146
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007147 // /* GcRoot<mirror::Object> */ root = *address
7148 __ movl(root_reg, address);
7149 if (fixup_label != nullptr) {
7150 __ Bind(fixup_label);
7151 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007152 static_assert(
7153 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
7154 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
7155 "have different sizes.");
7156 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
7157 "art::mirror::CompressedReference<mirror::Object> and int32_t "
7158 "have different sizes.");
7159
Vladimir Marko953437b2016-08-24 08:30:46 +00007160 // Slow path marking the GC root `root`.
Vladimir Marko174b2e22017-10-12 13:34:49 +01007161 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathX86(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007162 instruction, root, /* unpoison_ref_before_marking */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007163 codegen_->AddSlowPath(slow_path);
7164
Roland Levillaind966ce72017-02-09 16:20:14 +00007165 // Test the entrypoint (`Thread::Current()->pReadBarrierMarkReg ## root.reg()`).
7166 const int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +01007167 Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(root.reg());
Roland Levillaind966ce72017-02-09 16:20:14 +00007168 __ fs()->cmpl(Address::Absolute(entry_point_offset), Immediate(0));
7169 // The entrypoint is null when the GC is not marking.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007170 __ j(kNotEqual, slow_path->GetEntryLabel());
7171 __ Bind(slow_path->GetExitLabel());
7172 } else {
7173 // GC root loaded through a slow path for read barriers other
7174 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007175 // /* GcRoot<mirror::Object>* */ root = address
7176 __ leal(root_reg, address);
7177 if (fixup_label != nullptr) {
7178 __ Bind(fixup_label);
7179 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007180 // /* mirror::Object* */ root = root->Read()
7181 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
7182 }
7183 } else {
7184 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007185 // /* GcRoot<mirror::Object> */ root = *address
7186 __ movl(root_reg, address);
7187 if (fixup_label != nullptr) {
7188 __ Bind(fixup_label);
7189 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007190 // Note that GC roots are not affected by heap poisoning, thus we
7191 // do not have to unpoison `root_reg` here.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007192 }
7193}
7194
7195void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
7196 Location ref,
7197 Register obj,
7198 uint32_t offset,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007199 bool needs_null_check) {
7200 DCHECK(kEmitCompilerReadBarrier);
7201 DCHECK(kUseBakerReadBarrier);
7202
7203 // /* HeapReference<Object> */ ref = *(obj + offset)
7204 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007205 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007206}
7207
7208void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
7209 Location ref,
7210 Register obj,
7211 uint32_t data_offset,
7212 Location index,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007213 bool needs_null_check) {
7214 DCHECK(kEmitCompilerReadBarrier);
7215 DCHECK(kUseBakerReadBarrier);
7216
Roland Levillain3d312422016-06-23 13:53:42 +01007217 static_assert(
7218 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
7219 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00007220 // /* HeapReference<Object> */ ref =
7221 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007222 Address src = CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007223 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007224}
7225
7226void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
7227 Location ref,
7228 Register obj,
7229 const Address& src,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007230 bool needs_null_check,
7231 bool always_update_field,
7232 Register* temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007233 DCHECK(kEmitCompilerReadBarrier);
7234 DCHECK(kUseBakerReadBarrier);
7235
7236 // In slow path based read barriers, the read barrier call is
7237 // inserted after the original load. However, in fast path based
7238 // Baker's read barriers, we need to perform the load of
7239 // mirror::Object::monitor_ *before* the original reference load.
7240 // This load-load ordering is required by the read barrier.
7241 // The fast path/slow path (for Baker's algorithm) should look like:
7242 //
7243 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7244 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7245 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007246 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain7c1559a2015-12-15 10:55:36 +00007247 // if (is_gray) {
7248 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7249 // }
7250 //
7251 // Note: the original implementation in ReadBarrier::Barrier is
7252 // slightly more complex as:
7253 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007254 // the high-bits of rb_state, which are expected to be all zeroes
7255 // (we use CodeGeneratorX86::GenerateMemoryBarrier instead here,
7256 // which is a no-op thanks to the x86 memory model);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007257 // - it performs additional checks that we do not do here for
7258 // performance reasons.
7259
7260 Register ref_reg = ref.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +00007261 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7262
Vladimir Marko953437b2016-08-24 08:30:46 +00007263 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007264 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
7265 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko953437b2016-08-24 08:30:46 +00007266 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
7267 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
7268 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
7269
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007270 // if (rb_state == ReadBarrier::GrayState())
Vladimir Marko953437b2016-08-24 08:30:46 +00007271 // ref = ReadBarrier::Mark(ref);
7272 // At this point, just do the "if" and make sure that flags are preserved until the branch.
7273 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain7c1559a2015-12-15 10:55:36 +00007274 if (needs_null_check) {
7275 MaybeRecordImplicitNullCheck(instruction);
7276 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007277
7278 // Load fence to prevent load-load reordering.
7279 // Note that this is a no-op, thanks to the x86 memory model.
7280 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
7281
7282 // The actual reference load.
7283 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00007284 __ movl(ref_reg, src); // Flags are unaffected.
7285
7286 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
7287 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007288 SlowPathCode* slow_path;
7289 if (always_update_field) {
7290 DCHECK(temp != nullptr);
Vladimir Marko174b2e22017-10-12 13:34:49 +01007291 slow_path = new (GetScopedAllocator()) ReadBarrierMarkAndUpdateFieldSlowPathX86(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007292 instruction, ref, obj, src, /* unpoison_ref_before_marking */ true, *temp);
7293 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01007294 slow_path = new (GetScopedAllocator()) ReadBarrierMarkSlowPathX86(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007295 instruction, ref, /* unpoison_ref_before_marking */ true);
7296 }
Vladimir Marko953437b2016-08-24 08:30:46 +00007297 AddSlowPath(slow_path);
7298
7299 // We have done the "if" of the gray bit check above, now branch based on the flags.
7300 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain7c1559a2015-12-15 10:55:36 +00007301
7302 // Object* ref = ref_addr->AsMirrorPtr()
7303 __ MaybeUnpoisonHeapReference(ref_reg);
7304
Roland Levillain7c1559a2015-12-15 10:55:36 +00007305 __ Bind(slow_path->GetExitLabel());
7306}
7307
7308void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
7309 Location out,
7310 Location ref,
7311 Location obj,
7312 uint32_t offset,
7313 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007314 DCHECK(kEmitCompilerReadBarrier);
7315
Roland Levillain7c1559a2015-12-15 10:55:36 +00007316 // Insert a slow path based read barrier *after* the reference load.
7317 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007318 // If heap poisoning is enabled, the unpoisoning of the loaded
7319 // reference will be carried out by the runtime within the slow
7320 // path.
7321 //
7322 // Note that `ref` currently does not get unpoisoned (when heap
7323 // poisoning is enabled), which is alright as the `ref` argument is
7324 // not used by the artReadBarrierSlow entry point.
7325 //
7326 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01007327 SlowPathCode* slow_path = new (GetScopedAllocator())
Roland Levillain0d5a2812015-11-13 10:07:31 +00007328 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
7329 AddSlowPath(slow_path);
7330
Roland Levillain0d5a2812015-11-13 10:07:31 +00007331 __ jmp(slow_path->GetEntryLabel());
7332 __ Bind(slow_path->GetExitLabel());
7333}
7334
Roland Levillain7c1559a2015-12-15 10:55:36 +00007335void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7336 Location out,
7337 Location ref,
7338 Location obj,
7339 uint32_t offset,
7340 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007341 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007342 // Baker's read barriers shall be handled by the fast path
7343 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
7344 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007345 // If heap poisoning is enabled, unpoisoning will be taken care of
7346 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007347 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007348 } else if (kPoisonHeapReferences) {
7349 __ UnpoisonHeapReference(out.AsRegister<Register>());
7350 }
7351}
7352
Roland Levillain7c1559a2015-12-15 10:55:36 +00007353void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7354 Location out,
7355 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007356 DCHECK(kEmitCompilerReadBarrier);
7357
Roland Levillain7c1559a2015-12-15 10:55:36 +00007358 // Insert a slow path based read barrier *after* the GC root load.
7359 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007360 // Note that GC roots are not affected by heap poisoning, so we do
7361 // not need to do anything special for this here.
7362 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007363 new (GetScopedAllocator()) ReadBarrierForRootSlowPathX86(instruction, out, root);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007364 AddSlowPath(slow_path);
7365
Roland Levillain0d5a2812015-11-13 10:07:31 +00007366 __ jmp(slow_path->GetEntryLabel());
7367 __ Bind(slow_path->GetExitLabel());
7368}
7369
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007370void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007371 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007372 LOG(FATAL) << "Unreachable";
7373}
7374
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007375void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007376 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007377 LOG(FATAL) << "Unreachable";
7378}
7379
Mark Mendellfe57faa2015-09-18 09:26:15 -04007380// Simple implementation of packed switch - generate cascaded compare/jumps.
7381void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7382 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007383 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendellfe57faa2015-09-18 09:26:15 -04007384 locations->SetInAt(0, Location::RequiresRegister());
7385}
7386
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007387void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
7388 int32_t lower_bound,
7389 uint32_t num_entries,
7390 HBasicBlock* switch_block,
7391 HBasicBlock* default_block) {
7392 // Figure out the correct compare values and jump conditions.
7393 // Handle the first compare/branch as a special case because it might
7394 // jump to the default case.
7395 DCHECK_GT(num_entries, 2u);
7396 Condition first_condition;
7397 uint32_t index;
7398 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
7399 if (lower_bound != 0) {
7400 first_condition = kLess;
7401 __ cmpl(value_reg, Immediate(lower_bound));
7402 __ j(first_condition, codegen_->GetLabelOf(default_block));
7403 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007404
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007405 index = 1;
7406 } else {
7407 // Handle all the compare/jumps below.
7408 first_condition = kBelow;
7409 index = 0;
7410 }
7411
7412 // Handle the rest of the compare/jumps.
7413 for (; index + 1 < num_entries; index += 2) {
7414 int32_t compare_to_value = lower_bound + index + 1;
7415 __ cmpl(value_reg, Immediate(compare_to_value));
7416 // Jump to successors[index] if value < case_value[index].
7417 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
7418 // Jump to successors[index + 1] if value == case_value[index + 1].
7419 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
7420 }
7421
7422 if (index != num_entries) {
7423 // There are an odd number of entries. Handle the last one.
7424 DCHECK_EQ(index + 1, num_entries);
7425 __ cmpl(value_reg, Immediate(lower_bound + index));
7426 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007427 }
7428
7429 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007430 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
7431 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007432 }
7433}
7434
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007435void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7436 int32_t lower_bound = switch_instr->GetStartValue();
7437 uint32_t num_entries = switch_instr->GetNumEntries();
7438 LocationSummary* locations = switch_instr->GetLocations();
7439 Register value_reg = locations->InAt(0).AsRegister<Register>();
7440
7441 GenPackedSwitchWithCompares(value_reg,
7442 lower_bound,
7443 num_entries,
7444 switch_instr->GetBlock(),
7445 switch_instr->GetDefaultBlock());
7446}
7447
Mark Mendell805b3b52015-09-18 14:10:29 -04007448void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7449 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007450 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendell805b3b52015-09-18 14:10:29 -04007451 locations->SetInAt(0, Location::RequiresRegister());
7452
7453 // Constant area pointer.
7454 locations->SetInAt(1, Location::RequiresRegister());
7455
7456 // And the temporary we need.
7457 locations->AddTemp(Location::RequiresRegister());
7458}
7459
7460void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7461 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007462 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04007463 LocationSummary* locations = switch_instr->GetLocations();
7464 Register value_reg = locations->InAt(0).AsRegister<Register>();
7465 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7466
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007467 if (num_entries <= kPackedSwitchJumpTableThreshold) {
7468 GenPackedSwitchWithCompares(value_reg,
7469 lower_bound,
7470 num_entries,
7471 switch_instr->GetBlock(),
7472 default_block);
7473 return;
7474 }
7475
Mark Mendell805b3b52015-09-18 14:10:29 -04007476 // Optimizing has a jump area.
7477 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7478 Register constant_area = locations->InAt(1).AsRegister<Register>();
7479
7480 // Remove the bias, if needed.
7481 if (lower_bound != 0) {
7482 __ leal(temp_reg, Address(value_reg, -lower_bound));
7483 value_reg = temp_reg;
7484 }
7485
7486 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007487 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04007488 __ cmpl(value_reg, Immediate(num_entries - 1));
7489 __ j(kAbove, codegen_->GetLabelOf(default_block));
7490
7491 // We are in the range of the table.
7492 // Load (target-constant_area) from the jump table, indexing by the value.
7493 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
7494
7495 // Compute the actual target address by adding in constant_area.
7496 __ addl(temp_reg, constant_area);
7497
7498 // And jump.
7499 __ jmp(temp_reg);
7500}
7501
Mark Mendell0616ae02015-04-17 12:49:27 -04007502void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
7503 HX86ComputeBaseMethodAddress* insn) {
7504 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007505 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Mark Mendell0616ae02015-04-17 12:49:27 -04007506 locations->SetOut(Location::RequiresRegister());
7507}
7508
7509void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
7510 HX86ComputeBaseMethodAddress* insn) {
7511 LocationSummary* locations = insn->GetLocations();
7512 Register reg = locations->Out().AsRegister<Register>();
7513
7514 // Generate call to next instruction.
7515 Label next_instruction;
7516 __ call(&next_instruction);
7517 __ Bind(&next_instruction);
7518
7519 // Remember this offset for later use with constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007520 codegen_->AddMethodAddressOffset(insn, GetAssembler()->CodeSize());
Mark Mendell0616ae02015-04-17 12:49:27 -04007521
7522 // Grab the return address off the stack.
7523 __ popl(reg);
7524}
7525
7526void LocationsBuilderX86::VisitX86LoadFromConstantTable(
7527 HX86LoadFromConstantTable* insn) {
7528 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007529 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Mark Mendell0616ae02015-04-17 12:49:27 -04007530
7531 locations->SetInAt(0, Location::RequiresRegister());
7532 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
7533
7534 // If we don't need to be materialized, we only need the inputs to be set.
David Brazdilb3e773e2016-01-26 11:28:37 +00007535 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007536 return;
7537 }
7538
7539 switch (insn->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007540 case DataType::Type::kFloat32:
7541 case DataType::Type::kFloat64:
Mark Mendell0616ae02015-04-17 12:49:27 -04007542 locations->SetOut(Location::RequiresFpuRegister());
7543 break;
7544
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007545 case DataType::Type::kInt32:
Mark Mendell0616ae02015-04-17 12:49:27 -04007546 locations->SetOut(Location::RequiresRegister());
7547 break;
7548
7549 default:
7550 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7551 }
7552}
7553
7554void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
David Brazdilb3e773e2016-01-26 11:28:37 +00007555 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007556 return;
7557 }
7558
7559 LocationSummary* locations = insn->GetLocations();
7560 Location out = locations->Out();
7561 Register const_area = locations->InAt(0).AsRegister<Register>();
7562 HConstant *value = insn->GetConstant();
7563
7564 switch (insn->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007565 case DataType::Type::kFloat32:
Mark Mendell0616ae02015-04-17 12:49:27 -04007566 __ movss(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007567 codegen_->LiteralFloatAddress(
7568 value->AsFloatConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04007569 break;
7570
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007571 case DataType::Type::kFloat64:
Mark Mendell0616ae02015-04-17 12:49:27 -04007572 __ movsd(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007573 codegen_->LiteralDoubleAddress(
7574 value->AsDoubleConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04007575 break;
7576
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007577 case DataType::Type::kInt32:
Mark Mendell0616ae02015-04-17 12:49:27 -04007578 __ movl(out.AsRegister<Register>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007579 codegen_->LiteralInt32Address(
7580 value->AsIntConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04007581 break;
7582
7583 default:
7584 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7585 }
7586}
7587
Mark Mendell0616ae02015-04-17 12:49:27 -04007588/**
7589 * Class to handle late fixup of offsets into constant area.
7590 */
Vladimir Marko5233f932015-09-29 19:01:15 +01007591class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04007592 public:
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007593 RIPFixup(CodeGeneratorX86& codegen,
7594 HX86ComputeBaseMethodAddress* base_method_address,
7595 size_t offset)
7596 : codegen_(&codegen),
7597 base_method_address_(base_method_address),
7598 offset_into_constant_area_(offset) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04007599
7600 protected:
7601 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
7602
7603 CodeGeneratorX86* codegen_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007604 HX86ComputeBaseMethodAddress* base_method_address_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007605
7606 private:
7607 void Process(const MemoryRegion& region, int pos) OVERRIDE {
7608 // Patch the correct offset for the instruction. The place to patch is the
7609 // last 4 bytes of the instruction.
7610 // The value to patch is the distance from the offset in the constant area
7611 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04007612 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007613 int32_t relative_position =
7614 constant_offset - codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell0616ae02015-04-17 12:49:27 -04007615
7616 // Patch in the right value.
7617 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
7618 }
7619
Mark Mendell0616ae02015-04-17 12:49:27 -04007620 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04007621 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007622};
7623
Mark Mendell805b3b52015-09-18 14:10:29 -04007624/**
7625 * Class to handle late fixup of offsets to a jump table that will be created in the
7626 * constant area.
7627 */
7628class JumpTableRIPFixup : public RIPFixup {
7629 public:
7630 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007631 : RIPFixup(codegen, switch_instr->GetBaseMethodAddress(), static_cast<size_t>(-1)),
7632 switch_instr_(switch_instr) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04007633
7634 void CreateJumpTable() {
7635 X86Assembler* assembler = codegen_->GetAssembler();
7636
7637 // Ensure that the reference to the jump table has the correct offset.
7638 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
7639 SetOffset(offset_in_constant_table);
7640
7641 // The label values in the jump table are computed relative to the
7642 // instruction addressing the constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007643 const int32_t relative_offset = codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell805b3b52015-09-18 14:10:29 -04007644
7645 // Populate the jump table with the correct values for the jump table.
7646 int32_t num_entries = switch_instr_->GetNumEntries();
7647 HBasicBlock* block = switch_instr_->GetBlock();
7648 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
7649 // The value that we want is the target offset - the position of the table.
7650 for (int32_t i = 0; i < num_entries; i++) {
7651 HBasicBlock* b = successors[i];
7652 Label* l = codegen_->GetLabelOf(b);
7653 DCHECK(l->IsBound());
7654 int32_t offset_to_block = l->Position() - relative_offset;
7655 assembler->AppendInt32(offset_to_block);
7656 }
7657 }
7658
7659 private:
7660 const HX86PackedSwitch* switch_instr_;
7661};
7662
7663void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
7664 // Generate the constant area if needed.
7665 X86Assembler* assembler = GetAssembler();
7666 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
7667 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
7668 // byte values.
7669 assembler->Align(4, 0);
7670 constant_area_start_ = assembler->CodeSize();
7671
7672 // Populate any jump tables.
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007673 for (JumpTableRIPFixup* jump_table : fixups_to_jump_tables_) {
Mark Mendell805b3b52015-09-18 14:10:29 -04007674 jump_table->CreateJumpTable();
7675 }
7676
7677 // And now add the constant area to the generated code.
7678 assembler->AddConstantArea();
7679 }
7680
7681 // And finish up.
7682 CodeGenerator::Finalize(allocator);
7683}
7684
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007685Address CodeGeneratorX86::LiteralDoubleAddress(double v,
7686 HX86ComputeBaseMethodAddress* method_base,
7687 Register reg) {
7688 AssemblerFixup* fixup =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007689 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddDouble(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04007690 return Address(reg, kDummy32BitOffset, fixup);
7691}
7692
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007693Address CodeGeneratorX86::LiteralFloatAddress(float v,
7694 HX86ComputeBaseMethodAddress* method_base,
7695 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007696 AssemblerFixup* fixup =
7697 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddFloat(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04007698 return Address(reg, kDummy32BitOffset, fixup);
7699}
7700
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007701Address CodeGeneratorX86::LiteralInt32Address(int32_t v,
7702 HX86ComputeBaseMethodAddress* method_base,
7703 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007704 AssemblerFixup* fixup =
7705 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddInt32(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04007706 return Address(reg, kDummy32BitOffset, fixup);
7707}
7708
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007709Address CodeGeneratorX86::LiteralInt64Address(int64_t v,
7710 HX86ComputeBaseMethodAddress* method_base,
7711 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007712 AssemblerFixup* fixup =
7713 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddInt64(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04007714 return Address(reg, kDummy32BitOffset, fixup);
7715}
7716
Aart Bika19616e2016-02-01 18:57:58 -08007717void CodeGeneratorX86::Load32BitValue(Register dest, int32_t value) {
7718 if (value == 0) {
7719 __ xorl(dest, dest);
7720 } else {
7721 __ movl(dest, Immediate(value));
7722 }
7723}
7724
7725void CodeGeneratorX86::Compare32BitValue(Register dest, int32_t value) {
7726 if (value == 0) {
7727 __ testl(dest, dest);
7728 } else {
7729 __ cmpl(dest, Immediate(value));
7730 }
7731}
7732
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007733void CodeGeneratorX86::GenerateIntCompare(Location lhs, Location rhs) {
7734 Register lhs_reg = lhs.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07007735 GenerateIntCompare(lhs_reg, rhs);
7736}
7737
7738void CodeGeneratorX86::GenerateIntCompare(Register lhs, Location rhs) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007739 if (rhs.IsConstant()) {
7740 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
jessicahandojo4877b792016-09-08 19:49:13 -07007741 Compare32BitValue(lhs, value);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007742 } else if (rhs.IsStackSlot()) {
jessicahandojo4877b792016-09-08 19:49:13 -07007743 __ cmpl(lhs, Address(ESP, rhs.GetStackIndex()));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007744 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07007745 __ cmpl(lhs, rhs.AsRegister<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007746 }
7747}
7748
7749Address CodeGeneratorX86::ArrayAddress(Register obj,
7750 Location index,
7751 ScaleFactor scale,
7752 uint32_t data_offset) {
7753 return index.IsConstant() ?
7754 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << scale) + data_offset) :
7755 Address(obj, index.AsRegister<Register>(), scale, data_offset);
7756}
7757
Mark Mendell805b3b52015-09-18 14:10:29 -04007758Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
7759 Register reg,
7760 Register value) {
7761 // Create a fixup to be used to create and address the jump table.
7762 JumpTableRIPFixup* table_fixup =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007763 new (GetGraph()->GetAllocator()) JumpTableRIPFixup(*this, switch_instr);
Mark Mendell805b3b52015-09-18 14:10:29 -04007764
7765 // We have to populate the jump tables.
7766 fixups_to_jump_tables_.push_back(table_fixup);
7767
7768 // We want a scaled address, as we are extracting the correct offset from the table.
7769 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
7770}
7771
Andreas Gampe85b62f22015-09-09 13:15:38 -07007772// TODO: target as memory.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007773void CodeGeneratorX86::MoveFromReturnRegister(Location target, DataType::Type type) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07007774 if (!target.IsValid()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007775 DCHECK_EQ(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007776 return;
7777 }
7778
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007779 DCHECK_NE(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007780
7781 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
7782 if (target.Equals(return_loc)) {
7783 return;
7784 }
7785
7786 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7787 // with the else branch.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007788 if (type == DataType::Type::kInt64) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007789 HParallelMove parallel_move(GetGraph()->GetAllocator());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007790 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), DataType::Type::kInt32, nullptr);
7791 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), DataType::Type::kInt32, nullptr);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007792 GetMoveResolver()->EmitNativeCode(&parallel_move);
7793 } else {
7794 // Let the parallel move resolver take care of all of this.
Vladimir Markoca6fff82017-10-03 14:49:14 +01007795 HParallelMove parallel_move(GetGraph()->GetAllocator());
Andreas Gampe85b62f22015-09-09 13:15:38 -07007796 parallel_move.AddMove(return_loc, target, type, nullptr);
7797 GetMoveResolver()->EmitNativeCode(&parallel_move);
7798 }
7799}
7800
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007801void CodeGeneratorX86::PatchJitRootUse(uint8_t* code,
7802 const uint8_t* roots_data,
7803 const PatchInfo<Label>& info,
7804 uint64_t index_in_table) const {
7805 uint32_t code_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
7806 uintptr_t address =
7807 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
7808 typedef __attribute__((__aligned__(1))) uint32_t unaligned_uint32_t;
7809 reinterpret_cast<unaligned_uint32_t*>(code + code_offset)[0] =
7810 dchecked_integral_cast<uint32_t>(address);
7811}
7812
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007813void CodeGeneratorX86::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
7814 for (const PatchInfo<Label>& info : jit_string_patches_) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01007815 StringReference string_reference(&info.dex_file, dex::StringIndex(info.index));
7816 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007817 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007818 }
7819
7820 for (const PatchInfo<Label>& info : jit_class_patches_) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01007821 TypeReference type_reference(&info.dex_file, dex::TypeIndex(info.index));
7822 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007823 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007824 }
7825}
7826
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007827void LocationsBuilderX86::VisitIntermediateAddress(HIntermediateAddress* instruction
7828 ATTRIBUTE_UNUSED) {
7829 LOG(FATAL) << "Unreachable";
7830}
7831
7832void InstructionCodeGeneratorX86::VisitIntermediateAddress(HIntermediateAddress* instruction
7833 ATTRIBUTE_UNUSED) {
7834 LOG(FATAL) << "Unreachable";
7835}
7836
Roland Levillain4d027112015-07-01 15:41:14 +01007837#undef __
7838
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007839} // namespace x86
7840} // namespace art