blob: f84dd0045e7ef018a7b1ee89714ea7b4d296b9d1 [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) {
Vladimir Marko33bff252017-11-01 14:35:42 +00001075 size_t reserved_bytes = GetStackOverflowReservedBytes(InstructionSet::kX86);
1076 __ testl(EAX, Address(ESP, -static_cast<int32_t>(reserved_bytes)));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001077 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +01001078 }
1079
Mark Mendell5f874182015-03-04 15:42:45 -05001080 if (HasEmptyFrame()) {
1081 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001082 }
Mark Mendell5f874182015-03-04 15:42:45 -05001083
1084 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
1085 Register reg = kCoreCalleeSaves[i];
1086 if (allocated_registers_.ContainsCoreRegister(reg)) {
1087 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001088 __ cfi().AdjustCFAOffset(kX86WordSize);
1089 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -05001090 }
1091 }
1092
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001093 int adjust = GetFrameSize() - FrameEntrySpillSize();
1094 __ subl(ESP, Immediate(adjust));
1095 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001096 // Save the current method if we need it. Note that we do not
1097 // do this in HCurrentMethod, as the instruction might have been removed
1098 // in the SSA graph.
1099 if (RequiresCurrentMethod()) {
1100 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
1101 }
Nicolas Geoffrayf7893532017-06-15 12:34:36 +01001102
1103 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1104 // Initialize should_deoptimize flag to 0.
1105 __ movl(Address(ESP, GetStackOffsetOfShouldDeoptimizeFlag()), Immediate(0));
1106 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001107}
1108
1109void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001110 __ cfi().RememberState();
1111 if (!HasEmptyFrame()) {
1112 int adjust = GetFrameSize() - FrameEntrySpillSize();
1113 __ addl(ESP, Immediate(adjust));
1114 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -05001115
David Srbeckyc34dc932015-04-12 09:27:43 +01001116 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1117 Register reg = kCoreCalleeSaves[i];
1118 if (allocated_registers_.ContainsCoreRegister(reg)) {
1119 __ popl(reg);
1120 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
1121 __ cfi().Restore(DWARFReg(reg));
1122 }
Mark Mendell5f874182015-03-04 15:42:45 -05001123 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001124 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001125 __ ret();
1126 __ cfi().RestoreState();
1127 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001128}
1129
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001130void CodeGeneratorX86::Bind(HBasicBlock* block) {
1131 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001132}
1133
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001134Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(DataType::Type type) const {
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001135 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001136 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001137 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001138 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001139 case DataType::Type::kInt8:
1140 case DataType::Type::kUint16:
1141 case DataType::Type::kInt16:
1142 case DataType::Type::kInt32:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001143 return Location::RegisterLocation(EAX);
1144
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001145 case DataType::Type::kInt64:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001146 return Location::RegisterPairLocation(EAX, EDX);
1147
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001148 case DataType::Type::kVoid:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001149 return Location::NoLocation();
1150
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001151 case DataType::Type::kFloat64:
1152 case DataType::Type::kFloat32:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001153 return Location::FpuRegisterLocation(XMM0);
1154 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001155
1156 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001157}
1158
1159Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
1160 return Location::RegisterLocation(kMethodRegisterArgument);
1161}
1162
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001163Location InvokeDexCallingConventionVisitorX86::GetNextLocation(DataType::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001164 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001165 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001166 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001167 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001168 case DataType::Type::kInt8:
1169 case DataType::Type::kUint16:
1170 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001171 case DataType::Type::kInt32: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001172 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001173 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001174 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001175 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001176 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001177 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001178 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001179 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001180
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001181 case DataType::Type::kInt64: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001182 uint32_t index = gp_index_;
1183 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001184 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001185 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001186 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
1187 calling_convention.GetRegisterPairAt(index));
1188 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001189 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001190 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1191 }
1192 }
1193
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001194 case DataType::Type::kFloat32: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001195 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001196 stack_index_++;
1197 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1198 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1199 } else {
1200 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1201 }
1202 }
1203
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001204 case DataType::Type::kFloat64: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001205 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001206 stack_index_ += 2;
1207 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1208 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1209 } else {
1210 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001211 }
1212 }
1213
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001214 case DataType::Type::kVoid:
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001215 LOG(FATAL) << "Unexpected parameter type " << type;
1216 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001217 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00001218 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001219}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001220
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001221void CodeGeneratorX86::Move32(Location destination, Location source) {
1222 if (source.Equals(destination)) {
1223 return;
1224 }
1225 if (destination.IsRegister()) {
1226 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001227 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001228 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001229 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001230 } else {
1231 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001232 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001233 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001234 } else if (destination.IsFpuRegister()) {
1235 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001236 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001237 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001238 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001239 } else {
1240 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001241 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001242 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001243 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001244 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001245 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001246 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001247 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001248 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05001249 } else if (source.IsConstant()) {
1250 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001251 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05001252 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001253 } else {
1254 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001255 __ pushl(Address(ESP, source.GetStackIndex()));
1256 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001257 }
1258 }
1259}
1260
1261void CodeGeneratorX86::Move64(Location destination, Location source) {
1262 if (source.Equals(destination)) {
1263 return;
1264 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001265 if (destination.IsRegisterPair()) {
1266 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001267 EmitParallelMoves(
1268 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1269 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001270 DataType::Type::kInt32,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001271 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001272 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001273 DataType::Type::kInt32);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001274 } else if (source.IsFpuRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001275 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
1276 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
1277 __ psrlq(src_reg, Immediate(32));
1278 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001279 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001280 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001281 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001282 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
1283 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001284 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
1285 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001286 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05001287 if (source.IsFpuRegister()) {
1288 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
1289 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001290 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001291 } else if (source.IsRegisterPair()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001292 size_t elem_size = DataType::Size(DataType::Type::kInt32);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001293 // Create stack space for 2 elements.
1294 __ subl(ESP, Immediate(2 * elem_size));
1295 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
1296 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
1297 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
1298 // And remove the temporary stack space we allocated.
1299 __ addl(ESP, Immediate(2 * elem_size));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001300 } else {
1301 LOG(FATAL) << "Unimplemented";
1302 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001303 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001304 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001305 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001306 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001307 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001308 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001309 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001310 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001311 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001312 } else if (source.IsConstant()) {
1313 HConstant* constant = source.GetConstant();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001314 DCHECK(constant->IsLongConstant() || constant->IsDoubleConstant());
1315 int64_t value = GetInt64ValueOf(constant);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001316 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001317 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
1318 Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001319 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001320 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001321 EmitParallelMoves(
1322 Location::StackSlot(source.GetStackIndex()),
1323 Location::StackSlot(destination.GetStackIndex()),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001324 DataType::Type::kInt32,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001325 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001326 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001327 DataType::Type::kInt32);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001328 }
1329 }
1330}
1331
Calin Juravle175dc732015-08-25 15:42:32 +01001332void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
1333 DCHECK(location.IsRegister());
1334 __ movl(location.AsRegister<Register>(), Immediate(value));
1335}
1336
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001337void CodeGeneratorX86::MoveLocation(Location dst, Location src, DataType::Type dst_type) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001338 HParallelMove move(GetGraph()->GetAllocator());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001339 if (dst_type == DataType::Type::kInt64 && !src.IsConstant() && !src.IsFpuRegister()) {
1340 move.AddMove(src.ToLow(), dst.ToLow(), DataType::Type::kInt32, nullptr);
1341 move.AddMove(src.ToHigh(), dst.ToHigh(), DataType::Type::kInt32, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001342 } else {
David Brazdil74eb1b22015-12-14 11:44:01 +00001343 move.AddMove(src, dst, dst_type, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001344 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001345 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001346}
1347
1348void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1349 if (location.IsRegister()) {
1350 locations->AddTemp(location);
1351 } else if (location.IsRegisterPair()) {
1352 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1353 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1354 } else {
1355 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1356 }
1357}
1358
David Brazdilfc6a86a2015-06-26 10:33:45 +00001359void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001360 DCHECK(!successor->IsExitBlock());
1361
1362 HBasicBlock* block = got->GetBlock();
1363 HInstruction* previous = got->GetPrevious();
1364
1365 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001366 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001367 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1368 return;
1369 }
1370
1371 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1372 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1373 }
1374 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001375 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001376 }
1377}
1378
David Brazdilfc6a86a2015-06-26 10:33:45 +00001379void LocationsBuilderX86::VisitGoto(HGoto* got) {
1380 got->SetLocations(nullptr);
1381}
1382
1383void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1384 HandleGoto(got, got->GetSuccessor());
1385}
1386
1387void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1388 try_boundary->SetLocations(nullptr);
1389}
1390
1391void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1392 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1393 if (!successor->IsExitBlock()) {
1394 HandleGoto(try_boundary, successor);
1395 }
1396}
1397
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001398void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001399 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001400}
1401
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001402void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001403}
1404
Mark Mendell152408f2015-12-31 12:28:50 -05001405template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001406void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001407 LabelType* true_label,
1408 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001409 if (cond->IsFPConditionTrueIfNaN()) {
1410 __ j(kUnordered, true_label);
1411 } else if (cond->IsFPConditionFalseIfNaN()) {
1412 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001413 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001414 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001415}
1416
Mark Mendell152408f2015-12-31 12:28:50 -05001417template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001418void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001419 LabelType* true_label,
1420 LabelType* false_label) {
Mark Mendellc4701932015-04-10 13:18:51 -04001421 LocationSummary* locations = cond->GetLocations();
1422 Location left = locations->InAt(0);
1423 Location right = locations->InAt(1);
1424 IfCondition if_cond = cond->GetCondition();
1425
Mark Mendellc4701932015-04-10 13:18:51 -04001426 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001427 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001428 IfCondition true_high_cond = if_cond;
1429 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001430 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001431
1432 // Set the conditions for the test, remembering that == needs to be
1433 // decided using the low words.
1434 switch (if_cond) {
1435 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001436 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001437 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001438 break;
1439 case kCondLT:
1440 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001441 break;
1442 case kCondLE:
1443 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001444 break;
1445 case kCondGT:
1446 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001447 break;
1448 case kCondGE:
1449 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001450 break;
Aart Bike9f37602015-10-09 11:15:55 -07001451 case kCondB:
1452 false_high_cond = kCondA;
1453 break;
1454 case kCondBE:
1455 true_high_cond = kCondB;
1456 break;
1457 case kCondA:
1458 false_high_cond = kCondB;
1459 break;
1460 case kCondAE:
1461 true_high_cond = kCondA;
1462 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001463 }
1464
1465 if (right.IsConstant()) {
1466 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001467 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001468 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001469
Aart Bika19616e2016-02-01 18:57:58 -08001470 codegen_->Compare32BitValue(left_high, val_high);
Mark Mendellc4701932015-04-10 13:18:51 -04001471 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001472 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001473 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001474 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001475 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001476 __ j(X86Condition(true_high_cond), true_label);
1477 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001478 }
1479 // Must be equal high, so compare the lows.
Aart Bika19616e2016-02-01 18:57:58 -08001480 codegen_->Compare32BitValue(left_low, val_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001481 } else if (right.IsRegisterPair()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001482 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001483 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001484
1485 __ cmpl(left_high, right_high);
1486 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001487 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001488 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001489 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001490 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001491 __ j(X86Condition(true_high_cond), true_label);
1492 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001493 }
1494 // Must be equal high, so compare the lows.
1495 __ cmpl(left_low, right_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001496 } else {
1497 DCHECK(right.IsDoubleStackSlot());
1498 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
1499 if (if_cond == kCondNE) {
1500 __ j(X86Condition(true_high_cond), true_label);
1501 } else if (if_cond == kCondEQ) {
1502 __ j(X86Condition(false_high_cond), false_label);
1503 } else {
1504 __ j(X86Condition(true_high_cond), true_label);
1505 __ j(X86Condition(false_high_cond), false_label);
1506 }
1507 // Must be equal high, so compare the lows.
1508 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Mark Mendellc4701932015-04-10 13:18:51 -04001509 }
1510 // The last comparison might be unsigned.
1511 __ j(final_condition, true_label);
1512}
1513
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001514void InstructionCodeGeneratorX86::GenerateFPCompare(Location lhs,
1515 Location rhs,
1516 HInstruction* insn,
1517 bool is_double) {
1518 HX86LoadFromConstantTable* const_area = insn->InputAt(1)->AsX86LoadFromConstantTable();
1519 if (is_double) {
1520 if (rhs.IsFpuRegister()) {
1521 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1522 } else if (const_area != nullptr) {
1523 DCHECK(const_area->IsEmittedAtUseSite());
1524 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(),
1525 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001526 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
1527 const_area->GetBaseMethodAddress(),
1528 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001529 } else {
1530 DCHECK(rhs.IsDoubleStackSlot());
1531 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1532 }
1533 } else {
1534 if (rhs.IsFpuRegister()) {
1535 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1536 } else if (const_area != nullptr) {
1537 DCHECK(const_area->IsEmittedAtUseSite());
1538 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(),
1539 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001540 const_area->GetConstant()->AsFloatConstant()->GetValue(),
1541 const_area->GetBaseMethodAddress(),
1542 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001543 } else {
1544 DCHECK(rhs.IsStackSlot());
1545 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1546 }
1547 }
1548}
1549
Mark Mendell152408f2015-12-31 12:28:50 -05001550template<class LabelType>
David Brazdil0debae72015-11-12 18:37:00 +00001551void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendell152408f2015-12-31 12:28:50 -05001552 LabelType* true_target_in,
1553 LabelType* false_target_in) {
David Brazdil0debae72015-11-12 18:37:00 +00001554 // Generated branching requires both targets to be explicit. If either of the
1555 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Mark Mendell152408f2015-12-31 12:28:50 -05001556 LabelType fallthrough_target;
1557 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1558 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
David Brazdil0debae72015-11-12 18:37:00 +00001559
Mark Mendellc4701932015-04-10 13:18:51 -04001560 LocationSummary* locations = condition->GetLocations();
1561 Location left = locations->InAt(0);
1562 Location right = locations->InAt(1);
1563
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001564 DataType::Type type = condition->InputAt(0)->GetType();
Mark Mendellc4701932015-04-10 13:18:51 -04001565 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001566 case DataType::Type::kInt64:
Mark Mendellc4701932015-04-10 13:18:51 -04001567 GenerateLongComparesAndJumps(condition, true_target, false_target);
1568 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001569 case DataType::Type::kFloat32:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001570 GenerateFPCompare(left, right, condition, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001571 GenerateFPJumps(condition, true_target, false_target);
1572 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001573 case DataType::Type::kFloat64:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001574 GenerateFPCompare(left, right, condition, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001575 GenerateFPJumps(condition, true_target, false_target);
1576 break;
1577 default:
1578 LOG(FATAL) << "Unexpected compare type " << type;
1579 }
1580
David Brazdil0debae72015-11-12 18:37:00 +00001581 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001582 __ jmp(false_target);
1583 }
David Brazdil0debae72015-11-12 18:37:00 +00001584
1585 if (fallthrough_target.IsLinked()) {
1586 __ Bind(&fallthrough_target);
1587 }
Mark Mendellc4701932015-04-10 13:18:51 -04001588}
1589
David Brazdil0debae72015-11-12 18:37:00 +00001590static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1591 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1592 // are set only strictly before `branch`. We can't use the eflags on long/FP
1593 // conditions if they are materialized due to the complex branching.
1594 return cond->IsCondition() &&
1595 cond->GetNext() == branch &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001596 cond->InputAt(0)->GetType() != DataType::Type::kInt64 &&
1597 !DataType::IsFloatingPointType(cond->InputAt(0)->GetType());
David Brazdil0debae72015-11-12 18:37:00 +00001598}
1599
Mark Mendell152408f2015-12-31 12:28:50 -05001600template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001601void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001602 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001603 LabelType* true_target,
1604 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001605 HInstruction* cond = instruction->InputAt(condition_input_index);
1606
1607 if (true_target == nullptr && false_target == nullptr) {
1608 // Nothing to do. The code always falls through.
1609 return;
1610 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001611 // Constant condition, statically compared against "true" (integer value 1).
1612 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001613 if (true_target != nullptr) {
1614 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001615 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001616 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001617 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001618 if (false_target != nullptr) {
1619 __ jmp(false_target);
1620 }
1621 }
1622 return;
1623 }
1624
1625 // The following code generates these patterns:
1626 // (1) true_target == nullptr && false_target != nullptr
1627 // - opposite condition true => branch to false_target
1628 // (2) true_target != nullptr && false_target == nullptr
1629 // - condition true => branch to true_target
1630 // (3) true_target != nullptr && false_target != nullptr
1631 // - condition true => branch to true_target
1632 // - branch to false_target
1633 if (IsBooleanValueOrMaterializedCondition(cond)) {
1634 if (AreEflagsSetFrom(cond, instruction)) {
1635 if (true_target == nullptr) {
1636 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
1637 } else {
1638 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
1639 }
1640 } else {
1641 // Materialized condition, compare against 0.
1642 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1643 if (lhs.IsRegister()) {
1644 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1645 } else {
1646 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1647 }
1648 if (true_target == nullptr) {
1649 __ j(kEqual, false_target);
1650 } else {
1651 __ j(kNotEqual, true_target);
1652 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001653 }
1654 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001655 // Condition has not been materialized, use its inputs as the comparison and
1656 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001657 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001658
1659 // If this is a long or FP comparison that has been folded into
1660 // the HCondition, generate the comparison directly.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001661 DataType::Type type = condition->InputAt(0)->GetType();
1662 if (type == DataType::Type::kInt64 || DataType::IsFloatingPointType(type)) {
David Brazdil0debae72015-11-12 18:37:00 +00001663 GenerateCompareTestAndBranch(condition, true_target, false_target);
1664 return;
1665 }
1666
1667 Location lhs = condition->GetLocations()->InAt(0);
1668 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001669 // LHS is guaranteed to be in a register (see LocationsBuilderX86::HandleCondition).
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001670 codegen_->GenerateIntCompare(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00001671 if (true_target == nullptr) {
1672 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1673 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001674 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001675 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001676 }
David Brazdil0debae72015-11-12 18:37:00 +00001677
1678 // If neither branch falls through (case 3), the conditional branch to `true_target`
1679 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1680 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001681 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001682 }
1683}
1684
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001685void LocationsBuilderX86::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001686 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00001687 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001688 locations->SetInAt(0, Location::Any());
1689 }
1690}
1691
1692void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001693 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1694 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1695 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1696 nullptr : codegen_->GetLabelOf(true_successor);
1697 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1698 nullptr : codegen_->GetLabelOf(false_successor);
1699 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001700}
1701
1702void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001703 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001704 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01001705 InvokeRuntimeCallingConvention calling_convention;
1706 RegisterSet caller_saves = RegisterSet::Empty();
1707 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1708 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00001709 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001710 locations->SetInAt(0, Location::Any());
1711 }
1712}
1713
1714void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001715 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001716 GenerateTestAndBranch<Label>(deoptimize,
1717 /* condition_input_index */ 0,
1718 slow_path->GetEntryLabel(),
1719 /* false_target */ nullptr);
1720}
1721
Mingyao Yang063fc772016-08-02 11:02:54 -07001722void LocationsBuilderX86::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001723 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yang063fc772016-08-02 11:02:54 -07001724 LocationSummary(flag, LocationSummary::kNoCall);
1725 locations->SetOut(Location::RequiresRegister());
1726}
1727
1728void InstructionCodeGeneratorX86::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
1729 __ movl(flag->GetLocations()->Out().AsRegister<Register>(),
1730 Address(ESP, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
1731}
1732
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001733static bool SelectCanUseCMOV(HSelect* select) {
1734 // There are no conditional move instructions for XMMs.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001735 if (DataType::IsFloatingPointType(select->GetType())) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001736 return false;
1737 }
1738
1739 // A FP condition doesn't generate the single CC that we need.
1740 // In 32 bit mode, a long condition doesn't generate a single CC either.
1741 HInstruction* condition = select->GetCondition();
1742 if (condition->IsCondition()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001743 DataType::Type compare_type = condition->InputAt(0)->GetType();
1744 if (compare_type == DataType::Type::kInt64 ||
1745 DataType::IsFloatingPointType(compare_type)) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001746 return false;
1747 }
1748 }
1749
1750 // We can generate a CMOV for this Select.
1751 return true;
1752}
1753
David Brazdil74eb1b22015-12-14 11:44:01 +00001754void LocationsBuilderX86::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001755 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001756 if (DataType::IsFloatingPointType(select->GetType())) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001757 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001758 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001759 } else {
1760 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001761 if (SelectCanUseCMOV(select)) {
1762 if (select->InputAt(1)->IsConstant()) {
1763 // Cmov can't handle a constant value.
1764 locations->SetInAt(1, Location::RequiresRegister());
1765 } else {
1766 locations->SetInAt(1, Location::Any());
1767 }
1768 } else {
1769 locations->SetInAt(1, Location::Any());
1770 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001771 }
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001772 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1773 locations->SetInAt(2, Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00001774 }
1775 locations->SetOut(Location::SameAsFirstInput());
1776}
1777
1778void InstructionCodeGeneratorX86::VisitSelect(HSelect* select) {
1779 LocationSummary* locations = select->GetLocations();
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001780 DCHECK(locations->InAt(0).Equals(locations->Out()));
1781 if (SelectCanUseCMOV(select)) {
1782 // If both the condition and the source types are integer, we can generate
1783 // a CMOV to implement Select.
1784
1785 HInstruction* select_condition = select->GetCondition();
1786 Condition cond = kNotEqual;
1787
1788 // Figure out how to test the 'condition'.
1789 if (select_condition->IsCondition()) {
1790 HCondition* condition = select_condition->AsCondition();
1791 if (!condition->IsEmittedAtUseSite()) {
1792 // This was a previously materialized condition.
1793 // Can we use the existing condition code?
1794 if (AreEflagsSetFrom(condition, select)) {
1795 // Materialization was the previous instruction. Condition codes are right.
1796 cond = X86Condition(condition->GetCondition());
1797 } else {
1798 // No, we have to recreate the condition code.
1799 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1800 __ testl(cond_reg, cond_reg);
1801 }
1802 } else {
1803 // We can't handle FP or long here.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001804 DCHECK_NE(condition->InputAt(0)->GetType(), DataType::Type::kInt64);
1805 DCHECK(!DataType::IsFloatingPointType(condition->InputAt(0)->GetType()));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001806 LocationSummary* cond_locations = condition->GetLocations();
Roland Levillain0b671c02016-08-19 12:02:34 +01001807 codegen_->GenerateIntCompare(cond_locations->InAt(0), cond_locations->InAt(1));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001808 cond = X86Condition(condition->GetCondition());
1809 }
1810 } else {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01001811 // Must be a Boolean condition, which needs to be compared to 0.
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001812 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1813 __ testl(cond_reg, cond_reg);
1814 }
1815
1816 // If the condition is true, overwrite the output, which already contains false.
1817 Location false_loc = locations->InAt(0);
1818 Location true_loc = locations->InAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001819 if (select->GetType() == DataType::Type::kInt64) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001820 // 64 bit conditional move.
1821 Register false_high = false_loc.AsRegisterPairHigh<Register>();
1822 Register false_low = false_loc.AsRegisterPairLow<Register>();
1823 if (true_loc.IsRegisterPair()) {
1824 __ cmovl(cond, false_high, true_loc.AsRegisterPairHigh<Register>());
1825 __ cmovl(cond, false_low, true_loc.AsRegisterPairLow<Register>());
1826 } else {
1827 __ cmovl(cond, false_high, Address(ESP, true_loc.GetHighStackIndex(kX86WordSize)));
1828 __ cmovl(cond, false_low, Address(ESP, true_loc.GetStackIndex()));
1829 }
1830 } else {
1831 // 32 bit conditional move.
1832 Register false_reg = false_loc.AsRegister<Register>();
1833 if (true_loc.IsRegister()) {
1834 __ cmovl(cond, false_reg, true_loc.AsRegister<Register>());
1835 } else {
1836 __ cmovl(cond, false_reg, Address(ESP, true_loc.GetStackIndex()));
1837 }
1838 }
1839 } else {
1840 NearLabel false_target;
1841 GenerateTestAndBranch<NearLabel>(
1842 select, /* condition_input_index */ 2, /* true_target */ nullptr, &false_target);
1843 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1844 __ Bind(&false_target);
1845 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001846}
1847
David Srbecky0cf44932015-12-09 14:09:59 +00001848void LocationsBuilderX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001849 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00001850}
1851
David Srbeckyd28f4a02016-03-14 17:14:24 +00001852void InstructionCodeGeneratorX86::VisitNativeDebugInfo(HNativeDebugInfo*) {
1853 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001854}
1855
1856void CodeGeneratorX86::GenerateNop() {
1857 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001858}
1859
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001860void LocationsBuilderX86::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001861 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01001862 new (GetGraph()->GetAllocator()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001863 // Handle the long/FP comparisons made in instruction simplification.
1864 switch (cond->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001865 case DataType::Type::kInt64: {
Mark Mendellc4701932015-04-10 13:18:51 -04001866 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell8659e842016-02-16 10:41:46 -05001867 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001868 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001869 locations->SetOut(Location::RequiresRegister());
1870 }
1871 break;
1872 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001873 case DataType::Type::kFloat32:
1874 case DataType::Type::kFloat64: {
Mark Mendellc4701932015-04-10 13:18:51 -04001875 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001876 if (cond->InputAt(1)->IsX86LoadFromConstantTable()) {
1877 DCHECK(cond->InputAt(1)->IsEmittedAtUseSite());
1878 } else if (cond->InputAt(1)->IsConstant()) {
1879 locations->SetInAt(1, Location::RequiresFpuRegister());
1880 } else {
1881 locations->SetInAt(1, Location::Any());
1882 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001883 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001884 locations->SetOut(Location::RequiresRegister());
1885 }
1886 break;
1887 }
1888 default:
1889 locations->SetInAt(0, Location::RequiresRegister());
1890 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001891 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001892 // We need a byte register.
1893 locations->SetOut(Location::RegisterLocation(ECX));
1894 }
1895 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001896 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001897}
1898
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001899void InstructionCodeGeneratorX86::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001900 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001901 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001902 }
Mark Mendellc4701932015-04-10 13:18:51 -04001903
1904 LocationSummary* locations = cond->GetLocations();
1905 Location lhs = locations->InAt(0);
1906 Location rhs = locations->InAt(1);
1907 Register reg = locations->Out().AsRegister<Register>();
Mark Mendell152408f2015-12-31 12:28:50 -05001908 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001909
1910 switch (cond->InputAt(0)->GetType()) {
1911 default: {
1912 // Integer case.
1913
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01001914 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04001915 __ xorl(reg, reg);
Roland Levillain0b671c02016-08-19 12:02:34 +01001916 codegen_->GenerateIntCompare(lhs, rhs);
Aart Bike9f37602015-10-09 11:15:55 -07001917 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001918 return;
1919 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001920 case DataType::Type::kInt64:
Mark Mendellc4701932015-04-10 13:18:51 -04001921 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1922 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001923 case DataType::Type::kFloat32:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001924 GenerateFPCompare(lhs, rhs, cond, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001925 GenerateFPJumps(cond, &true_label, &false_label);
1926 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001927 case DataType::Type::kFloat64:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001928 GenerateFPCompare(lhs, rhs, cond, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001929 GenerateFPJumps(cond, &true_label, &false_label);
1930 break;
1931 }
1932
1933 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001934 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001935
Roland Levillain4fa13f62015-07-06 18:11:54 +01001936 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001937 __ Bind(&false_label);
1938 __ xorl(reg, reg);
1939 __ jmp(&done_label);
1940
Roland Levillain4fa13f62015-07-06 18:11:54 +01001941 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001942 __ Bind(&true_label);
1943 __ movl(reg, Immediate(1));
1944 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001945}
1946
1947void LocationsBuilderX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001948 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001949}
1950
1951void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001952 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001953}
1954
1955void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001956 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001957}
1958
1959void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001960 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001961}
1962
1963void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001964 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001965}
1966
1967void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001968 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001969}
1970
1971void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001972 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001973}
1974
1975void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001976 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001977}
1978
1979void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001980 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001981}
1982
1983void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001984 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001985}
1986
1987void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001988 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001989}
1990
1991void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001992 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001993}
1994
Aart Bike9f37602015-10-09 11:15:55 -07001995void LocationsBuilderX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001996 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001997}
1998
1999void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002000 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002001}
2002
2003void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002004 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002005}
2006
2007void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002008 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002009}
2010
2011void LocationsBuilderX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002012 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002013}
2014
2015void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002016 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002017}
2018
2019void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002020 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002021}
2022
2023void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002024 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002025}
2026
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002027void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002028 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002029 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002030 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002031}
2032
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002033void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002034 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002035}
2036
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002037void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
2038 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002039 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002040 locations->SetOut(Location::ConstantLocation(constant));
2041}
2042
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002043void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002044 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002045}
2046
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002047void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002048 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002049 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002050 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002051}
2052
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002053void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002054 // Will be generated at use site.
2055}
2056
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002057void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
2058 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002059 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002060 locations->SetOut(Location::ConstantLocation(constant));
2061}
2062
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002063void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002064 // Will be generated at use site.
2065}
2066
2067void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
2068 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002069 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002070 locations->SetOut(Location::ConstantLocation(constant));
2071}
2072
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002073void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002074 // Will be generated at use site.
2075}
2076
Igor Murashkind01745e2017-04-05 16:40:31 -07002077void LocationsBuilderX86::VisitConstructorFence(HConstructorFence* constructor_fence) {
2078 constructor_fence->SetLocations(nullptr);
2079}
2080
2081void InstructionCodeGeneratorX86::VisitConstructorFence(
2082 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
2083 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
2084}
2085
Calin Juravle27df7582015-04-17 19:12:31 +01002086void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2087 memory_barrier->SetLocations(nullptr);
2088}
2089
2090void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00002091 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002092}
2093
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002094void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002095 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002096}
2097
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002098void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002099 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002100}
2101
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002102void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002103 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002104 new (GetGraph()->GetAllocator()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002105 switch (ret->InputAt(0)->GetType()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002106 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002107 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002108 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002109 case DataType::Type::kInt8:
2110 case DataType::Type::kUint16:
2111 case DataType::Type::kInt16:
2112 case DataType::Type::kInt32:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002113 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002114 break;
2115
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002116 case DataType::Type::kInt64:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002117 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002118 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002119 break;
2120
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002121 case DataType::Type::kFloat32:
2122 case DataType::Type::kFloat64:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002123 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002124 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002125 break;
2126
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002127 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002128 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002129 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002130}
2131
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002132void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002133 if (kIsDebugBuild) {
2134 switch (ret->InputAt(0)->GetType()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002135 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002136 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002137 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002138 case DataType::Type::kInt8:
2139 case DataType::Type::kUint16:
2140 case DataType::Type::kInt16:
2141 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002142 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002143 break;
2144
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002145 case DataType::Type::kInt64:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002146 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
2147 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002148 break;
2149
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002150 case DataType::Type::kFloat32:
2151 case DataType::Type::kFloat64:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002152 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002153 break;
2154
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002155 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002156 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002157 }
2158 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002159 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002160}
2161
Calin Juravle175dc732015-08-25 15:42:32 +01002162void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2163 // The trampoline uses the same calling convention as dex calling conventions,
2164 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2165 // the method_idx.
2166 HandleInvoke(invoke);
2167}
2168
2169void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2170 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2171}
2172
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002173void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002174 // Explicit clinit checks triggered by static invokes must have been pruned by
2175 // art::PrepareForRegisterAllocation.
2176 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002177
Mark Mendellfb8d2792015-03-31 22:16:59 -04002178 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002179 if (intrinsic.TryDispatch(invoke)) {
Vladimir Marko65979462017-05-19 17:25:12 +01002180 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeMethodLoadKind()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00002181 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002182 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04002183 return;
2184 }
2185
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002186 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002187
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002188 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
Vladimir Marko65979462017-05-19 17:25:12 +01002189 if (invoke->HasPcRelativeMethodLoadKind()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00002190 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002191 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002192}
2193
Mark Mendell09ed1a32015-03-25 08:30:06 -04002194static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
2195 if (invoke->GetLocations()->Intrinsified()) {
2196 IntrinsicCodeGeneratorX86 intrinsic(codegen);
2197 intrinsic.Dispatch(invoke);
2198 return true;
2199 }
2200 return false;
2201}
2202
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002203void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002204 // Explicit clinit checks triggered by static invokes must have been pruned by
2205 // art::PrepareForRegisterAllocation.
2206 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002207
Mark Mendell09ed1a32015-03-25 08:30:06 -04002208 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2209 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002210 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002211
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002212 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04002213 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002214 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002215}
2216
2217void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00002218 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
2219 if (intrinsic.TryDispatch(invoke)) {
2220 return;
2221 }
2222
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002223 HandleInvoke(invoke);
2224}
2225
2226void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002227 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002228 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002229}
2230
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002231void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002232 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2233 return;
2234 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002235
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002236 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002237 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002238}
2239
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002240void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002241 // This call to HandleInvoke allocates a temporary (core) register
2242 // which is also used to transfer the hidden argument from FP to
2243 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002244 HandleInvoke(invoke);
2245 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002246 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002247}
2248
2249void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
2250 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002251 LocationSummary* locations = invoke->GetLocations();
2252 Register temp = locations->GetTemp(0).AsRegister<Register>();
2253 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002254 Location receiver = locations->InAt(0);
2255 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2256
Roland Levillain0d5a2812015-11-13 10:07:31 +00002257 // Set the hidden argument. This is safe to do this here, as XMM7
2258 // won't be modified thereafter, before the `call` instruction.
2259 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002260 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002261 __ movd(hidden_reg, temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002262
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002263 if (receiver.IsStackSlot()) {
2264 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002265 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002266 __ movl(temp, Address(temp, class_offset));
2267 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002268 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002269 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002270 }
Roland Levillain4d027112015-07-01 15:41:14 +01002271 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002272 // Instead of simply (possibly) unpoisoning `temp` here, we should
2273 // emit a read barrier for the previous class reference load.
2274 // However this is not required in practice, as this is an
2275 // intermediate/temporary reference and because the current
2276 // concurrent copying collector keeps the from-space memory
2277 // intact/accessible until the end of the marking phase (the
2278 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002279 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002280 // temp = temp->GetAddressOfIMT()
2281 __ movl(temp,
2282 Address(temp, mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002283 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002284 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002285 invoke->GetImtIndex(), kX86PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002286 __ movl(temp, Address(temp, method_offset));
2287 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002288 __ call(Address(temp,
Andreas Gampe542451c2016-07-26 09:02:02 -07002289 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002290
2291 DCHECK(!codegen_->IsLeafMethod());
2292 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2293}
2294
Orion Hodsonac141392017-01-13 11:53:47 +00002295void LocationsBuilderX86::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2296 HandleInvoke(invoke);
2297}
2298
2299void InstructionCodeGeneratorX86::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2300 codegen_->GenerateInvokePolymorphicCall(invoke);
2301}
2302
Roland Levillain88cb1752014-10-20 16:36:47 +01002303void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2304 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002305 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Roland Levillain88cb1752014-10-20 16:36:47 +01002306 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002307 case DataType::Type::kInt32:
2308 case DataType::Type::kInt64:
Roland Levillain88cb1752014-10-20 16:36:47 +01002309 locations->SetInAt(0, Location::RequiresRegister());
2310 locations->SetOut(Location::SameAsFirstInput());
2311 break;
2312
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002313 case DataType::Type::kFloat32:
Roland Levillain5368c212014-11-27 15:03:41 +00002314 locations->SetInAt(0, Location::RequiresFpuRegister());
2315 locations->SetOut(Location::SameAsFirstInput());
2316 locations->AddTemp(Location::RequiresRegister());
2317 locations->AddTemp(Location::RequiresFpuRegister());
2318 break;
2319
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002320 case DataType::Type::kFloat64:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002321 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002322 locations->SetOut(Location::SameAsFirstInput());
2323 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002324 break;
2325
2326 default:
2327 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2328 }
2329}
2330
2331void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2332 LocationSummary* locations = neg->GetLocations();
2333 Location out = locations->Out();
2334 Location in = locations->InAt(0);
2335 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002336 case DataType::Type::kInt32:
Roland Levillain88cb1752014-10-20 16:36:47 +01002337 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002338 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002339 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002340 break;
2341
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002342 case DataType::Type::kInt64:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002343 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002344 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002345 __ negl(out.AsRegisterPairLow<Register>());
2346 // Negation is similar to subtraction from zero. The least
2347 // significant byte triggers a borrow when it is different from
2348 // zero; to take it into account, add 1 to the most significant
2349 // byte if the carry flag (CF) is set to 1 after the first NEGL
2350 // operation.
2351 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2352 __ negl(out.AsRegisterPairHigh<Register>());
2353 break;
2354
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002355 case DataType::Type::kFloat32: {
Roland Levillain5368c212014-11-27 15:03:41 +00002356 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002357 Register constant = locations->GetTemp(0).AsRegister<Register>();
2358 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002359 // Implement float negation with an exclusive or with value
2360 // 0x80000000 (mask for bit 31, representing the sign of a
2361 // single-precision floating-point number).
2362 __ movl(constant, Immediate(INT32_C(0x80000000)));
2363 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002364 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002365 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002366 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002367
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002368 case DataType::Type::kFloat64: {
Roland Levillain5368c212014-11-27 15:03:41 +00002369 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002370 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002371 // Implement double negation with an exclusive or with value
2372 // 0x8000000000000000 (mask for bit 63, representing the sign of
2373 // a double-precision floating-point number).
2374 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002375 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002376 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002377 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002378
2379 default:
2380 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2381 }
2382}
2383
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002384void LocationsBuilderX86::VisitX86FPNeg(HX86FPNeg* neg) {
2385 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002386 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002387 DCHECK(DataType::IsFloatingPointType(neg->GetType()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002388 locations->SetInAt(0, Location::RequiresFpuRegister());
2389 locations->SetInAt(1, Location::RequiresRegister());
2390 locations->SetOut(Location::SameAsFirstInput());
2391 locations->AddTemp(Location::RequiresFpuRegister());
2392}
2393
2394void InstructionCodeGeneratorX86::VisitX86FPNeg(HX86FPNeg* neg) {
2395 LocationSummary* locations = neg->GetLocations();
2396 Location out = locations->Out();
2397 DCHECK(locations->InAt(0).Equals(out));
2398
2399 Register constant_area = locations->InAt(1).AsRegister<Register>();
2400 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002401 if (neg->GetType() == DataType::Type::kFloat32) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00002402 __ movss(mask, codegen_->LiteralInt32Address(INT32_C(0x80000000),
2403 neg->GetBaseMethodAddress(),
2404 constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002405 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
2406 } else {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00002407 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000),
2408 neg->GetBaseMethodAddress(),
2409 constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002410 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
2411 }
2412}
2413
Roland Levillaindff1f282014-11-05 14:15:05 +00002414void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002415 DataType::Type result_type = conversion->GetResultType();
2416 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002417 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
2418 << input_type << " -> " << result_type;
Roland Levillain624279f2014-12-04 11:54:28 +00002419
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002420 // The float-to-long and double-to-long type conversions rely on a
2421 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002422 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002423 ((input_type == DataType::Type::kFloat32 || input_type == DataType::Type::kFloat64)
2424 && result_type == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002425 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002426 : LocationSummary::kNoCall;
2427 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002428 new (GetGraph()->GetAllocator()) LocationSummary(conversion, call_kind);
Roland Levillain624279f2014-12-04 11:54:28 +00002429
Roland Levillaindff1f282014-11-05 14:15:05 +00002430 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002431 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002432 case DataType::Type::kInt8:
Roland Levillain51d3fc42014-11-13 14:11:42 +00002433 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002434 case DataType::Type::kUint8:
2435 case DataType::Type::kInt8:
2436 case DataType::Type::kUint16:
2437 case DataType::Type::kInt16:
2438 case DataType::Type::kInt32:
2439 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2440 // Make the output overlap to please the register allocator. This greatly simplifies
2441 // the validation of the linear scan implementation
2442 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2443 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002444 case DataType::Type::kInt64: {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002445 HInstruction* input = conversion->InputAt(0);
2446 Location input_location = input->IsConstant()
2447 ? Location::ConstantLocation(input->AsConstant())
2448 : Location::RegisterPairLocation(EAX, EDX);
2449 locations->SetInAt(0, input_location);
2450 // Make the output overlap to please the register allocator. This greatly simplifies
2451 // the validation of the linear scan implementation
2452 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2453 break;
2454 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002455
2456 default:
2457 LOG(FATAL) << "Unexpected type conversion from " << input_type
2458 << " to " << result_type;
2459 }
2460 break;
2461
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002462 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002463 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002464 DCHECK(DataType::IsIntegralType(input_type)) << input_type;
2465 locations->SetInAt(0, Location::Any());
2466 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Roland Levillain01a8d712014-11-14 16:27:39 +00002467 break;
2468
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002469 case DataType::Type::kInt32:
Roland Levillain946e1432014-11-11 17:35:19 +00002470 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002471 case DataType::Type::kInt64:
Roland Levillain946e1432014-11-11 17:35:19 +00002472 locations->SetInAt(0, Location::Any());
2473 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2474 break;
2475
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002476 case DataType::Type::kFloat32:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002477 locations->SetInAt(0, Location::RequiresFpuRegister());
2478 locations->SetOut(Location::RequiresRegister());
2479 locations->AddTemp(Location::RequiresFpuRegister());
2480 break;
2481
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002482 case DataType::Type::kFloat64:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002483 locations->SetInAt(0, Location::RequiresFpuRegister());
2484 locations->SetOut(Location::RequiresRegister());
2485 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002486 break;
2487
2488 default:
2489 LOG(FATAL) << "Unexpected type conversion from " << input_type
2490 << " to " << result_type;
2491 }
2492 break;
2493
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002494 case DataType::Type::kInt64:
Roland Levillaindff1f282014-11-05 14:15:05 +00002495 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002496 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002497 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002498 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002499 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002500 case DataType::Type::kInt16:
2501 case DataType::Type::kInt32:
Roland Levillaindff1f282014-11-05 14:15:05 +00002502 locations->SetInAt(0, Location::RegisterLocation(EAX));
2503 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2504 break;
2505
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002506 case DataType::Type::kFloat32:
2507 case DataType::Type::kFloat64: {
Vladimir Marko949c91f2015-01-27 10:48:44 +00002508 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002509 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2510 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2511
Vladimir Marko949c91f2015-01-27 10:48:44 +00002512 // The runtime helper puts the result in EAX, EDX.
2513 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002514 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002515 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002516
2517 default:
2518 LOG(FATAL) << "Unexpected type conversion from " << input_type
2519 << " to " << result_type;
2520 }
2521 break;
2522
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002523 case DataType::Type::kFloat32:
Roland Levillaincff13742014-11-17 14:32:17 +00002524 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002525 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002526 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002527 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002528 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002529 case DataType::Type::kInt16:
2530 case DataType::Type::kInt32:
Roland Levillaincff13742014-11-17 14:32:17 +00002531 locations->SetInAt(0, Location::RequiresRegister());
2532 locations->SetOut(Location::RequiresFpuRegister());
2533 break;
2534
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002535 case DataType::Type::kInt64:
Roland Levillain232ade02015-04-20 15:14:36 +01002536 locations->SetInAt(0, Location::Any());
2537 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002538 break;
2539
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002540 case DataType::Type::kFloat64:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002541 locations->SetInAt(0, Location::RequiresFpuRegister());
2542 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002543 break;
2544
2545 default:
2546 LOG(FATAL) << "Unexpected type conversion from " << input_type
2547 << " to " << result_type;
2548 };
2549 break;
2550
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002551 case DataType::Type::kFloat64:
Roland Levillaincff13742014-11-17 14:32:17 +00002552 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002553 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002554 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002555 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002556 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002557 case DataType::Type::kInt16:
2558 case DataType::Type::kInt32:
Roland Levillaincff13742014-11-17 14:32:17 +00002559 locations->SetInAt(0, Location::RequiresRegister());
2560 locations->SetOut(Location::RequiresFpuRegister());
2561 break;
2562
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002563 case DataType::Type::kInt64:
Roland Levillain232ade02015-04-20 15:14:36 +01002564 locations->SetInAt(0, Location::Any());
2565 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002566 break;
2567
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002568 case DataType::Type::kFloat32:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002569 locations->SetInAt(0, Location::RequiresFpuRegister());
2570 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002571 break;
2572
2573 default:
2574 LOG(FATAL) << "Unexpected type conversion from " << input_type
2575 << " to " << result_type;
2576 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002577 break;
2578
2579 default:
2580 LOG(FATAL) << "Unexpected type conversion from " << input_type
2581 << " to " << result_type;
2582 }
2583}
2584
2585void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2586 LocationSummary* locations = conversion->GetLocations();
2587 Location out = locations->Out();
2588 Location in = locations->InAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002589 DataType::Type result_type = conversion->GetResultType();
2590 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002591 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
2592 << input_type << " -> " << result_type;
Roland Levillaindff1f282014-11-05 14:15:05 +00002593 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002594 case DataType::Type::kUint8:
2595 switch (input_type) {
2596 case DataType::Type::kInt8:
2597 case DataType::Type::kUint16:
2598 case DataType::Type::kInt16:
2599 case DataType::Type::kInt32:
2600 if (in.IsRegister()) {
2601 __ movzxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
2602 } else {
2603 DCHECK(in.GetConstant()->IsIntConstant());
2604 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2605 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint8_t>(value)));
2606 }
2607 break;
2608 case DataType::Type::kInt64:
2609 if (in.IsRegisterPair()) {
2610 __ movzxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2611 } else {
2612 DCHECK(in.GetConstant()->IsLongConstant());
2613 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2614 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint8_t>(value)));
2615 }
2616 break;
2617
2618 default:
2619 LOG(FATAL) << "Unexpected type conversion from " << input_type
2620 << " to " << result_type;
2621 }
2622 break;
2623
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002624 case DataType::Type::kInt8:
Roland Levillain51d3fc42014-11-13 14:11:42 +00002625 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002626 case DataType::Type::kUint8:
2627 case DataType::Type::kUint16:
2628 case DataType::Type::kInt16:
2629 case DataType::Type::kInt32:
2630 if (in.IsRegister()) {
2631 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
2632 } else {
2633 DCHECK(in.GetConstant()->IsIntConstant());
2634 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2635 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2636 }
2637 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002638 case DataType::Type::kInt64:
Vladimir Markob52bbde2016-02-12 12:06:05 +00002639 if (in.IsRegisterPair()) {
2640 __ movsxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2641 } else {
2642 DCHECK(in.GetConstant()->IsLongConstant());
2643 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2644 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2645 }
2646 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002647
2648 default:
2649 LOG(FATAL) << "Unexpected type conversion from " << input_type
2650 << " to " << result_type;
2651 }
2652 break;
2653
2654 case DataType::Type::kUint16:
2655 switch (input_type) {
2656 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002657 case DataType::Type::kInt16:
2658 case DataType::Type::kInt32:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002659 if (in.IsRegister()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002660 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
2661 } else if (in.IsStackSlot()) {
2662 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002663 } else {
2664 DCHECK(in.GetConstant()->IsIntConstant());
2665 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002666 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
2667 }
2668 break;
2669 case DataType::Type::kInt64:
2670 if (in.IsRegisterPair()) {
2671 __ movzxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2672 } else if (in.IsDoubleStackSlot()) {
2673 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2674 } else {
2675 DCHECK(in.GetConstant()->IsLongConstant());
2676 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2677 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002678 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002679 break;
2680
2681 default:
2682 LOG(FATAL) << "Unexpected type conversion from " << input_type
2683 << " to " << result_type;
2684 }
2685 break;
2686
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002687 case DataType::Type::kInt16:
Roland Levillain01a8d712014-11-14 16:27:39 +00002688 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002689 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002690 case DataType::Type::kInt32:
Roland Levillain01a8d712014-11-14 16:27:39 +00002691 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002692 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002693 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002694 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002695 } else {
2696 DCHECK(in.GetConstant()->IsIntConstant());
2697 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002698 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002699 }
2700 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002701 case DataType::Type::kInt64:
2702 if (in.IsRegisterPair()) {
2703 __ movsxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2704 } else if (in.IsDoubleStackSlot()) {
2705 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2706 } else {
2707 DCHECK(in.GetConstant()->IsLongConstant());
2708 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2709 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
2710 }
2711 break;
Roland Levillain01a8d712014-11-14 16:27:39 +00002712
2713 default:
2714 LOG(FATAL) << "Unexpected type conversion from " << input_type
2715 << " to " << result_type;
2716 }
2717 break;
2718
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002719 case DataType::Type::kInt32:
Roland Levillain946e1432014-11-11 17:35:19 +00002720 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002721 case DataType::Type::kInt64:
Roland Levillain946e1432014-11-11 17:35:19 +00002722 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002723 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002724 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002725 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002726 } else {
2727 DCHECK(in.IsConstant());
2728 DCHECK(in.GetConstant()->IsLongConstant());
2729 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002730 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002731 }
2732 break;
2733
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002734 case DataType::Type::kFloat32: {
Roland Levillain3f8f9362014-12-02 17:45:01 +00002735 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2736 Register output = out.AsRegister<Register>();
2737 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002738 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002739
2740 __ movl(output, Immediate(kPrimIntMax));
2741 // temp = int-to-float(output)
2742 __ cvtsi2ss(temp, output);
2743 // if input >= temp goto done
2744 __ comiss(input, temp);
2745 __ j(kAboveEqual, &done);
2746 // if input == NaN goto nan
2747 __ j(kUnordered, &nan);
2748 // output = float-to-int-truncate(input)
2749 __ cvttss2si(output, input);
2750 __ jmp(&done);
2751 __ Bind(&nan);
2752 // output = 0
2753 __ xorl(output, output);
2754 __ Bind(&done);
2755 break;
2756 }
2757
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002758 case DataType::Type::kFloat64: {
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002759 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2760 Register output = out.AsRegister<Register>();
2761 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002762 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002763
2764 __ movl(output, Immediate(kPrimIntMax));
2765 // temp = int-to-double(output)
2766 __ cvtsi2sd(temp, output);
2767 // if input >= temp goto done
2768 __ comisd(input, temp);
2769 __ j(kAboveEqual, &done);
2770 // if input == NaN goto nan
2771 __ j(kUnordered, &nan);
2772 // output = double-to-int-truncate(input)
2773 __ cvttsd2si(output, input);
2774 __ jmp(&done);
2775 __ Bind(&nan);
2776 // output = 0
2777 __ xorl(output, output);
2778 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002779 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002780 }
Roland Levillain946e1432014-11-11 17:35:19 +00002781
2782 default:
2783 LOG(FATAL) << "Unexpected type conversion from " << input_type
2784 << " to " << result_type;
2785 }
2786 break;
2787
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002788 case DataType::Type::kInt64:
Roland Levillaindff1f282014-11-05 14:15:05 +00002789 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002790 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002791 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002792 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002793 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002794 case DataType::Type::kInt16:
2795 case DataType::Type::kInt32:
Roland Levillaindff1f282014-11-05 14:15:05 +00002796 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2797 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002798 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002799 __ cdq();
2800 break;
2801
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002802 case DataType::Type::kFloat32:
Serban Constantinescuba45db02016-07-12 22:53:02 +01002803 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002804 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002805 break;
2806
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002807 case DataType::Type::kFloat64:
Serban Constantinescuba45db02016-07-12 22:53:02 +01002808 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002809 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002810 break;
2811
2812 default:
2813 LOG(FATAL) << "Unexpected type conversion from " << input_type
2814 << " to " << result_type;
2815 }
2816 break;
2817
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002818 case DataType::Type::kFloat32:
Roland Levillaincff13742014-11-17 14:32:17 +00002819 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002820 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002821 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002822 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002823 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002824 case DataType::Type::kInt16:
2825 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002826 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002827 break;
2828
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002829 case DataType::Type::kInt64: {
Roland Levillain232ade02015-04-20 15:14:36 +01002830 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002831
Roland Levillain232ade02015-04-20 15:14:36 +01002832 // Create stack space for the call to
2833 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2834 // TODO: enhance register allocator to ask for stack temporaries.
2835 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002836 adjustment = DataType::Size(DataType::Type::kInt64);
Roland Levillain232ade02015-04-20 15:14:36 +01002837 __ subl(ESP, Immediate(adjustment));
2838 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002839
Roland Levillain232ade02015-04-20 15:14:36 +01002840 // Load the value to the FP stack, using temporaries if needed.
2841 PushOntoFPStack(in, 0, adjustment, false, true);
2842
2843 if (out.IsStackSlot()) {
2844 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2845 } else {
2846 __ fstps(Address(ESP, 0));
2847 Location stack_temp = Location::StackSlot(0);
2848 codegen_->Move32(out, stack_temp);
2849 }
2850
2851 // Remove the temporary stack space we allocated.
2852 if (adjustment != 0) {
2853 __ addl(ESP, Immediate(adjustment));
2854 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002855 break;
2856 }
2857
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002858 case DataType::Type::kFloat64:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002859 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002860 break;
2861
2862 default:
2863 LOG(FATAL) << "Unexpected type conversion from " << input_type
2864 << " to " << result_type;
2865 };
2866 break;
2867
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002868 case DataType::Type::kFloat64:
Roland Levillaincff13742014-11-17 14:32:17 +00002869 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002870 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002871 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002872 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002873 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002874 case DataType::Type::kInt16:
2875 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002876 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002877 break;
2878
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002879 case DataType::Type::kInt64: {
Roland Levillain232ade02015-04-20 15:14:36 +01002880 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002881
Roland Levillain232ade02015-04-20 15:14:36 +01002882 // Create stack space for the call to
2883 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2884 // TODO: enhance register allocator to ask for stack temporaries.
2885 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002886 adjustment = DataType::Size(DataType::Type::kInt64);
Roland Levillain232ade02015-04-20 15:14:36 +01002887 __ subl(ESP, Immediate(adjustment));
2888 }
2889
2890 // Load the value to the FP stack, using temporaries if needed.
2891 PushOntoFPStack(in, 0, adjustment, false, true);
2892
2893 if (out.IsDoubleStackSlot()) {
2894 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2895 } else {
2896 __ fstpl(Address(ESP, 0));
2897 Location stack_temp = Location::DoubleStackSlot(0);
2898 codegen_->Move64(out, stack_temp);
2899 }
2900
2901 // Remove the temporary stack space we allocated.
2902 if (adjustment != 0) {
2903 __ addl(ESP, Immediate(adjustment));
2904 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002905 break;
2906 }
2907
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002908 case DataType::Type::kFloat32:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002909 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002910 break;
2911
2912 default:
2913 LOG(FATAL) << "Unexpected type conversion from " << input_type
2914 << " to " << result_type;
2915 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002916 break;
2917
2918 default:
2919 LOG(FATAL) << "Unexpected type conversion from " << input_type
2920 << " to " << result_type;
2921 }
2922}
2923
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002924void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002925 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002926 new (GetGraph()->GetAllocator()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002927 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002928 case DataType::Type::kInt32: {
Mark Mendell09b84632015-02-13 17:48:38 -05002929 locations->SetInAt(0, Location::RequiresRegister());
2930 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2931 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2932 break;
2933 }
2934
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002935 case DataType::Type::kInt64: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002936 locations->SetInAt(0, Location::RequiresRegister());
2937 locations->SetInAt(1, Location::Any());
2938 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002939 break;
2940 }
2941
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002942 case DataType::Type::kFloat32:
2943 case DataType::Type::kFloat64: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002944 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002945 if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2946 DCHECK(add->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002947 } else if (add->InputAt(1)->IsConstant()) {
2948 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002949 } else {
2950 locations->SetInAt(1, Location::Any());
2951 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002952 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002953 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002954 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002955
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002956 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002957 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2958 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002959 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002960}
2961
2962void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2963 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002964 Location first = locations->InAt(0);
2965 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002966 Location out = locations->Out();
2967
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002968 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002969 case DataType::Type::kInt32: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002970 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002971 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2972 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002973 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2974 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05002975 } else {
2976 __ leal(out.AsRegister<Register>(), Address(
2977 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
2978 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002979 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002980 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
2981 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2982 __ addl(out.AsRegister<Register>(), Immediate(value));
2983 } else {
2984 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
2985 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002986 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05002987 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002988 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002989 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002990 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002991 }
2992
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002993 case DataType::Type::kInt64: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002994 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002995 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2996 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002997 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002998 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
2999 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003000 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003001 } else {
3002 DCHECK(second.IsConstant()) << second;
3003 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3004 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3005 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003006 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003007 break;
3008 }
3009
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003010 case DataType::Type::kFloat32: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003011 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003012 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04003013 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3014 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003015 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003016 __ addss(first.AsFpuRegister<XmmRegister>(),
3017 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003018 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3019 const_area->GetBaseMethodAddress(),
3020 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003021 } else {
3022 DCHECK(second.IsStackSlot());
3023 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003024 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003025 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003026 }
3027
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003028 case DataType::Type::kFloat64: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003029 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003030 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04003031 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3032 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003033 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003034 __ addsd(first.AsFpuRegister<XmmRegister>(),
3035 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003036 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3037 const_area->GetBaseMethodAddress(),
3038 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003039 } else {
3040 DCHECK(second.IsDoubleStackSlot());
3041 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003042 }
3043 break;
3044 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003045
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003046 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003047 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003048 }
3049}
3050
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003051void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003052 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003053 new (GetGraph()->GetAllocator()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003054 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003055 case DataType::Type::kInt32:
3056 case DataType::Type::kInt64: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003057 locations->SetInAt(0, Location::RequiresRegister());
3058 locations->SetInAt(1, Location::Any());
3059 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003060 break;
3061 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003062 case DataType::Type::kFloat32:
3063 case DataType::Type::kFloat64: {
Calin Juravle11351682014-10-23 15:38:15 +01003064 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003065 if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3066 DCHECK(sub->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003067 } else if (sub->InputAt(1)->IsConstant()) {
3068 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003069 } else {
3070 locations->SetInAt(1, Location::Any());
3071 }
Calin Juravle11351682014-10-23 15:38:15 +01003072 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003073 break;
Calin Juravle11351682014-10-23 15:38:15 +01003074 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003075
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003076 default:
Calin Juravle11351682014-10-23 15:38:15 +01003077 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003078 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003079}
3080
3081void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
3082 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003083 Location first = locations->InAt(0);
3084 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01003085 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003086 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003087 case DataType::Type::kInt32: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003088 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003089 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003090 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003091 __ subl(first.AsRegister<Register>(),
3092 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003093 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003094 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003095 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003096 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003097 }
3098
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003099 case DataType::Type::kInt64: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003100 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01003101 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3102 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003103 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01003104 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003105 __ sbbl(first.AsRegisterPairHigh<Register>(),
3106 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003107 } else {
3108 DCHECK(second.IsConstant()) << second;
3109 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3110 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3111 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003112 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003113 break;
3114 }
3115
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003116 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003117 if (second.IsFpuRegister()) {
3118 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3119 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3120 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003121 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003122 __ subss(first.AsFpuRegister<XmmRegister>(),
3123 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003124 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3125 const_area->GetBaseMethodAddress(),
3126 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003127 } else {
3128 DCHECK(second.IsStackSlot());
3129 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3130 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003131 break;
Calin Juravle11351682014-10-23 15:38:15 +01003132 }
3133
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003134 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003135 if (second.IsFpuRegister()) {
3136 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3137 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3138 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003139 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003140 __ subsd(first.AsFpuRegister<XmmRegister>(),
3141 codegen_->LiteralDoubleAddress(
3142 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003143 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003144 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3145 } else {
3146 DCHECK(second.IsDoubleStackSlot());
3147 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3148 }
Calin Juravle11351682014-10-23 15:38:15 +01003149 break;
3150 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003151
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003152 default:
Calin Juravle11351682014-10-23 15:38:15 +01003153 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003154 }
3155}
3156
Calin Juravle34bacdf2014-10-07 20:23:36 +01003157void LocationsBuilderX86::VisitMul(HMul* mul) {
3158 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003159 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Calin Juravle34bacdf2014-10-07 20:23:36 +01003160 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003161 case DataType::Type::kInt32:
Calin Juravle34bacdf2014-10-07 20:23:36 +01003162 locations->SetInAt(0, Location::RequiresRegister());
3163 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003164 if (mul->InputAt(1)->IsIntConstant()) {
3165 // Can use 3 operand multiply.
3166 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3167 } else {
3168 locations->SetOut(Location::SameAsFirstInput());
3169 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003170 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003171 case DataType::Type::kInt64: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003172 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003173 locations->SetInAt(1, Location::Any());
3174 locations->SetOut(Location::SameAsFirstInput());
3175 // Needed for imul on 32bits with 64bits output.
3176 locations->AddTemp(Location::RegisterLocation(EAX));
3177 locations->AddTemp(Location::RegisterLocation(EDX));
3178 break;
3179 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003180 case DataType::Type::kFloat32:
3181 case DataType::Type::kFloat64: {
Calin Juravleb5bfa962014-10-21 18:02:24 +01003182 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003183 if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3184 DCHECK(mul->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003185 } else if (mul->InputAt(1)->IsConstant()) {
3186 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003187 } else {
3188 locations->SetInAt(1, Location::Any());
3189 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003190 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003191 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003192 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003193
3194 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003195 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003196 }
3197}
3198
3199void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
3200 LocationSummary* locations = mul->GetLocations();
3201 Location first = locations->InAt(0);
3202 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003203 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003204
3205 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003206 case DataType::Type::kInt32:
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003207 // The constant may have ended up in a register, so test explicitly to avoid
3208 // problems where the output may not be the same as the first operand.
3209 if (mul->InputAt(1)->IsIntConstant()) {
3210 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3211 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
3212 } else if (second.IsRegister()) {
3213 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003214 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003215 } else {
3216 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003217 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003218 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003219 }
3220 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003221
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003222 case DataType::Type::kInt64: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003223 Register in1_hi = first.AsRegisterPairHigh<Register>();
3224 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003225 Register eax = locations->GetTemp(0).AsRegister<Register>();
3226 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003227
3228 DCHECK_EQ(EAX, eax);
3229 DCHECK_EQ(EDX, edx);
3230
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003231 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01003232 // output: in1
3233 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3234 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3235 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003236 if (second.IsConstant()) {
3237 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003238
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003239 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3240 int32_t low_value = Low32Bits(value);
3241 int32_t high_value = High32Bits(value);
3242 Immediate low(low_value);
3243 Immediate high(high_value);
3244
3245 __ movl(eax, high);
3246 // eax <- in1.lo * in2.hi
3247 __ imull(eax, in1_lo);
3248 // in1.hi <- in1.hi * in2.lo
3249 __ imull(in1_hi, low);
3250 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3251 __ addl(in1_hi, eax);
3252 // move in2_lo to eax to prepare for double precision
3253 __ movl(eax, low);
3254 // edx:eax <- in1.lo * in2.lo
3255 __ mull(in1_lo);
3256 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3257 __ addl(in1_hi, edx);
3258 // in1.lo <- (in1.lo * in2.lo)[31:0];
3259 __ movl(in1_lo, eax);
3260 } else if (second.IsRegisterPair()) {
3261 Register in2_hi = second.AsRegisterPairHigh<Register>();
3262 Register in2_lo = second.AsRegisterPairLow<Register>();
3263
3264 __ movl(eax, in2_hi);
3265 // eax <- in1.lo * in2.hi
3266 __ imull(eax, in1_lo);
3267 // in1.hi <- in1.hi * in2.lo
3268 __ imull(in1_hi, in2_lo);
3269 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3270 __ addl(in1_hi, eax);
3271 // move in1_lo to eax to prepare for double precision
3272 __ movl(eax, in1_lo);
3273 // edx:eax <- in1.lo * in2.lo
3274 __ mull(in2_lo);
3275 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3276 __ addl(in1_hi, edx);
3277 // in1.lo <- (in1.lo * in2.lo)[31:0];
3278 __ movl(in1_lo, eax);
3279 } else {
3280 DCHECK(second.IsDoubleStackSlot()) << second;
3281 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
3282 Address in2_lo(ESP, second.GetStackIndex());
3283
3284 __ movl(eax, in2_hi);
3285 // eax <- in1.lo * in2.hi
3286 __ imull(eax, in1_lo);
3287 // in1.hi <- in1.hi * in2.lo
3288 __ imull(in1_hi, in2_lo);
3289 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3290 __ addl(in1_hi, eax);
3291 // move in1_lo to eax to prepare for double precision
3292 __ movl(eax, in1_lo);
3293 // edx:eax <- in1.lo * in2.lo
3294 __ mull(in2_lo);
3295 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3296 __ addl(in1_hi, edx);
3297 // in1.lo <- (in1.lo * in2.lo)[31:0];
3298 __ movl(in1_lo, eax);
3299 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003300
3301 break;
3302 }
3303
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003304 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003305 DCHECK(first.Equals(locations->Out()));
3306 if (second.IsFpuRegister()) {
3307 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3308 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3309 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003310 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003311 __ mulss(first.AsFpuRegister<XmmRegister>(),
3312 codegen_->LiteralFloatAddress(
3313 const_area->GetConstant()->AsFloatConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003314 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003315 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3316 } else {
3317 DCHECK(second.IsStackSlot());
3318 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3319 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003320 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003321 }
3322
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003323 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003324 DCHECK(first.Equals(locations->Out()));
3325 if (second.IsFpuRegister()) {
3326 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3327 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3328 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003329 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003330 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3331 codegen_->LiteralDoubleAddress(
3332 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003333 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003334 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3335 } else {
3336 DCHECK(second.IsDoubleStackSlot());
3337 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3338 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003339 break;
3340 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003341
3342 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003343 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003344 }
3345}
3346
Roland Levillain232ade02015-04-20 15:14:36 +01003347void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3348 uint32_t temp_offset,
3349 uint32_t stack_adjustment,
3350 bool is_fp,
3351 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003352 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003353 DCHECK(!is_wide);
3354 if (is_fp) {
3355 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3356 } else {
3357 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3358 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003359 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003360 DCHECK(is_wide);
3361 if (is_fp) {
3362 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3363 } else {
3364 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3365 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003366 } else {
3367 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003368 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003369 Location stack_temp = Location::StackSlot(temp_offset);
3370 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003371 if (is_fp) {
3372 __ flds(Address(ESP, temp_offset));
3373 } else {
3374 __ filds(Address(ESP, temp_offset));
3375 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003376 } else {
3377 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3378 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003379 if (is_fp) {
3380 __ fldl(Address(ESP, temp_offset));
3381 } else {
3382 __ fildl(Address(ESP, temp_offset));
3383 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003384 }
3385 }
3386}
3387
3388void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003389 DataType::Type type = rem->GetResultType();
3390 bool is_float = type == DataType::Type::kFloat32;
3391 size_t elem_size = DataType::Size(type);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003392 LocationSummary* locations = rem->GetLocations();
3393 Location first = locations->InAt(0);
3394 Location second = locations->InAt(1);
3395 Location out = locations->Out();
3396
3397 // Create stack space for 2 elements.
3398 // TODO: enhance register allocator to ask for stack temporaries.
3399 __ subl(ESP, Immediate(2 * elem_size));
3400
3401 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003402 const bool is_wide = !is_float;
3403 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
3404 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003405
3406 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003407 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003408 __ Bind(&retry);
3409 __ fprem();
3410
3411 // Move FP status to AX.
3412 __ fstsw();
3413
3414 // And see if the argument reduction is complete. This is signaled by the
3415 // C2 FPU flag bit set to 0.
3416 __ andl(EAX, Immediate(kC2ConditionMask));
3417 __ j(kNotEqual, &retry);
3418
3419 // We have settled on the final value. Retrieve it into an XMM register.
3420 // Store FP top of stack to real stack.
3421 if (is_float) {
3422 __ fsts(Address(ESP, 0));
3423 } else {
3424 __ fstl(Address(ESP, 0));
3425 }
3426
3427 // Pop the 2 items from the FP stack.
3428 __ fucompp();
3429
3430 // Load the value from the stack into an XMM register.
3431 DCHECK(out.IsFpuRegister()) << out;
3432 if (is_float) {
3433 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3434 } else {
3435 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3436 }
3437
3438 // And remove the temporary stack space we allocated.
3439 __ addl(ESP, Immediate(2 * elem_size));
3440}
3441
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003442
3443void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3444 DCHECK(instruction->IsDiv() || instruction->IsRem());
3445
3446 LocationSummary* locations = instruction->GetLocations();
3447 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003448 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003449
3450 Register out_register = locations->Out().AsRegister<Register>();
3451 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003452 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003453
3454 DCHECK(imm == 1 || imm == -1);
3455
3456 if (instruction->IsRem()) {
3457 __ xorl(out_register, out_register);
3458 } else {
3459 __ movl(out_register, input_register);
3460 if (imm == -1) {
3461 __ negl(out_register);
3462 }
3463 }
3464}
3465
3466
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003467void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003468 LocationSummary* locations = instruction->GetLocations();
3469
3470 Register out_register = locations->Out().AsRegister<Register>();
3471 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003472 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003473 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3474 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003475
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003476 Register num = locations->GetTemp(0).AsRegister<Register>();
3477
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003478 __ leal(num, Address(input_register, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003479 __ testl(input_register, input_register);
3480 __ cmovl(kGreaterEqual, num, input_register);
3481 int shift = CTZ(imm);
3482 __ sarl(num, Immediate(shift));
3483
3484 if (imm < 0) {
3485 __ negl(num);
3486 }
3487
3488 __ movl(out_register, num);
3489}
3490
3491void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3492 DCHECK(instruction->IsDiv() || instruction->IsRem());
3493
3494 LocationSummary* locations = instruction->GetLocations();
3495 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3496
3497 Register eax = locations->InAt(0).AsRegister<Register>();
3498 Register out = locations->Out().AsRegister<Register>();
3499 Register num;
3500 Register edx;
3501
3502 if (instruction->IsDiv()) {
3503 edx = locations->GetTemp(0).AsRegister<Register>();
3504 num = locations->GetTemp(1).AsRegister<Register>();
3505 } else {
3506 edx = locations->Out().AsRegister<Register>();
3507 num = locations->GetTemp(0).AsRegister<Register>();
3508 }
3509
3510 DCHECK_EQ(EAX, eax);
3511 DCHECK_EQ(EDX, edx);
3512 if (instruction->IsDiv()) {
3513 DCHECK_EQ(EAX, out);
3514 } else {
3515 DCHECK_EQ(EDX, out);
3516 }
3517
3518 int64_t magic;
3519 int shift;
3520 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3521
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003522 // Save the numerator.
3523 __ movl(num, eax);
3524
3525 // EAX = magic
3526 __ movl(eax, Immediate(magic));
3527
3528 // EDX:EAX = magic * numerator
3529 __ imull(num);
3530
3531 if (imm > 0 && magic < 0) {
3532 // EDX += num
3533 __ addl(edx, num);
3534 } else if (imm < 0 && magic > 0) {
3535 __ subl(edx, num);
3536 }
3537
3538 // Shift if needed.
3539 if (shift != 0) {
3540 __ sarl(edx, Immediate(shift));
3541 }
3542
3543 // EDX += 1 if EDX < 0
3544 __ movl(eax, edx);
3545 __ shrl(edx, Immediate(31));
3546 __ addl(edx, eax);
3547
3548 if (instruction->IsRem()) {
3549 __ movl(eax, num);
3550 __ imull(edx, Immediate(imm));
3551 __ subl(eax, edx);
3552 __ movl(edx, eax);
3553 } else {
3554 __ movl(eax, edx);
3555 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003556}
3557
Calin Juravlebacfec32014-11-14 15:54:36 +00003558void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3559 DCHECK(instruction->IsDiv() || instruction->IsRem());
3560
3561 LocationSummary* locations = instruction->GetLocations();
3562 Location out = locations->Out();
3563 Location first = locations->InAt(0);
3564 Location second = locations->InAt(1);
3565 bool is_div = instruction->IsDiv();
3566
3567 switch (instruction->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003568 case DataType::Type::kInt32: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003569 DCHECK_EQ(EAX, first.AsRegister<Register>());
3570 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003571
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003572 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003573 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003574
3575 if (imm == 0) {
3576 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3577 } else if (imm == 1 || imm == -1) {
3578 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003579 } else if (is_div && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003580 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003581 } else {
3582 DCHECK(imm <= -2 || imm >= 2);
3583 GenerateDivRemWithAnyConstant(instruction);
3584 }
3585 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01003586 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) DivRemMinusOneSlowPathX86(
David Srbecky9cd6d372016-02-09 15:24:47 +00003587 instruction, out.AsRegister<Register>(), is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003588 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003589
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003590 Register second_reg = second.AsRegister<Register>();
3591 // 0x80000000/-1 triggers an arithmetic exception!
3592 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3593 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003594
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003595 __ cmpl(second_reg, Immediate(-1));
3596 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003597
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003598 // edx:eax <- sign-extended of eax
3599 __ cdq();
3600 // eax = quotient, edx = remainder
3601 __ idivl(second_reg);
3602 __ Bind(slow_path->GetExitLabel());
3603 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003604 break;
3605 }
3606
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003607 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003608 InvokeRuntimeCallingConvention calling_convention;
3609 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3610 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3611 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3612 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3613 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3614 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3615
3616 if (is_div) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003617 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003618 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003619 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003620 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003621 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003622 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003623 break;
3624 }
3625
3626 default:
3627 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3628 }
3629}
3630
Calin Juravle7c4954d2014-10-28 16:57:40 +00003631void LocationsBuilderX86::VisitDiv(HDiv* div) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003632 LocationSummary::CallKind call_kind = (div->GetResultType() == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003633 ? LocationSummary::kCallOnMainOnly
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003634 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01003635 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(div, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003636
Calin Juravle7c4954d2014-10-28 16:57:40 +00003637 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003638 case DataType::Type::kInt32: {
Calin Juravled0d48522014-11-04 16:40:20 +00003639 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003640 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003641 locations->SetOut(Location::SameAsFirstInput());
3642 // Intel uses edx:eax as the dividend.
3643 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003644 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3645 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3646 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003647 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003648 locations->AddTemp(Location::RequiresRegister());
3649 }
Calin Juravled0d48522014-11-04 16:40:20 +00003650 break;
3651 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003652 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003653 InvokeRuntimeCallingConvention calling_convention;
3654 locations->SetInAt(0, Location::RegisterPairLocation(
3655 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3656 locations->SetInAt(1, Location::RegisterPairLocation(
3657 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3658 // Runtime helper puts the result in EAX, EDX.
3659 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003660 break;
3661 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003662 case DataType::Type::kFloat32:
3663 case DataType::Type::kFloat64: {
Calin Juravle7c4954d2014-10-28 16:57:40 +00003664 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003665 if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3666 DCHECK(div->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003667 } else if (div->InputAt(1)->IsConstant()) {
3668 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003669 } else {
3670 locations->SetInAt(1, Location::Any());
3671 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003672 locations->SetOut(Location::SameAsFirstInput());
3673 break;
3674 }
3675
3676 default:
3677 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3678 }
3679}
3680
3681void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3682 LocationSummary* locations = div->GetLocations();
3683 Location first = locations->InAt(0);
3684 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003685
3686 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003687 case DataType::Type::kInt32:
3688 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003689 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003690 break;
3691 }
3692
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003693 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003694 if (second.IsFpuRegister()) {
3695 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3696 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3697 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003698 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003699 __ divss(first.AsFpuRegister<XmmRegister>(),
3700 codegen_->LiteralFloatAddress(
3701 const_area->GetConstant()->AsFloatConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003702 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003703 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3704 } else {
3705 DCHECK(second.IsStackSlot());
3706 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3707 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003708 break;
3709 }
3710
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003711 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003712 if (second.IsFpuRegister()) {
3713 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3714 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3715 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003716 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003717 __ divsd(first.AsFpuRegister<XmmRegister>(),
3718 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003719 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3720 const_area->GetBaseMethodAddress(),
3721 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003722 } else {
3723 DCHECK(second.IsDoubleStackSlot());
3724 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3725 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003726 break;
3727 }
3728
3729 default:
3730 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3731 }
3732}
3733
Calin Juravlebacfec32014-11-14 15:54:36 +00003734void LocationsBuilderX86::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003735 DataType::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003736
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003737 LocationSummary::CallKind call_kind = (rem->GetResultType() == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003738 ? LocationSummary::kCallOnMainOnly
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003739 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01003740 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003741
Calin Juravled2ec87d2014-12-08 14:24:46 +00003742 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003743 case DataType::Type::kInt32: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003744 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003745 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003746 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003747 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3748 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3749 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003750 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003751 locations->AddTemp(Location::RequiresRegister());
3752 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003753 break;
3754 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003755 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003756 InvokeRuntimeCallingConvention calling_convention;
3757 locations->SetInAt(0, Location::RegisterPairLocation(
3758 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3759 locations->SetInAt(1, Location::RegisterPairLocation(
3760 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3761 // Runtime helper puts the result in EAX, EDX.
3762 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3763 break;
3764 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003765 case DataType::Type::kFloat64:
3766 case DataType::Type::kFloat32: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003767 locations->SetInAt(0, Location::Any());
3768 locations->SetInAt(1, Location::Any());
3769 locations->SetOut(Location::RequiresFpuRegister());
3770 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003771 break;
3772 }
3773
3774 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003775 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003776 }
3777}
3778
3779void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003780 DataType::Type type = rem->GetResultType();
Calin Juravlebacfec32014-11-14 15:54:36 +00003781 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003782 case DataType::Type::kInt32:
3783 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003784 GenerateDivRemIntegral(rem);
3785 break;
3786 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003787 case DataType::Type::kFloat32:
3788 case DataType::Type::kFloat64: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003789 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003790 break;
3791 }
3792 default:
3793 LOG(FATAL) << "Unexpected rem type " << type;
3794 }
3795}
3796
Calin Juravled0d48522014-11-04 16:40:20 +00003797void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003798 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003799 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003800 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003801 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003802 case DataType::Type::kInt8:
3803 case DataType::Type::kUint16:
3804 case DataType::Type::kInt16:
3805 case DataType::Type::kInt32: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003806 locations->SetInAt(0, Location::Any());
3807 break;
3808 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003809 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003810 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
3811 if (!instruction->IsConstant()) {
3812 locations->AddTemp(Location::RequiresRegister());
3813 }
3814 break;
3815 }
3816 default:
3817 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3818 }
Calin Juravled0d48522014-11-04 16:40:20 +00003819}
3820
3821void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01003822 SlowPathCode* slow_path =
3823 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003824 codegen_->AddSlowPath(slow_path);
3825
3826 LocationSummary* locations = instruction->GetLocations();
3827 Location value = locations->InAt(0);
3828
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003829 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003830 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003831 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003832 case DataType::Type::kInt8:
3833 case DataType::Type::kUint16:
3834 case DataType::Type::kInt16:
3835 case DataType::Type::kInt32: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003836 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003837 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003838 __ j(kEqual, slow_path->GetEntryLabel());
3839 } else if (value.IsStackSlot()) {
3840 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
3841 __ j(kEqual, slow_path->GetEntryLabel());
3842 } else {
3843 DCHECK(value.IsConstant()) << value;
3844 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01003845 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003846 }
3847 }
3848 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003849 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003850 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003851 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003852 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003853 __ movl(temp, value.AsRegisterPairLow<Register>());
3854 __ orl(temp, value.AsRegisterPairHigh<Register>());
3855 __ j(kEqual, slow_path->GetEntryLabel());
3856 } else {
3857 DCHECK(value.IsConstant()) << value;
3858 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3859 __ jmp(slow_path->GetEntryLabel());
3860 }
3861 }
3862 break;
3863 }
3864 default:
3865 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003866 }
Calin Juravled0d48522014-11-04 16:40:20 +00003867}
3868
Calin Juravle9aec02f2014-11-18 23:06:35 +00003869void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3870 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3871
3872 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003873 new (GetGraph()->GetAllocator()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003874
3875 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003876 case DataType::Type::kInt32:
3877 case DataType::Type::kInt64: {
Mark P Mendell73945692015-04-29 14:56:17 +00003878 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003879 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003880 // The shift count needs to be in CL or a constant.
3881 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003882 locations->SetOut(Location::SameAsFirstInput());
3883 break;
3884 }
3885 default:
3886 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3887 }
3888}
3889
3890void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3891 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3892
3893 LocationSummary* locations = op->GetLocations();
3894 Location first = locations->InAt(0);
3895 Location second = locations->InAt(1);
3896 DCHECK(first.Equals(locations->Out()));
3897
3898 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003899 case DataType::Type::kInt32: {
Mark P Mendell73945692015-04-29 14:56:17 +00003900 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003901 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003902 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003903 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003904 DCHECK_EQ(ECX, second_reg);
3905 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003906 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003907 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003908 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003909 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003910 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003911 }
3912 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003913 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003914 if (shift == 0) {
3915 return;
3916 }
3917 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003918 if (op->IsShl()) {
3919 __ shll(first_reg, imm);
3920 } else if (op->IsShr()) {
3921 __ sarl(first_reg, imm);
3922 } else {
3923 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003924 }
3925 }
3926 break;
3927 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003928 case DataType::Type::kInt64: {
Mark P Mendell73945692015-04-29 14:56:17 +00003929 if (second.IsRegister()) {
3930 Register second_reg = second.AsRegister<Register>();
3931 DCHECK_EQ(ECX, second_reg);
3932 if (op->IsShl()) {
3933 GenerateShlLong(first, second_reg);
3934 } else if (op->IsShr()) {
3935 GenerateShrLong(first, second_reg);
3936 } else {
3937 GenerateUShrLong(first, second_reg);
3938 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003939 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003940 // Shift by a constant.
Roland Levillain5b5b9312016-03-22 14:57:31 +00003941 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003942 // Nothing to do if the shift is 0, as the input is already the output.
3943 if (shift != 0) {
3944 if (op->IsShl()) {
3945 GenerateShlLong(first, shift);
3946 } else if (op->IsShr()) {
3947 GenerateShrLong(first, shift);
3948 } else {
3949 GenerateUShrLong(first, shift);
3950 }
3951 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003952 }
3953 break;
3954 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003955 default:
3956 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3957 }
3958}
3959
Mark P Mendell73945692015-04-29 14:56:17 +00003960void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3961 Register low = loc.AsRegisterPairLow<Register>();
3962 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003963 if (shift == 1) {
3964 // This is just an addition.
3965 __ addl(low, low);
3966 __ adcl(high, high);
3967 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003968 // Shift by 32 is easy. High gets low, and low gets 0.
3969 codegen_->EmitParallelMoves(
3970 loc.ToLow(),
3971 loc.ToHigh(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003972 DataType::Type::kInt32,
Mark P Mendell73945692015-04-29 14:56:17 +00003973 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3974 loc.ToLow(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003975 DataType::Type::kInt32);
Mark P Mendell73945692015-04-29 14:56:17 +00003976 } else if (shift > 32) {
3977 // Low part becomes 0. High part is low part << (shift-32).
3978 __ movl(high, low);
3979 __ shll(high, Immediate(shift - 32));
3980 __ xorl(low, low);
3981 } else {
3982 // Between 1 and 31.
3983 __ shld(high, low, Immediate(shift));
3984 __ shll(low, Immediate(shift));
3985 }
3986}
3987
Calin Juravle9aec02f2014-11-18 23:06:35 +00003988void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003989 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003990 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
3991 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
3992 __ testl(shifter, Immediate(32));
3993 __ j(kEqual, &done);
3994 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
3995 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
3996 __ Bind(&done);
3997}
3998
Mark P Mendell73945692015-04-29 14:56:17 +00003999void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
4000 Register low = loc.AsRegisterPairLow<Register>();
4001 Register high = loc.AsRegisterPairHigh<Register>();
4002 if (shift == 32) {
4003 // Need to copy the sign.
4004 DCHECK_NE(low, high);
4005 __ movl(low, high);
4006 __ sarl(high, Immediate(31));
4007 } else if (shift > 32) {
4008 DCHECK_NE(low, high);
4009 // High part becomes sign. Low part is shifted by shift - 32.
4010 __ movl(low, high);
4011 __ sarl(high, Immediate(31));
4012 __ sarl(low, Immediate(shift - 32));
4013 } else {
4014 // Between 1 and 31.
4015 __ shrd(low, high, Immediate(shift));
4016 __ sarl(high, Immediate(shift));
4017 }
4018}
4019
Calin Juravle9aec02f2014-11-18 23:06:35 +00004020void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004021 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004022 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4023 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
4024 __ testl(shifter, Immediate(32));
4025 __ j(kEqual, &done);
4026 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4027 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
4028 __ Bind(&done);
4029}
4030
Mark P Mendell73945692015-04-29 14:56:17 +00004031void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
4032 Register low = loc.AsRegisterPairLow<Register>();
4033 Register high = loc.AsRegisterPairHigh<Register>();
4034 if (shift == 32) {
4035 // Shift by 32 is easy. Low gets high, and high gets 0.
4036 codegen_->EmitParallelMoves(
4037 loc.ToHigh(),
4038 loc.ToLow(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004039 DataType::Type::kInt32,
Mark P Mendell73945692015-04-29 14:56:17 +00004040 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
4041 loc.ToHigh(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004042 DataType::Type::kInt32);
Mark P Mendell73945692015-04-29 14:56:17 +00004043 } else if (shift > 32) {
4044 // Low part is high >> (shift - 32). High part becomes 0.
4045 __ movl(low, high);
4046 __ shrl(low, Immediate(shift - 32));
4047 __ xorl(high, high);
4048 } else {
4049 // Between 1 and 31.
4050 __ shrd(low, high, Immediate(shift));
4051 __ shrl(high, Immediate(shift));
4052 }
4053}
4054
Calin Juravle9aec02f2014-11-18 23:06:35 +00004055void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004056 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004057 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4058 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
4059 __ testl(shifter, Immediate(32));
4060 __ j(kEqual, &done);
4061 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4062 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
4063 __ Bind(&done);
4064}
4065
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004066void LocationsBuilderX86::VisitRor(HRor* ror) {
4067 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004068 new (GetGraph()->GetAllocator()) LocationSummary(ror, LocationSummary::kNoCall);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004069
4070 switch (ror->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004071 case DataType::Type::kInt64:
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004072 // Add the temporary needed.
4073 locations->AddTemp(Location::RequiresRegister());
4074 FALLTHROUGH_INTENDED;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004075 case DataType::Type::kInt32:
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004076 locations->SetInAt(0, Location::RequiresRegister());
4077 // The shift count needs to be in CL (unless it is a constant).
4078 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
4079 locations->SetOut(Location::SameAsFirstInput());
4080 break;
4081 default:
4082 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4083 UNREACHABLE();
4084 }
4085}
4086
4087void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
4088 LocationSummary* locations = ror->GetLocations();
4089 Location first = locations->InAt(0);
4090 Location second = locations->InAt(1);
4091
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004092 if (ror->GetResultType() == DataType::Type::kInt32) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004093 Register first_reg = first.AsRegister<Register>();
4094 if (second.IsRegister()) {
4095 Register second_reg = second.AsRegister<Register>();
4096 __ rorl(first_reg, second_reg);
4097 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004098 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004099 __ rorl(first_reg, imm);
4100 }
4101 return;
4102 }
4103
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004104 DCHECK_EQ(ror->GetResultType(), DataType::Type::kInt64);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004105 Register first_reg_lo = first.AsRegisterPairLow<Register>();
4106 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
4107 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
4108 if (second.IsRegister()) {
4109 Register second_reg = second.AsRegister<Register>();
4110 DCHECK_EQ(second_reg, ECX);
4111 __ movl(temp_reg, first_reg_hi);
4112 __ shrd(first_reg_hi, first_reg_lo, second_reg);
4113 __ shrd(first_reg_lo, temp_reg, second_reg);
4114 __ movl(temp_reg, first_reg_hi);
4115 __ testl(second_reg, Immediate(32));
4116 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
4117 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
4118 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004119 int32_t shift_amt = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004120 if (shift_amt == 0) {
4121 // Already fine.
4122 return;
4123 }
4124 if (shift_amt == 32) {
4125 // Just swap.
4126 __ movl(temp_reg, first_reg_lo);
4127 __ movl(first_reg_lo, first_reg_hi);
4128 __ movl(first_reg_hi, temp_reg);
4129 return;
4130 }
4131
4132 Immediate imm(shift_amt);
4133 // Save the constents of the low value.
4134 __ movl(temp_reg, first_reg_lo);
4135
4136 // Shift right into low, feeding bits from high.
4137 __ shrd(first_reg_lo, first_reg_hi, imm);
4138
4139 // Shift right into high, feeding bits from the original low.
4140 __ shrd(first_reg_hi, temp_reg, imm);
4141
4142 // Swap if needed.
4143 if (shift_amt > 32) {
4144 __ movl(temp_reg, first_reg_lo);
4145 __ movl(first_reg_lo, first_reg_hi);
4146 __ movl(first_reg_hi, temp_reg);
4147 }
4148 }
4149}
4150
Calin Juravle9aec02f2014-11-18 23:06:35 +00004151void LocationsBuilderX86::VisitShl(HShl* shl) {
4152 HandleShift(shl);
4153}
4154
4155void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
4156 HandleShift(shl);
4157}
4158
4159void LocationsBuilderX86::VisitShr(HShr* shr) {
4160 HandleShift(shr);
4161}
4162
4163void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
4164 HandleShift(shr);
4165}
4166
4167void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
4168 HandleShift(ushr);
4169}
4170
4171void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
4172 HandleShift(ushr);
4173}
4174
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004175void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004176 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
4177 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004178 locations->SetOut(Location::RegisterLocation(EAX));
David Brazdil6de19382016-01-08 17:37:10 +00004179 if (instruction->IsStringAlloc()) {
4180 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4181 } else {
4182 InvokeRuntimeCallingConvention calling_convention;
4183 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00004184 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004185}
4186
4187void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004188 // Note: if heap poisoning is enabled, the entry point takes cares
4189 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004190 if (instruction->IsStringAlloc()) {
4191 // String is allocated through StringFactory. Call NewEmptyString entry point.
4192 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07004193 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004194 __ fs()->movl(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString)));
4195 __ call(Address(temp, code_offset.Int32Value()));
4196 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4197 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01004198 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00004199 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00004200 DCHECK(!codegen_->IsLeafMethod());
4201 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004202}
4203
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004204void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004205 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
4206 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004207 locations->SetOut(Location::RegisterLocation(EAX));
4208 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004209 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4210 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004211}
4212
4213void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004214 // Note: if heap poisoning is enabled, the entry point takes cares
4215 // of poisoning the reference.
Nicolas Geoffrayd0958442017-01-30 14:57:16 +00004216 QuickEntrypointEnum entrypoint =
4217 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
4218 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004219 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004220 DCHECK(!codegen_->IsLeafMethod());
4221}
4222
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004223void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004224 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004225 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004226 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4227 if (location.IsStackSlot()) {
4228 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4229 } else if (location.IsDoubleStackSlot()) {
4230 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004231 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004232 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004233}
4234
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004235void InstructionCodeGeneratorX86::VisitParameterValue(
4236 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4237}
4238
4239void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
4240 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004241 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004242 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4243}
4244
4245void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004246}
4247
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004248void LocationsBuilderX86::VisitClassTableGet(HClassTableGet* instruction) {
4249 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004250 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004251 locations->SetInAt(0, Location::RequiresRegister());
4252 locations->SetOut(Location::RequiresRegister());
4253}
4254
4255void InstructionCodeGeneratorX86::VisitClassTableGet(HClassTableGet* instruction) {
4256 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004257 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004258 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004259 instruction->GetIndex(), kX86PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004260 __ movl(locations->Out().AsRegister<Register>(),
4261 Address(locations->InAt(0).AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004262 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004263 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004264 instruction->GetIndex(), kX86PointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004265 __ movl(locations->Out().AsRegister<Register>(),
4266 Address(locations->InAt(0).AsRegister<Register>(),
4267 mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
4268 // temp = temp->GetImtEntryAt(method_offset);
4269 __ movl(locations->Out().AsRegister<Register>(),
4270 Address(locations->Out().AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004271 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004272}
4273
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004274void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004275 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004276 new (GetGraph()->GetAllocator()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004277 locations->SetInAt(0, Location::RequiresRegister());
4278 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004279}
4280
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004281void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
4282 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01004283 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004284 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01004285 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004286 switch (not_->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004287 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004288 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004289 break;
4290
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004291 case DataType::Type::kInt64:
Roland Levillain70566432014-10-24 16:20:17 +01004292 __ notl(out.AsRegisterPairLow<Register>());
4293 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004294 break;
4295
4296 default:
4297 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4298 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004299}
4300
David Brazdil66d126e2015-04-03 16:02:44 +01004301void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
4302 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004303 new (GetGraph()->GetAllocator()) LocationSummary(bool_not, LocationSummary::kNoCall);
David Brazdil66d126e2015-04-03 16:02:44 +01004304 locations->SetInAt(0, Location::RequiresRegister());
4305 locations->SetOut(Location::SameAsFirstInput());
4306}
4307
4308void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004309 LocationSummary* locations = bool_not->GetLocations();
4310 Location in = locations->InAt(0);
4311 Location out = locations->Out();
4312 DCHECK(in.Equals(out));
4313 __ xorl(out.AsRegister<Register>(), Immediate(1));
4314}
4315
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004316void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004317 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004318 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004319 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004320 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004321 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004322 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004323 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004324 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004325 case DataType::Type::kInt32:
4326 case DataType::Type::kInt64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00004327 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00004328 locations->SetInAt(1, Location::Any());
4329 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4330 break;
4331 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004332 case DataType::Type::kFloat32:
4333 case DataType::Type::kFloat64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00004334 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004335 if (compare->InputAt(1)->IsX86LoadFromConstantTable()) {
4336 DCHECK(compare->InputAt(1)->IsEmittedAtUseSite());
4337 } else if (compare->InputAt(1)->IsConstant()) {
4338 locations->SetInAt(1, Location::RequiresFpuRegister());
4339 } else {
4340 locations->SetInAt(1, Location::Any());
4341 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004342 locations->SetOut(Location::RequiresRegister());
4343 break;
4344 }
4345 default:
4346 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4347 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004348}
4349
4350void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004351 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004352 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004353 Location left = locations->InAt(0);
4354 Location right = locations->InAt(1);
4355
Mark Mendell0c9497d2015-08-21 09:30:05 -04004356 NearLabel less, greater, done;
Aart Bika19616e2016-02-01 18:57:58 -08004357 Condition less_cond = kLess;
4358
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004359 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004360 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004361 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004362 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004363 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004364 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004365 case DataType::Type::kInt32: {
Roland Levillain0b671c02016-08-19 12:02:34 +01004366 codegen_->GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08004367 break;
4368 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004369 case DataType::Type::kInt64: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004370 Register left_low = left.AsRegisterPairLow<Register>();
4371 Register left_high = left.AsRegisterPairHigh<Register>();
4372 int32_t val_low = 0;
4373 int32_t val_high = 0;
4374 bool right_is_const = false;
4375
4376 if (right.IsConstant()) {
4377 DCHECK(right.GetConstant()->IsLongConstant());
4378 right_is_const = true;
4379 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
4380 val_low = Low32Bits(val);
4381 val_high = High32Bits(val);
4382 }
4383
Calin Juravleddb7df22014-11-25 20:56:51 +00004384 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004385 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004386 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004387 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004388 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004389 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004390 codegen_->Compare32BitValue(left_high, val_high);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004391 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004392 __ j(kLess, &less); // Signed compare.
4393 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004394 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004395 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004396 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004397 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004398 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004399 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004400 codegen_->Compare32BitValue(left_low, val_low);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004401 }
Aart Bika19616e2016-02-01 18:57:58 -08004402 less_cond = kBelow; // for CF (unsigned).
Calin Juravleddb7df22014-11-25 20:56:51 +00004403 break;
4404 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004405 case DataType::Type::kFloat32: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004406 GenerateFPCompare(left, right, compare, false);
Calin Juravleddb7df22014-11-25 20:56:51 +00004407 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004408 less_cond = kBelow; // for CF (floats).
Calin Juravleddb7df22014-11-25 20:56:51 +00004409 break;
4410 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004411 case DataType::Type::kFloat64: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004412 GenerateFPCompare(left, right, compare, true);
Calin Juravleddb7df22014-11-25 20:56:51 +00004413 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004414 less_cond = kBelow; // for CF (floats).
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004415 break;
4416 }
4417 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004418 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004419 }
Aart Bika19616e2016-02-01 18:57:58 -08004420
Calin Juravleddb7df22014-11-25 20:56:51 +00004421 __ movl(out, Immediate(0));
4422 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08004423 __ j(less_cond, &less);
Calin Juravleddb7df22014-11-25 20:56:51 +00004424
4425 __ Bind(&greater);
4426 __ movl(out, Immediate(1));
4427 __ jmp(&done);
4428
4429 __ Bind(&less);
4430 __ movl(out, Immediate(-1));
4431
4432 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004433}
4434
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004435void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004436 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004437 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004438 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004439 locations->SetInAt(i, Location::Any());
4440 }
4441 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004442}
4443
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004444void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004445 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004446}
4447
Roland Levillain7c1559a2015-12-15 10:55:36 +00004448void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004449 /*
4450 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
4451 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
4452 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4453 */
4454 switch (kind) {
4455 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004456 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004457 break;
4458 }
4459 case MemBarrierKind::kAnyStore:
4460 case MemBarrierKind::kLoadAny:
4461 case MemBarrierKind::kStoreStore: {
4462 // nop
4463 break;
4464 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004465 case MemBarrierKind::kNTStoreStore:
4466 // Non-Temporal Store/Store needs an explicit fence.
4467 MemoryFence(/* non-temporal */ true);
4468 break;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004469 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004470}
4471
Vladimir Markodc151b22015-10-15 18:02:30 +01004472HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
4473 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004474 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004475 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004476}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004477
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004478Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4479 Register temp) {
4480 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Markoc53c0792015-11-19 15:48:33 +00004481 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004482 if (!invoke->GetLocations()->Intrinsified()) {
4483 return location.AsRegister<Register>();
4484 }
4485 // For intrinsics we allow any location, so it may be on the stack.
4486 if (!location.IsRegister()) {
4487 __ movl(temp, Address(ESP, location.GetStackIndex()));
4488 return temp;
4489 }
4490 // For register locations, check if the register was saved. If so, get it from the stack.
4491 // Note: There is a chance that the register was saved but not overwritten, so we could
4492 // save one load. However, since this is just an intrinsic slow path we prefer this
4493 // simple and more robust approach rather that trying to determine if that's the case.
4494 SlowPathCode* slow_path = GetCurrentSlowPath();
Vladimir Marko4ee8e292017-06-02 15:39:30 +00004495 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
4496 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4497 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4498 __ movl(temp, Address(ESP, stack_offset));
4499 return temp;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004500 }
4501 return location.AsRegister<Register>();
4502}
4503
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004504void CodeGeneratorX86::GenerateStaticOrDirectCall(
4505 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Vladimir Marko58155012015-08-19 12:49:41 +00004506 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4507 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004508 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00004509 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004510 uint32_t offset =
4511 GetThreadOffset<kX86PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
4512 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004513 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004514 }
Vladimir Marko58155012015-08-19 12:49:41 +00004515 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004516 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004517 break;
Vladimir Marko65979462017-05-19 17:25:12 +01004518 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
4519 DCHECK(GetCompilerOptions().IsBootImage());
4520 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4521 temp.AsRegister<Register>());
4522 __ leal(temp.AsRegister<Register>(), Address(base_reg, CodeGeneratorX86::kDummy32BitOffset));
4523 RecordBootMethodPatch(invoke);
4524 break;
4525 }
Vladimir Marko58155012015-08-19 12:49:41 +00004526 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4527 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
4528 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004529 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004530 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4531 temp.AsRegister<Register>());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004532 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004533 // Bind a new fixup label at the end of the "movl" insn.
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004534 __ Bind(NewMethodBssEntryPatch(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004535 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress(),
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004536 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex())));
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004537 break;
4538 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004539 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
4540 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
4541 return; // No code pointer retrieval; the runtime performs the call directly.
Vladimir Marko9b688a02015-05-06 14:12:42 +01004542 }
Vladimir Marko58155012015-08-19 12:49:41 +00004543 }
4544
4545 switch (invoke->GetCodePtrLocation()) {
4546 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4547 __ call(GetFrameEntryLabel());
4548 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004549 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4550 // (callee_method + offset_of_quick_compiled_code)()
4551 __ call(Address(callee_method.AsRegister<Register>(),
4552 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07004553 kX86PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004554 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04004555 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004556 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Mark Mendell09ed1a32015-03-25 08:30:06 -04004557
4558 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04004559}
4560
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004561void CodeGeneratorX86::GenerateVirtualCall(
4562 HInvokeVirtual* invoke, Location temp_in, SlowPathCode* slow_path) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004563 Register temp = temp_in.AsRegister<Register>();
4564 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4565 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004566
4567 // Use the calling convention instead of the location of the receiver, as
4568 // intrinsics may have put the receiver in a different register. In the intrinsics
4569 // slow path, the arguments have been moved to the right place, so here we are
4570 // guaranteed that the receiver is the first register of the calling convention.
4571 InvokeDexCallingConvention calling_convention;
4572 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004573 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004574 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004575 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004576 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004577 // Instead of simply (possibly) unpoisoning `temp` here, we should
4578 // emit a read barrier for the previous class reference load.
4579 // However this is not required in practice, as this is an
4580 // intermediate/temporary reference and because the current
4581 // concurrent copying collector keeps the from-space memory
4582 // intact/accessible until the end of the marking phase (the
4583 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004584 __ MaybeUnpoisonHeapReference(temp);
4585 // temp = temp->GetMethodAt(method_offset);
4586 __ movl(temp, Address(temp, method_offset));
4587 // call temp->GetEntryPoint();
4588 __ call(Address(
Andreas Gampe542451c2016-07-26 09:02:02 -07004589 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004590 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004591}
4592
Vladimir Marko65979462017-05-19 17:25:12 +01004593void CodeGeneratorX86::RecordBootMethodPatch(HInvokeStaticOrDirect* invoke) {
4594 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
4595 HX86ComputeBaseMethodAddress* address =
4596 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
4597 boot_image_method_patches_.emplace_back(address,
4598 *invoke->GetTargetMethod().dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07004599 invoke->GetTargetMethod().index);
Vladimir Marko65979462017-05-19 17:25:12 +01004600 __ Bind(&boot_image_method_patches_.back().label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004601}
4602
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004603Label* CodeGeneratorX86::NewMethodBssEntryPatch(
4604 HX86ComputeBaseMethodAddress* method_address,
4605 MethodReference target_method) {
4606 // Add the patch entry and bind its label at the end of the instruction.
4607 method_bss_entry_patches_.emplace_back(method_address,
4608 *target_method.dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07004609 target_method.index);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004610 return &method_bss_entry_patches_.back().label;
4611}
4612
Vladimir Marko1998cd02017-01-13 13:02:58 +00004613void CodeGeneratorX86::RecordBootTypePatch(HLoadClass* load_class) {
Vladimir Marko00916b92017-05-17 17:45:45 +01004614 HX86ComputeBaseMethodAddress* address = load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004615 boot_image_type_patches_.emplace_back(address,
4616 load_class->GetDexFile(),
Vladimir Marko1998cd02017-01-13 13:02:58 +00004617 load_class->GetTypeIndex().index_);
4618 __ Bind(&boot_image_type_patches_.back().label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004619}
4620
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004621Label* CodeGeneratorX86::NewTypeBssEntryPatch(HLoadClass* load_class) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004622 HX86ComputeBaseMethodAddress* address =
4623 load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
4624 type_bss_entry_patches_.emplace_back(
4625 address, load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00004626 return &type_bss_entry_patches_.back().label;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004627}
4628
Vladimir Marko65979462017-05-19 17:25:12 +01004629void CodeGeneratorX86::RecordBootStringPatch(HLoadString* load_string) {
Vladimir Marko65979462017-05-19 17:25:12 +01004630 HX86ComputeBaseMethodAddress* address = load_string->InputAt(0)->AsX86ComputeBaseMethodAddress();
4631 string_patches_.emplace_back(address,
4632 load_string->GetDexFile(),
4633 load_string->GetStringIndex().index_);
4634 __ Bind(&string_patches_.back().label);
4635}
4636
Vladimir Markoaad75c62016-10-03 08:46:48 +00004637Label* CodeGeneratorX86::NewStringBssEntryPatch(HLoadString* load_string) {
4638 DCHECK(!GetCompilerOptions().IsBootImage());
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004639 HX86ComputeBaseMethodAddress* address =
4640 load_string->InputAt(0)->AsX86ComputeBaseMethodAddress();
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004641 string_bss_entry_patches_.emplace_back(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004642 address, load_string->GetDexFile(), load_string->GetStringIndex().index_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004643 return &string_bss_entry_patches_.back().label;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004644}
4645
Vladimir Markoaad75c62016-10-03 08:46:48 +00004646// The label points to the end of the "movl" or another instruction but the literal offset
4647// for method patch needs to point to the embedded constant which occupies the last 4 bytes.
4648constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
4649
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004650template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +00004651inline void CodeGeneratorX86::EmitPcRelativeLinkerPatches(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004652 const ArenaDeque<X86PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004653 ArenaVector<linker::LinkerPatch>* linker_patches) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004654 for (const X86PcRelativePatchInfo& info : infos) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00004655 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004656 linker_patches->push_back(Factory(
4657 literal_offset, &info.dex_file, GetMethodAddressOffset(info.method_address), info.index));
Vladimir Markoaad75c62016-10-03 08:46:48 +00004658 }
4659}
4660
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004661void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Marko58155012015-08-19 12:49:41 +00004662 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004663 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01004664 boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004665 method_bss_entry_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +00004666 boot_image_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01004667 type_bss_entry_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004668 string_patches_.size() +
4669 string_bss_entry_patches_.size();
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004670 linker_patches->reserve(size);
Vladimir Marko764d4542017-05-16 10:31:41 +01004671 if (GetCompilerOptions().IsBootImage()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004672 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
4673 boot_image_method_patches_, linker_patches);
4674 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
4675 boot_image_type_patches_, linker_patches);
4676 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
4677 string_patches_, linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004678 } else {
Vladimir Marko65979462017-05-19 17:25:12 +01004679 DCHECK(boot_image_method_patches_.empty());
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004680 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeClassTablePatch>(
4681 boot_image_type_patches_, linker_patches);
4682 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringInternTablePatch>(
4683 string_patches_, linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004684 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004685 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
4686 method_bss_entry_patches_, linker_patches);
4687 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
4688 type_bss_entry_patches_, linker_patches);
4689 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
4690 string_bss_entry_patches_, linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00004691 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00004692}
4693
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004694void CodeGeneratorX86::MarkGCCard(Register temp,
4695 Register card,
4696 Register object,
4697 Register value,
4698 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004699 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004700 if (value_can_be_null) {
4701 __ testl(value, value);
4702 __ j(kEqual, &is_null);
4703 }
Andreas Gampe542451c2016-07-26 09:02:02 -07004704 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86PointerSize>().Int32Value()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004705 __ movl(temp, object);
4706 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004707 __ movb(Address(temp, card, TIMES_1, 0),
4708 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004709 if (value_can_be_null) {
4710 __ Bind(&is_null);
4711 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004712}
4713
Calin Juravle52c48962014-12-16 17:02:57 +00004714void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4715 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00004716
4717 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004718 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004719 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004720 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
4721 kEmitCompilerReadBarrier
4722 ? LocationSummary::kCallOnSlowPath
4723 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004724 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004725 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004726 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004727 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004728
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004729 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004730 locations->SetOut(Location::RequiresFpuRegister());
4731 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004732 // The output overlaps in case of long: we don't want the low move
4733 // to overwrite the object's location. Likewise, in the case of
4734 // an object field get with read barriers enabled, we do not want
4735 // the move to overwrite the object's location, as we need it to emit
4736 // the read barrier.
4737 locations->SetOut(
4738 Location::RequiresRegister(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004739 (object_field_get_with_read_barrier || instruction->GetType() == DataType::Type::kInt64) ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00004740 Location::kOutputOverlap :
4741 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004742 }
Calin Juravle52c48962014-12-16 17:02:57 +00004743
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004744 if (field_info.IsVolatile() && (field_info.GetFieldType() == DataType::Type::kInt64)) {
Calin Juravle52c48962014-12-16 17:02:57 +00004745 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00004746 // So we use an XMM register as a temp to achieve atomicity (first
4747 // load the temp into the XMM and then copy the XMM into the
4748 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00004749 locations->AddTemp(Location::RequiresFpuRegister());
4750 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004751}
4752
Calin Juravle52c48962014-12-16 17:02:57 +00004753void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
4754 const FieldInfo& field_info) {
4755 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004756
Calin Juravle52c48962014-12-16 17:02:57 +00004757 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004758 Location base_loc = locations->InAt(0);
4759 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004760 Location out = locations->Out();
4761 bool is_volatile = field_info.IsVolatile();
Vladimir Marko61b92282017-10-11 13:23:17 +01004762 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
4763 DataType::Type load_type = instruction->GetType();
Calin Juravle52c48962014-12-16 17:02:57 +00004764 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4765
Vladimir Marko61b92282017-10-11 13:23:17 +01004766 switch (load_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004767 case DataType::Type::kBool:
4768 case DataType::Type::kUint8: {
Calin Juravle52c48962014-12-16 17:02:57 +00004769 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004770 break;
4771 }
4772
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004773 case DataType::Type::kInt8: {
Calin Juravle52c48962014-12-16 17:02:57 +00004774 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004775 break;
4776 }
4777
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004778 case DataType::Type::kUint16: {
4779 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004780 break;
4781 }
4782
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004783 case DataType::Type::kInt16: {
4784 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004785 break;
4786 }
4787
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004788 case DataType::Type::kInt32:
Calin Juravle52c48962014-12-16 17:02:57 +00004789 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004790 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00004791
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004792 case DataType::Type::kReference: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004793 // /* HeapReference<Object> */ out = *(base + offset)
4794 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004795 // Note that a potential implicit null check is handled in this
4796 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4797 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00004798 instruction, out, base, offset, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00004799 if (is_volatile) {
4800 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4801 }
4802 } else {
4803 __ movl(out.AsRegister<Register>(), Address(base, offset));
4804 codegen_->MaybeRecordImplicitNullCheck(instruction);
4805 if (is_volatile) {
4806 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4807 }
4808 // If read barriers are enabled, emit read barriers other than
4809 // Baker's using a slow path (and also unpoison the loaded
4810 // reference, if heap poisoning is enabled).
4811 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4812 }
4813 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004814 }
4815
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004816 case DataType::Type::kInt64: {
Calin Juravle52c48962014-12-16 17:02:57 +00004817 if (is_volatile) {
4818 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4819 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004820 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004821 __ movd(out.AsRegisterPairLow<Register>(), temp);
4822 __ psrlq(temp, Immediate(32));
4823 __ movd(out.AsRegisterPairHigh<Register>(), temp);
4824 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004825 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00004826 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004827 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004828 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
4829 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004830 break;
4831 }
4832
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004833 case DataType::Type::kFloat32: {
Calin Juravle52c48962014-12-16 17:02:57 +00004834 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004835 break;
4836 }
4837
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004838 case DataType::Type::kFloat64: {
Calin Juravle52c48962014-12-16 17:02:57 +00004839 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004840 break;
4841 }
4842
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004843 case DataType::Type::kVoid:
Vladimir Marko61b92282017-10-11 13:23:17 +01004844 LOG(FATAL) << "Unreachable type " << load_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004845 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004846 }
Calin Juravle52c48962014-12-16 17:02:57 +00004847
Vladimir Marko61b92282017-10-11 13:23:17 +01004848 if (load_type == DataType::Type::kReference || load_type == DataType::Type::kInt64) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004849 // Potential implicit null checks, in the case of reference or
4850 // long fields, are handled in the previous switch statement.
4851 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004852 codegen_->MaybeRecordImplicitNullCheck(instruction);
4853 }
4854
Calin Juravle52c48962014-12-16 17:02:57 +00004855 if (is_volatile) {
Vladimir Marko61b92282017-10-11 13:23:17 +01004856 if (load_type == DataType::Type::kReference) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004857 // Memory barriers, in the case of references, are also handled
4858 // in the previous switch statement.
4859 } else {
4860 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4861 }
Roland Levillain4d027112015-07-01 15:41:14 +01004862 }
Calin Juravle52c48962014-12-16 17:02:57 +00004863}
4864
4865void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4866 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4867
4868 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004869 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00004870 locations->SetInAt(0, Location::RequiresRegister());
4871 bool is_volatile = field_info.IsVolatile();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004872 DataType::Type field_type = field_info.GetFieldType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004873 bool is_byte_type = DataType::Size(field_type) == 1u;
Calin Juravle52c48962014-12-16 17:02:57 +00004874
4875 // The register allocator does not support multiple
4876 // inputs that die at entry with one in a specific register.
4877 if (is_byte_type) {
4878 // Ensure the value is in a byte register.
4879 locations->SetInAt(1, Location::RegisterLocation(EAX));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004880 } else if (DataType::IsFloatingPointType(field_type)) {
4881 if (is_volatile && field_type == DataType::Type::kFloat64) {
Mark Mendell81489372015-11-04 11:30:41 -05004882 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4883 locations->SetInAt(1, Location::RequiresFpuRegister());
4884 } else {
4885 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4886 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004887 } else if (is_volatile && field_type == DataType::Type::kInt64) {
Mark Mendell81489372015-11-04 11:30:41 -05004888 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00004889 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004890
Calin Juravle52c48962014-12-16 17:02:57 +00004891 // 64bits value can be atomically written to an address with movsd and an XMM register.
4892 // We need two XMM registers because there's no easier way to (bit) copy a register pair
4893 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
4894 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
4895 // isolated cases when we need this it isn't worth adding the extra complexity.
4896 locations->AddTemp(Location::RequiresFpuRegister());
4897 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004898 } else {
4899 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4900
4901 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4902 // Temporary registers for the write barrier.
4903 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
4904 // Ensure the card is in a byte register.
4905 locations->AddTemp(Location::RegisterLocation(ECX));
4906 }
Calin Juravle52c48962014-12-16 17:02:57 +00004907 }
4908}
4909
4910void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004911 const FieldInfo& field_info,
4912 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004913 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4914
4915 LocationSummary* locations = instruction->GetLocations();
4916 Register base = locations->InAt(0).AsRegister<Register>();
4917 Location value = locations->InAt(1);
4918 bool is_volatile = field_info.IsVolatile();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004919 DataType::Type field_type = field_info.GetFieldType();
Calin Juravle52c48962014-12-16 17:02:57 +00004920 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004921 bool needs_write_barrier =
4922 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004923
4924 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004925 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004926 }
4927
Mark Mendell81489372015-11-04 11:30:41 -05004928 bool maybe_record_implicit_null_check_done = false;
4929
Calin Juravle52c48962014-12-16 17:02:57 +00004930 switch (field_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004931 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004932 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004933 case DataType::Type::kInt8: {
Calin Juravle52c48962014-12-16 17:02:57 +00004934 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
4935 break;
4936 }
4937
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004938 case DataType::Type::kUint16:
4939 case DataType::Type::kInt16: {
Mark Mendell81489372015-11-04 11:30:41 -05004940 if (value.IsConstant()) {
Nicolas Geoffray78612082017-07-24 14:18:53 +01004941 __ movw(Address(base, offset),
4942 Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Mark Mendell81489372015-11-04 11:30:41 -05004943 } else {
4944 __ movw(Address(base, offset), value.AsRegister<Register>());
4945 }
Calin Juravle52c48962014-12-16 17:02:57 +00004946 break;
4947 }
4948
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004949 case DataType::Type::kInt32:
4950 case DataType::Type::kReference: {
Roland Levillain4d027112015-07-01 15:41:14 +01004951 if (kPoisonHeapReferences && needs_write_barrier) {
4952 // Note that in the case where `value` is a null reference,
4953 // we do not enter this block, as the reference does not
4954 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004955 DCHECK_EQ(field_type, DataType::Type::kReference);
Roland Levillain4d027112015-07-01 15:41:14 +01004956 Register temp = locations->GetTemp(0).AsRegister<Register>();
4957 __ movl(temp, value.AsRegister<Register>());
4958 __ PoisonHeapReference(temp);
4959 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05004960 } else if (value.IsConstant()) {
4961 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4962 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01004963 } else {
Nicolas Geoffray03971632016-03-17 10:44:24 +00004964 DCHECK(value.IsRegister()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01004965 __ movl(Address(base, offset), value.AsRegister<Register>());
4966 }
Calin Juravle52c48962014-12-16 17:02:57 +00004967 break;
4968 }
4969
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004970 case DataType::Type::kInt64: {
Calin Juravle52c48962014-12-16 17:02:57 +00004971 if (is_volatile) {
4972 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4973 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
4974 __ movd(temp1, value.AsRegisterPairLow<Register>());
4975 __ movd(temp2, value.AsRegisterPairHigh<Register>());
4976 __ punpckldq(temp1, temp2);
4977 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00004978 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05004979 } else if (value.IsConstant()) {
4980 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4981 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4982 codegen_->MaybeRecordImplicitNullCheck(instruction);
4983 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00004984 } else {
4985 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004986 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004987 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
4988 }
Mark Mendell81489372015-11-04 11:30:41 -05004989 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00004990 break;
4991 }
4992
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004993 case DataType::Type::kFloat32: {
Mark Mendell81489372015-11-04 11:30:41 -05004994 if (value.IsConstant()) {
4995 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4996 __ movl(Address(base, offset), Immediate(v));
4997 } else {
4998 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4999 }
Calin Juravle52c48962014-12-16 17:02:57 +00005000 break;
5001 }
5002
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005003 case DataType::Type::kFloat64: {
Mark Mendell81489372015-11-04 11:30:41 -05005004 if (value.IsConstant()) {
5005 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
5006 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
5007 codegen_->MaybeRecordImplicitNullCheck(instruction);
5008 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
5009 maybe_record_implicit_null_check_done = true;
5010 } else {
5011 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
5012 }
Calin Juravle52c48962014-12-16 17:02:57 +00005013 break;
5014 }
5015
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005016 case DataType::Type::kVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00005017 LOG(FATAL) << "Unreachable type " << field_type;
5018 UNREACHABLE();
5019 }
5020
Mark Mendell81489372015-11-04 11:30:41 -05005021 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005022 codegen_->MaybeRecordImplicitNullCheck(instruction);
5023 }
5024
Roland Levillain4d027112015-07-01 15:41:14 +01005025 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005026 Register temp = locations->GetTemp(0).AsRegister<Register>();
5027 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005028 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00005029 }
5030
Calin Juravle52c48962014-12-16 17:02:57 +00005031 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005032 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00005033 }
5034}
5035
5036void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5037 HandleFieldGet(instruction, instruction->GetFieldInfo());
5038}
5039
5040void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5041 HandleFieldGet(instruction, instruction->GetFieldInfo());
5042}
5043
5044void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5045 HandleFieldSet(instruction, instruction->GetFieldInfo());
5046}
5047
5048void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005049 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005050}
5051
5052void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5053 HandleFieldSet(instruction, instruction->GetFieldInfo());
5054}
5055
5056void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005057 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005058}
5059
5060void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5061 HandleFieldGet(instruction, instruction->GetFieldInfo());
5062}
5063
5064void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5065 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005066}
5067
Calin Juravlee460d1d2015-09-29 04:52:17 +01005068void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
5069 HUnresolvedInstanceFieldGet* instruction) {
5070 FieldAccessCallingConventionX86 calling_convention;
5071 codegen_->CreateUnresolvedFieldLocationSummary(
5072 instruction, instruction->GetFieldType(), calling_convention);
5073}
5074
5075void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
5076 HUnresolvedInstanceFieldGet* instruction) {
5077 FieldAccessCallingConventionX86 calling_convention;
5078 codegen_->GenerateUnresolvedFieldAccess(instruction,
5079 instruction->GetFieldType(),
5080 instruction->GetFieldIndex(),
5081 instruction->GetDexPc(),
5082 calling_convention);
5083}
5084
5085void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
5086 HUnresolvedInstanceFieldSet* instruction) {
5087 FieldAccessCallingConventionX86 calling_convention;
5088 codegen_->CreateUnresolvedFieldLocationSummary(
5089 instruction, instruction->GetFieldType(), calling_convention);
5090}
5091
5092void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
5093 HUnresolvedInstanceFieldSet* instruction) {
5094 FieldAccessCallingConventionX86 calling_convention;
5095 codegen_->GenerateUnresolvedFieldAccess(instruction,
5096 instruction->GetFieldType(),
5097 instruction->GetFieldIndex(),
5098 instruction->GetDexPc(),
5099 calling_convention);
5100}
5101
5102void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
5103 HUnresolvedStaticFieldGet* instruction) {
5104 FieldAccessCallingConventionX86 calling_convention;
5105 codegen_->CreateUnresolvedFieldLocationSummary(
5106 instruction, instruction->GetFieldType(), calling_convention);
5107}
5108
5109void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
5110 HUnresolvedStaticFieldGet* instruction) {
5111 FieldAccessCallingConventionX86 calling_convention;
5112 codegen_->GenerateUnresolvedFieldAccess(instruction,
5113 instruction->GetFieldType(),
5114 instruction->GetFieldIndex(),
5115 instruction->GetDexPc(),
5116 calling_convention);
5117}
5118
5119void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
5120 HUnresolvedStaticFieldSet* instruction) {
5121 FieldAccessCallingConventionX86 calling_convention;
5122 codegen_->CreateUnresolvedFieldLocationSummary(
5123 instruction, instruction->GetFieldType(), calling_convention);
5124}
5125
5126void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
5127 HUnresolvedStaticFieldSet* instruction) {
5128 FieldAccessCallingConventionX86 calling_convention;
5129 codegen_->GenerateUnresolvedFieldAccess(instruction,
5130 instruction->GetFieldType(),
5131 instruction->GetFieldIndex(),
5132 instruction->GetDexPc(),
5133 calling_convention);
5134}
5135
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005136void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005137 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5138 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
5139 ? Location::RequiresRegister()
5140 : Location::Any();
5141 locations->SetInAt(0, loc);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005142}
5143
Calin Juravle2ae48182016-03-16 14:05:09 +00005144void CodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
5145 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005146 return;
5147 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005148 LocationSummary* locations = instruction->GetLocations();
5149 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005150
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005151 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00005152 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005153}
5154
Calin Juravle2ae48182016-03-16 14:05:09 +00005155void CodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005156 SlowPathCode* slow_path = new (GetScopedAllocator()) NullCheckSlowPathX86(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005157 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005158
5159 LocationSummary* locations = instruction->GetLocations();
5160 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005161
5162 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04005163 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005164 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005165 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005166 } else {
5167 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00005168 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005169 __ jmp(slow_path->GetEntryLabel());
5170 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005171 }
5172 __ j(kEqual, slow_path->GetEntryLabel());
5173}
5174
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005175void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005176 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005177}
5178
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005179void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005180 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005181 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005182 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005183 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
5184 object_array_get_with_read_barrier
5185 ? LocationSummary::kCallOnSlowPath
5186 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005187 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005188 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005189 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005190 locations->SetInAt(0, Location::RequiresRegister());
5191 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005192 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005193 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5194 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005195 // The output overlaps in case of long: we don't want the low move
5196 // to overwrite the array's location. Likewise, in the case of an
5197 // object array get with read barriers enabled, we do not want the
5198 // move to overwrite the array's location, as we need it to emit
5199 // the read barrier.
5200 locations->SetOut(
5201 Location::RequiresRegister(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005202 (instruction->GetType() == DataType::Type::kInt64 || object_array_get_with_read_barrier)
5203 ? Location::kOutputOverlap
5204 : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005205 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005206}
5207
5208void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
5209 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005210 Location obj_loc = locations->InAt(0);
5211 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005212 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005213 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005214 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005215
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005216 DataType::Type type = instruction->GetType();
Calin Juravle77520bc2015-01-12 18:45:46 +00005217 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005218 case DataType::Type::kBool:
5219 case DataType::Type::kUint8: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005220 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005221 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005222 break;
5223 }
5224
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005225 case DataType::Type::kInt8: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005226 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005227 __ movsxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005228 break;
5229 }
5230
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005231 case DataType::Type::kUint16: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005232 Register out = out_loc.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07005233 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5234 // Branch cases into compressed and uncompressed for each index's type.
5235 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
5236 NearLabel done, not_compressed;
Vladimir Marko3c89d422017-02-17 11:30:23 +00005237 __ testb(Address(obj, count_offset), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005238 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005239 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
5240 "Expecting 0=compressed, 1=uncompressed");
5241 __ j(kNotZero, &not_compressed);
jessicahandojo4877b792016-09-08 19:49:13 -07005242 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
5243 __ jmp(&done);
5244 __ Bind(&not_compressed);
5245 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5246 __ Bind(&done);
5247 } else {
5248 // Common case for charAt of array of char or when string compression's
5249 // feature is turned off.
5250 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5251 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005252 break;
5253 }
5254
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005255 case DataType::Type::kInt16: {
5256 Register out = out_loc.AsRegister<Register>();
5257 __ movsxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5258 break;
5259 }
5260
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005261 case DataType::Type::kInt32: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005262 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005263 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005264 break;
5265 }
5266
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005267 case DataType::Type::kReference: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005268 static_assert(
5269 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5270 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00005271 // /* HeapReference<Object> */ out =
5272 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5273 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005274 // Note that a potential implicit null check is handled in this
5275 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
5276 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00005277 instruction, out_loc, obj, data_offset, index, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005278 } else {
5279 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005280 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
5281 codegen_->MaybeRecordImplicitNullCheck(instruction);
5282 // If read barriers are enabled, emit read barriers other than
5283 // Baker's using a slow path (and also unpoison the loaded
5284 // reference, if heap poisoning is enabled).
Roland Levillain7c1559a2015-12-15 10:55:36 +00005285 if (index.IsConstant()) {
5286 uint32_t offset =
5287 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005288 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5289 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005290 codegen_->MaybeGenerateReadBarrierSlow(
5291 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5292 }
5293 }
5294 break;
5295 }
5296
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005297 case DataType::Type::kInt64: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005298 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005299 __ movl(out_loc.AsRegisterPairLow<Register>(),
5300 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
5301 codegen_->MaybeRecordImplicitNullCheck(instruction);
5302 __ movl(out_loc.AsRegisterPairHigh<Register>(),
5303 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005304 break;
5305 }
5306
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005307 case DataType::Type::kFloat32: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005308 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005309 __ movss(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005310 break;
5311 }
5312
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005313 case DataType::Type::kFloat64: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005314 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005315 __ movsd(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005316 break;
5317 }
5318
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005319 case DataType::Type::kVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00005320 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005321 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005322 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005323
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005324 if (type == DataType::Type::kReference || type == DataType::Type::kInt64) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005325 // Potential implicit null checks, in the case of reference or
5326 // long arrays, are handled in the previous switch statement.
5327 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005328 codegen_->MaybeRecordImplicitNullCheck(instruction);
5329 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005330}
5331
5332void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005333 DataType::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005334
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005335 bool needs_write_barrier =
5336 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005337 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005338
Vladimir Markoca6fff82017-10-03 14:49:14 +01005339 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Nicolas Geoffray39468442014-09-02 15:17:15 +01005340 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01005341 may_need_runtime_call_for_type_check ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00005342 LocationSummary::kCallOnSlowPath :
5343 LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005344
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005345 bool is_byte_type = DataType::Size(value_type) == 1u;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005346 // We need the inputs to be different than the output in case of long operation.
5347 // In case of a byte operation, the register allocator does not support multiple
5348 // inputs that die at entry with one in a specific register.
5349 locations->SetInAt(0, Location::RequiresRegister());
5350 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5351 if (is_byte_type) {
5352 // Ensure the value is in a byte register.
5353 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005354 } else if (DataType::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05005355 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005356 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005357 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
5358 }
5359 if (needs_write_barrier) {
5360 // Temporary registers for the write barrier.
5361 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5362 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005363 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005364 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005365}
5366
5367void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
5368 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005369 Location array_loc = locations->InAt(0);
5370 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005371 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005372 Location value = locations->InAt(2);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005373 DataType::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005374 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5375 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5376 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005377 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005378 bool needs_write_barrier =
5379 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005380
5381 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005382 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005383 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005384 case DataType::Type::kInt8: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005385 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005386 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_1, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005387 if (value.IsRegister()) {
5388 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005389 } else {
Nicolas Geoffray78612082017-07-24 14:18:53 +01005390 __ movb(address, Immediate(CodeGenerator::GetInt8ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005391 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005392 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005393 break;
5394 }
5395
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005396 case DataType::Type::kUint16:
5397 case DataType::Type::kInt16: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005398 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005399 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_2, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005400 if (value.IsRegister()) {
5401 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005402 } else {
Nicolas Geoffray78612082017-07-24 14:18:53 +01005403 __ movw(address, Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005404 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005405 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005406 break;
5407 }
5408
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005409 case DataType::Type::kReference: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005410 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005411 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005412
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005413 if (!value.IsRegister()) {
5414 // Just setting null.
5415 DCHECK(instruction->InputAt(2)->IsNullConstant());
5416 DCHECK(value.IsConstant()) << value;
5417 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00005418 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005419 DCHECK(!needs_write_barrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005420 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005421 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005422 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005423
5424 DCHECK(needs_write_barrier);
5425 Register register_value = value.AsRegister<Register>();
Roland Levillain16d9f942016-08-25 17:27:56 +01005426 // We cannot use a NearLabel for `done`, as its range may be too
5427 // short when Baker read barriers are enabled.
5428 Label done;
5429 NearLabel not_null, do_put;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005430 SlowPathCode* slow_path = nullptr;
Roland Levillain16d9f942016-08-25 17:27:56 +01005431 Location temp_loc = locations->GetTemp(0);
5432 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005433 if (may_need_runtime_call_for_type_check) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005434 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathX86(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005435 codegen_->AddSlowPath(slow_path);
5436 if (instruction->GetValueCanBeNull()) {
5437 __ testl(register_value, register_value);
5438 __ j(kNotEqual, &not_null);
5439 __ movl(address, Immediate(0));
5440 codegen_->MaybeRecordImplicitNullCheck(instruction);
5441 __ jmp(&done);
5442 __ Bind(&not_null);
5443 }
5444
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005445 // Note that when Baker read barriers are enabled, the type
5446 // checks are performed without read barriers. This is fine,
5447 // even in the case where a class object is in the from-space
5448 // after the flip, as a comparison involving such a type would
5449 // not produce a false positive; it may of course produce a
5450 // false negative, in which case we would take the ArraySet
5451 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01005452
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005453 // /* HeapReference<Class> */ temp = array->klass_
5454 __ movl(temp, Address(array, class_offset));
5455 codegen_->MaybeRecordImplicitNullCheck(instruction);
5456 __ MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01005457
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005458 // /* HeapReference<Class> */ temp = temp->component_type_
5459 __ movl(temp, Address(temp, component_offset));
5460 // If heap poisoning is enabled, no need to unpoison `temp`
5461 // nor the object reference in `register_value->klass`, as
5462 // we are comparing two poisoned references.
5463 __ cmpl(temp, Address(register_value, class_offset));
Roland Levillain16d9f942016-08-25 17:27:56 +01005464
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005465 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5466 __ j(kEqual, &do_put);
5467 // If heap poisoning is enabled, the `temp` reference has
5468 // not been unpoisoned yet; unpoison it now.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005469 __ MaybeUnpoisonHeapReference(temp);
5470
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005471 // If heap poisoning is enabled, no need to unpoison the
5472 // heap reference loaded below, as it is only used for a
5473 // comparison with null.
5474 __ cmpl(Address(temp, super_offset), Immediate(0));
5475 __ j(kNotEqual, slow_path->GetEntryLabel());
5476 __ Bind(&do_put);
5477 } else {
5478 __ j(kNotEqual, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005479 }
5480 }
5481
5482 if (kPoisonHeapReferences) {
5483 __ movl(temp, register_value);
5484 __ PoisonHeapReference(temp);
5485 __ movl(address, temp);
5486 } else {
5487 __ movl(address, register_value);
5488 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005489 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005490 codegen_->MaybeRecordImplicitNullCheck(instruction);
5491 }
5492
5493 Register card = locations->GetTemp(1).AsRegister<Register>();
5494 codegen_->MarkGCCard(
5495 temp, card, array, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
5496 __ Bind(&done);
5497
5498 if (slow_path != nullptr) {
5499 __ Bind(slow_path->GetExitLabel());
5500 }
5501
5502 break;
5503 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005504
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005505 case DataType::Type::kInt32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005506 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005507 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005508 if (value.IsRegister()) {
5509 __ movl(address, value.AsRegister<Register>());
5510 } else {
5511 DCHECK(value.IsConstant()) << value;
5512 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5513 __ movl(address, Immediate(v));
5514 }
5515 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005516 break;
5517 }
5518
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005519 case DataType::Type::kInt64: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005520 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005521 if (value.IsRegisterPair()) {
5522 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
5523 value.AsRegisterPairLow<Register>());
5524 codegen_->MaybeRecordImplicitNullCheck(instruction);
5525 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
5526 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005527 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005528 DCHECK(value.IsConstant());
5529 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
5530 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
5531 Immediate(Low32Bits(val)));
5532 codegen_->MaybeRecordImplicitNullCheck(instruction);
5533 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
5534 Immediate(High32Bits(val)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005535 }
5536 break;
5537 }
5538
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005539 case DataType::Type::kFloat32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005540 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005541 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005542 if (value.IsFpuRegister()) {
5543 __ movss(address, value.AsFpuRegister<XmmRegister>());
5544 } else {
5545 DCHECK(value.IsConstant());
5546 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
5547 __ movl(address, Immediate(v));
5548 }
5549 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005550 break;
5551 }
5552
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005553 case DataType::Type::kFloat64: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005554 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005555 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005556 if (value.IsFpuRegister()) {
5557 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5558 } else {
5559 DCHECK(value.IsConstant());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005560 Address address_hi =
5561 CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset + kX86WordSize);
Mark Mendell81489372015-11-04 11:30:41 -05005562 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5563 __ movl(address, Immediate(Low32Bits(v)));
5564 codegen_->MaybeRecordImplicitNullCheck(instruction);
5565 __ movl(address_hi, Immediate(High32Bits(v)));
5566 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005567 break;
5568 }
5569
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005570 case DataType::Type::kVoid:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005571 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005572 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005573 }
5574}
5575
5576void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005577 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005578 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04005579 if (!instruction->IsEmittedAtUseSite()) {
5580 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5581 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005582}
5583
5584void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04005585 if (instruction->IsEmittedAtUseSite()) {
5586 return;
5587 }
5588
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005589 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005590 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005591 Register obj = locations->InAt(0).AsRegister<Register>();
5592 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005593 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005594 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo4877b792016-09-08 19:49:13 -07005595 // Mask out most significant bit in case the array is String's array of char.
5596 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005597 __ shrl(out, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005598 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005599}
5600
5601void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005602 RegisterSet caller_saves = RegisterSet::Empty();
5603 InvokeRuntimeCallingConvention calling_convention;
5604 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5605 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5606 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005607 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04005608 HInstruction* length = instruction->InputAt(1);
5609 if (!length->IsEmittedAtUseSite()) {
5610 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5611 }
jessicahandojo4877b792016-09-08 19:49:13 -07005612 // Need register to see array's length.
5613 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5614 locations->AddTemp(Location::RequiresRegister());
5615 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005616}
5617
5618void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
jessicahandojo4877b792016-09-08 19:49:13 -07005619 const bool is_string_compressed_char_at =
5620 mirror::kUseStringCompression && instruction->IsStringCharAt();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005621 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005622 Location index_loc = locations->InAt(0);
5623 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07005624 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01005625 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005626
Mark Mendell99dbd682015-04-22 16:18:52 -04005627 if (length_loc.IsConstant()) {
5628 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5629 if (index_loc.IsConstant()) {
5630 // BCE will remove the bounds check if we are guarenteed to pass.
5631 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5632 if (index < 0 || index >= length) {
5633 codegen_->AddSlowPath(slow_path);
5634 __ jmp(slow_path->GetEntryLabel());
5635 } else {
5636 // Some optimization after BCE may have generated this, and we should not
5637 // generate a bounds check if it is a valid range.
5638 }
5639 return;
5640 }
5641
5642 // We have to reverse the jump condition because the length is the constant.
5643 Register index_reg = index_loc.AsRegister<Register>();
5644 __ cmpl(index_reg, Immediate(length));
5645 codegen_->AddSlowPath(slow_path);
5646 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005647 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04005648 HInstruction* array_length = instruction->InputAt(1);
5649 if (array_length->IsEmittedAtUseSite()) {
5650 // Address the length field in the array.
5651 DCHECK(array_length->IsArrayLength());
5652 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
5653 Location array_loc = array_length->GetLocations()->InAt(0);
5654 Address array_len(array_loc.AsRegister<Register>(), len_offset);
jessicahandojo4877b792016-09-08 19:49:13 -07005655 if (is_string_compressed_char_at) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005656 // TODO: if index_loc.IsConstant(), compare twice the index (to compensate for
5657 // the string compression flag) with the in-memory length and avoid the temporary.
jessicahandojo4877b792016-09-08 19:49:13 -07005658 Register length_reg = locations->GetTemp(0).AsRegister<Register>();
5659 __ movl(length_reg, array_len);
5660 codegen_->MaybeRecordImplicitNullCheck(array_length);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005661 __ shrl(length_reg, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005662 codegen_->GenerateIntCompare(length_reg, index_loc);
Mark Mendellee8d9712016-07-12 11:13:15 -04005663 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07005664 // Checking bounds for general case:
5665 // Array of char or string's array with feature compression off.
5666 if (index_loc.IsConstant()) {
5667 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5668 __ cmpl(array_len, Immediate(value));
5669 } else {
5670 __ cmpl(array_len, index_loc.AsRegister<Register>());
5671 }
5672 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendellee8d9712016-07-12 11:13:15 -04005673 }
Mark Mendell99dbd682015-04-22 16:18:52 -04005674 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005675 codegen_->GenerateIntCompare(length_loc, index_loc);
Mark Mendell99dbd682015-04-22 16:18:52 -04005676 }
5677 codegen_->AddSlowPath(slow_path);
5678 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005679 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005680}
5681
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005682void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005683 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005684}
5685
5686void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01005687 if (instruction->GetNext()->IsSuspendCheck() &&
5688 instruction->GetBlock()->GetLoopInformation() != nullptr) {
5689 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
5690 // The back edge will generate the suspend check.
5691 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
5692 }
5693
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005694 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5695}
5696
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005697void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005698 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5699 instruction, LocationSummary::kCallOnSlowPath);
Aart Bikb13c65b2017-03-21 20:14:07 -07005700 // In suspend check slow path, usually there are no caller-save registers at all.
5701 // If SIMD instructions are present, however, we force spilling all live SIMD
5702 // registers in full width (since the runtime only saves/restores lower part).
Aart Bik5576f372017-03-23 16:17:37 -07005703 locations->SetCustomSlowPathCallerSaves(
5704 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005705}
5706
5707void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005708 HBasicBlock* block = instruction->GetBlock();
5709 if (block->GetLoopInformation() != nullptr) {
5710 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5711 // The back edge will generate the suspend check.
5712 return;
5713 }
5714 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5715 // The goto will generate the suspend check.
5716 return;
5717 }
5718 GenerateSuspendCheck(instruction, nullptr);
5719}
5720
5721void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
5722 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005723 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005724 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
5725 if (slow_path == nullptr) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005726 slow_path =
5727 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005728 instruction->SetSlowPath(slow_path);
5729 codegen_->AddSlowPath(slow_path);
5730 if (successor != nullptr) {
5731 DCHECK(successor->IsLoopHeader());
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005732 }
5733 } else {
5734 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5735 }
5736
Andreas Gampe542451c2016-07-26 09:02:02 -07005737 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86PointerSize>().Int32Value()),
Roland Levillain7c1559a2015-12-15 10:55:36 +00005738 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005739 if (successor == nullptr) {
5740 __ j(kNotEqual, slow_path->GetEntryLabel());
5741 __ Bind(slow_path->GetReturnLabel());
5742 } else {
5743 __ j(kEqual, codegen_->GetLabelOf(successor));
5744 __ jmp(slow_path->GetEntryLabel());
5745 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005746}
5747
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005748X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
5749 return codegen_->GetAssembler();
5750}
5751
Mark Mendell7c8d0092015-01-26 11:21:33 -05005752void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005753 ScratchRegisterScope ensure_scratch(
5754 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5755 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5756 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5757 __ movl(temp_reg, Address(ESP, src + stack_offset));
5758 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005759}
5760
5761void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005762 ScratchRegisterScope ensure_scratch(
5763 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5764 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5765 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5766 __ movl(temp_reg, Address(ESP, src + stack_offset));
5767 __ movl(Address(ESP, dst + stack_offset), temp_reg);
5768 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
5769 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005770}
5771
5772void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005773 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005774 Location source = move->GetSource();
5775 Location destination = move->GetDestination();
5776
5777 if (source.IsRegister()) {
5778 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005779 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005780 } else if (destination.IsFpuRegister()) {
5781 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005782 } else {
5783 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005784 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005785 }
David Brazdil74eb1b22015-12-14 11:44:01 +00005786 } else if (source.IsRegisterPair()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005787 size_t elem_size = DataType::Size(DataType::Type::kInt32);
David Brazdil74eb1b22015-12-14 11:44:01 +00005788 // Create stack space for 2 elements.
5789 __ subl(ESP, Immediate(2 * elem_size));
5790 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
5791 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
5792 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
5793 // And remove the temporary stack space we allocated.
5794 __ addl(ESP, Immediate(2 * elem_size));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005795 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005796 if (destination.IsRegister()) {
5797 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
5798 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005799 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005800 } else if (destination.IsRegisterPair()) {
5801 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
5802 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
5803 __ psrlq(src_reg, Immediate(32));
5804 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005805 } else if (destination.IsStackSlot()) {
5806 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07005807 } else if (destination.IsDoubleStackSlot()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005808 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07005809 } else {
5810 DCHECK(destination.IsSIMDStackSlot());
5811 __ movups(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005812 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005813 } else if (source.IsStackSlot()) {
5814 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005815 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005816 } else if (destination.IsFpuRegister()) {
5817 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005818 } else {
5819 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005820 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
5821 }
5822 } else if (source.IsDoubleStackSlot()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005823 if (destination.IsRegisterPair()) {
5824 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
5825 __ movl(destination.AsRegisterPairHigh<Register>(),
5826 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
5827 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005828 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
5829 } else {
5830 DCHECK(destination.IsDoubleStackSlot()) << destination;
5831 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005832 }
Aart Bik5576f372017-03-23 16:17:37 -07005833 } else if (source.IsSIMDStackSlot()) {
5834 DCHECK(destination.IsFpuRegister());
5835 __ movups(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005836 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005837 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005838 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005839 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005840 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005841 if (value == 0) {
5842 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
5843 } else {
5844 __ movl(destination.AsRegister<Register>(), Immediate(value));
5845 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005846 } else {
5847 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05005848 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005849 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005850 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005851 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005852 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005853 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005854 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005855 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5856 if (value == 0) {
5857 // Easy handling of 0.0.
5858 __ xorps(dest, dest);
5859 } else {
5860 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005861 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5862 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
5863 __ movl(temp, Immediate(value));
5864 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005865 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005866 } else {
5867 DCHECK(destination.IsStackSlot()) << destination;
5868 __ movl(Address(ESP, destination.GetStackIndex()), imm);
5869 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005870 } else if (constant->IsLongConstant()) {
5871 int64_t value = constant->AsLongConstant()->GetValue();
5872 int32_t low_value = Low32Bits(value);
5873 int32_t high_value = High32Bits(value);
5874 Immediate low(low_value);
5875 Immediate high(high_value);
5876 if (destination.IsDoubleStackSlot()) {
5877 __ movl(Address(ESP, destination.GetStackIndex()), low);
5878 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5879 } else {
5880 __ movl(destination.AsRegisterPairLow<Register>(), low);
5881 __ movl(destination.AsRegisterPairHigh<Register>(), high);
5882 }
5883 } else {
5884 DCHECK(constant->IsDoubleConstant());
5885 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005886 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005887 int32_t low_value = Low32Bits(value);
5888 int32_t high_value = High32Bits(value);
5889 Immediate low(low_value);
5890 Immediate high(high_value);
5891 if (destination.IsFpuRegister()) {
5892 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5893 if (value == 0) {
5894 // Easy handling of 0.0.
5895 __ xorpd(dest, dest);
5896 } else {
5897 __ pushl(high);
5898 __ pushl(low);
5899 __ movsd(dest, Address(ESP, 0));
5900 __ addl(ESP, Immediate(8));
5901 }
5902 } else {
5903 DCHECK(destination.IsDoubleStackSlot()) << destination;
5904 __ movl(Address(ESP, destination.GetStackIndex()), low);
5905 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5906 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005907 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005908 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005909 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005910 }
5911}
5912
Mark Mendella5c19ce2015-04-01 12:51:05 -04005913void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005914 Register suggested_scratch = reg == EAX ? EBX : EAX;
5915 ScratchRegisterScope ensure_scratch(
5916 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
5917
5918 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5919 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
5920 __ movl(Address(ESP, mem + stack_offset), reg);
5921 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005922}
5923
Mark Mendell7c8d0092015-01-26 11:21:33 -05005924void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005925 ScratchRegisterScope ensure_scratch(
5926 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5927
5928 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5929 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5930 __ movl(temp_reg, Address(ESP, mem + stack_offset));
5931 __ movss(Address(ESP, mem + stack_offset), reg);
5932 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005933}
5934
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005935void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005936 ScratchRegisterScope ensure_scratch1(
5937 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005938
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005939 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
5940 ScratchRegisterScope ensure_scratch2(
5941 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005942
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005943 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
5944 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
5945 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
5946 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
5947 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
5948 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005949}
5950
5951void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005952 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005953 Location source = move->GetSource();
5954 Location destination = move->GetDestination();
5955
5956 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04005957 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
5958 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
5959 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
5960 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
5961 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005962 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005963 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005964 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005965 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005966 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5967 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005968 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
5969 // Use XOR Swap algorithm to avoid a temporary.
5970 DCHECK_NE(source.reg(), destination.reg());
5971 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5972 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5973 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5974 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
5975 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
5976 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
5977 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005978 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
5979 // Take advantage of the 16 bytes in the XMM register.
5980 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
5981 Address stack(ESP, destination.GetStackIndex());
5982 // Load the double into the high doubleword.
5983 __ movhpd(reg, stack);
5984
5985 // Store the low double into the destination.
5986 __ movsd(stack, reg);
5987
5988 // Move the high double to the low double.
5989 __ psrldq(reg, Immediate(8));
5990 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
5991 // Take advantage of the 16 bytes in the XMM register.
5992 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
5993 Address stack(ESP, source.GetStackIndex());
5994 // Load the double into the high doubleword.
5995 __ movhpd(reg, stack);
5996
5997 // Store the low double into the destination.
5998 __ movsd(stack, reg);
5999
6000 // Move the high double to the low double.
6001 __ psrldq(reg, Immediate(8));
6002 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
6003 Exchange(destination.GetStackIndex(), source.GetStackIndex());
6004 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006005 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006006 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006007 }
6008}
6009
6010void ParallelMoveResolverX86::SpillScratch(int reg) {
6011 __ pushl(static_cast<Register>(reg));
6012}
6013
6014void ParallelMoveResolverX86::RestoreScratch(int reg) {
6015 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006016}
6017
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006018HLoadClass::LoadKind CodeGeneratorX86::GetSupportedLoadClassKind(
6019 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006020 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006021 case HLoadClass::LoadKind::kInvalid:
6022 LOG(FATAL) << "UNREACHABLE";
6023 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006024 case HLoadClass::LoadKind::kReferrersClass:
6025 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006026 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006027 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006028 case HLoadClass::LoadKind::kBssEntry:
Vladimir Marko764d4542017-05-16 10:31:41 +01006029 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006030 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006031 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006032 DCHECK(Runtime::Current()->UseJitCompilation());
6033 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01006034 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006035 case HLoadClass::LoadKind::kRuntimeCall:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006036 break;
6037 }
6038 return desired_class_load_kind;
6039}
6040
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006041void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00006042 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006043 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006044 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00006045 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006046 cls,
6047 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00006048 Location::RegisterLocation(EAX));
Vladimir Markoea4c1262017-02-06 19:59:33 +00006049 DCHECK_EQ(calling_convention.GetRegisterAt(0), EAX);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006050 return;
6051 }
Vladimir Marko41559982017-01-06 14:04:23 +00006052 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006053
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006054 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
6055 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006056 ? LocationSummary::kCallOnSlowPath
6057 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01006058 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006059 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006060 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006061 }
6062
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006063 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006064 load_kind == HLoadClass::LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006065 load_kind == HLoadClass::LoadKind::kBootImageClassTable ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006066 load_kind == HLoadClass::LoadKind::kBssEntry) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006067 locations->SetInAt(0, Location::RequiresRegister());
6068 }
6069 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006070 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
6071 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6072 // Rely on the type resolution and/or initialization to save everything.
6073 RegisterSet caller_saves = RegisterSet::Empty();
6074 InvokeRuntimeCallingConvention calling_convention;
6075 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6076 locations->SetCustomSlowPathCallerSaves(caller_saves);
6077 } else {
6078 // For non-Baker read barrier we have a temp-clobbering call.
6079 }
6080 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006081}
6082
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006083Label* CodeGeneratorX86::NewJitRootClassPatch(const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01006084 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006085 Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006086 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006087 // Add a patch entry and return the label.
Vladimir Marko174b2e22017-10-12 13:34:49 +01006088 jit_class_patches_.emplace_back(dex_file, type_index.index_);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006089 PatchInfo<Label>* info = &jit_class_patches_.back();
6090 return &info->label;
6091}
6092
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006093// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6094// move.
6095void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00006096 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006097 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00006098 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01006099 return;
6100 }
Vladimir Marko41559982017-01-06 14:04:23 +00006101 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01006102
Vladimir Marko41559982017-01-06 14:04:23 +00006103 LocationSummary* locations = cls->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006104 Location out_loc = locations->Out();
6105 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006106
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006107 bool generate_null_check = false;
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006108 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
6109 ? kWithoutReadBarrier
6110 : kCompilerReadBarrierOption;
Vladimir Marko41559982017-01-06 14:04:23 +00006111 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006112 case HLoadClass::LoadKind::kReferrersClass: {
6113 DCHECK(!cls->CanCallRuntime());
6114 DCHECK(!cls->MustGenerateClinitCheck());
6115 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6116 Register current_method = locations->InAt(0).AsRegister<Register>();
6117 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006118 cls,
6119 out_loc,
6120 Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()),
Roland Levillain00468f32016-10-27 18:02:48 +01006121 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006122 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006123 break;
6124 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006125 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006126 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006127 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006128 Register method_address = locations->InAt(0).AsRegister<Register>();
6129 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Marko1998cd02017-01-13 13:02:58 +00006130 codegen_->RecordBootTypePatch(cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006131 break;
6132 }
6133 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006134 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006135 uint32_t address = dchecked_integral_cast<uint32_t>(
6136 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
6137 DCHECK_NE(address, 0u);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006138 __ movl(out, Immediate(address));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006139 break;
6140 }
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006141 case HLoadClass::LoadKind::kBootImageClassTable: {
6142 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6143 Register method_address = locations->InAt(0).AsRegister<Register>();
6144 __ movl(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
6145 codegen_->RecordBootTypePatch(cls);
6146 // Extract the reference from the slot data, i.e. clear the hash bits.
6147 int32_t masked_hash = ClassTable::TableSlot::MaskHash(
6148 ComputeModifiedUtf8Hash(cls->GetDexFile().StringByTypeIdx(cls->GetTypeIndex())));
6149 if (masked_hash != 0) {
6150 __ subl(out, Immediate(masked_hash));
6151 }
6152 break;
6153 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006154 case HLoadClass::LoadKind::kBssEntry: {
6155 Register method_address = locations->InAt(0).AsRegister<Register>();
6156 Address address(method_address, CodeGeneratorX86::kDummy32BitOffset);
6157 Label* fixup_label = codegen_->NewTypeBssEntryPatch(cls);
6158 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
6159 generate_null_check = true;
6160 break;
6161 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006162 case HLoadClass::LoadKind::kJitTableAddress: {
6163 Address address = Address::Absolute(CodeGeneratorX86::kDummy32BitOffset);
6164 Label* fixup_label = codegen_->NewJitRootClassPatch(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006165 cls->GetDexFile(), cls->GetTypeIndex(), cls->GetClass());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006166 // /* GcRoot<mirror::Class> */ out = *address
Vladimir Markoea4c1262017-02-06 19:59:33 +00006167 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006168 break;
6169 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006170 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006171 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00006172 LOG(FATAL) << "UNREACHABLE";
6173 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006174 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006175
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006176 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6177 DCHECK(cls->CanCallRuntime());
Vladimir Marko174b2e22017-10-12 13:34:49 +01006178 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathX86(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006179 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
6180 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006181
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006182 if (generate_null_check) {
6183 __ testl(out, out);
6184 __ j(kEqual, slow_path->GetEntryLabel());
6185 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006186
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006187 if (cls->MustGenerateClinitCheck()) {
6188 GenerateClassInitializationCheck(slow_path, out);
6189 } else {
6190 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006191 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006192 }
6193}
6194
6195void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
6196 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006197 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006198 locations->SetInAt(0, Location::RequiresRegister());
6199 if (check->HasUses()) {
6200 locations->SetOut(Location::SameAsFirstInput());
6201 }
6202}
6203
6204void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006205 // We assume the class to not be null.
Vladimir Marko174b2e22017-10-12 13:34:49 +01006206 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006207 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006208 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00006209 GenerateClassInitializationCheck(slow_path,
6210 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006211}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006212
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006213void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07006214 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006215 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
6216 Immediate(mirror::Class::kStatusInitialized));
6217 __ j(kLess, slow_path->GetEntryLabel());
6218 __ Bind(slow_path->GetExitLabel());
6219 // No need for memory fence, thanks to the X86 memory model.
6220}
6221
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006222HLoadString::LoadKind CodeGeneratorX86::GetSupportedLoadStringKind(
6223 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006224 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006225 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006226 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00006227 case HLoadString::LoadKind::kBssEntry:
Vladimir Marko764d4542017-05-16 10:31:41 +01006228 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006229 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006230 case HLoadString::LoadKind::kJitTableAddress:
6231 DCHECK(Runtime::Current()->UseJitCompilation());
6232 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01006233 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006234 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006235 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006236 }
6237 return desired_string_load_kind;
6238}
6239
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006240void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006241 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01006242 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006243 HLoadString::LoadKind load_kind = load->GetLoadKind();
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006244 if (load_kind == HLoadString::LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006245 load_kind == HLoadString::LoadKind::kBootImageInternTable ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00006246 load_kind == HLoadString::LoadKind::kBssEntry) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006247 locations->SetInAt(0, Location::RequiresRegister());
6248 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006249 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006250 locations->SetOut(Location::RegisterLocation(EAX));
6251 } else {
6252 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006253 if (load_kind == HLoadString::LoadKind::kBssEntry) {
6254 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00006255 // Rely on the pResolveString to save everything.
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006256 RegisterSet caller_saves = RegisterSet::Empty();
6257 InvokeRuntimeCallingConvention calling_convention;
6258 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6259 locations->SetCustomSlowPathCallerSaves(caller_saves);
6260 } else {
6261 // For non-Baker read barrier we have a temp-clobbering call.
6262 }
6263 }
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006264 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006265}
6266
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006267Label* CodeGeneratorX86::NewJitRootStringPatch(const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01006268 dex::StringIndex string_index,
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006269 Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006270 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006271 // Add a patch entry and return the label.
Vladimir Marko174b2e22017-10-12 13:34:49 +01006272 jit_string_patches_.emplace_back(dex_file, string_index.index_);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006273 PatchInfo<Label>* info = &jit_string_patches_.back();
6274 return &info->label;
6275}
6276
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006277// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6278// move.
6279void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006280 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006281 Location out_loc = locations->Out();
6282 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006283
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006284 switch (load->GetLoadKind()) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006285 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006286 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006287 Register method_address = locations->InAt(0).AsRegister<Register>();
6288 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Markoaad75c62016-10-03 08:46:48 +00006289 codegen_->RecordBootStringPatch(load);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006290 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006291 }
6292 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006293 uint32_t address = dchecked_integral_cast<uint32_t>(
6294 reinterpret_cast<uintptr_t>(load->GetString().Get()));
6295 DCHECK_NE(address, 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006296 __ movl(out, Immediate(address));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006297 return;
6298 }
6299 case HLoadString::LoadKind::kBootImageInternTable: {
6300 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6301 Register method_address = locations->InAt(0).AsRegister<Register>();
6302 __ movl(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
6303 codegen_->RecordBootStringPatch(load);
6304 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006305 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00006306 case HLoadString::LoadKind::kBssEntry: {
6307 Register method_address = locations->InAt(0).AsRegister<Register>();
6308 Address address = Address(method_address, CodeGeneratorX86::kDummy32BitOffset);
6309 Label* fixup_label = codegen_->NewStringBssEntryPatch(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006310 // /* GcRoot<mirror::String> */ out = *address /* PC-relative */
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006311 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
Vladimir Marko174b2e22017-10-12 13:34:49 +01006312 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadStringSlowPathX86(load);
Vladimir Markoaad75c62016-10-03 08:46:48 +00006313 codegen_->AddSlowPath(slow_path);
6314 __ testl(out, out);
6315 __ j(kEqual, slow_path->GetEntryLabel());
6316 __ Bind(slow_path->GetExitLabel());
6317 return;
6318 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006319 case HLoadString::LoadKind::kJitTableAddress: {
6320 Address address = Address::Absolute(CodeGeneratorX86::kDummy32BitOffset);
6321 Label* fixup_label = codegen_->NewJitRootStringPatch(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006322 load->GetDexFile(), load->GetStringIndex(), load->GetString());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006323 // /* GcRoot<mirror::String> */ out = *address
6324 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
6325 return;
6326 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006327 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006328 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006329 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006330
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006331 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006332 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006333 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006334 __ movl(calling_convention.GetRegisterAt(0), Immediate(load->GetStringIndex().index_));
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006335 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6336 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006337}
6338
David Brazdilcb1c0552015-08-04 16:22:25 +01006339static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006340 return Address::Absolute(Thread::ExceptionOffset<kX86PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01006341}
6342
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006343void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
6344 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006345 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006346 locations->SetOut(Location::RequiresRegister());
6347}
6348
6349void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01006350 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
6351}
6352
6353void LocationsBuilderX86::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006354 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
David Brazdilcb1c0552015-08-04 16:22:25 +01006355}
6356
6357void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6358 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006359}
6360
6361void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006362 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6363 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006364 InvokeRuntimeCallingConvention calling_convention;
6365 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6366}
6367
6368void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006369 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006370 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006371}
6372
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006373// Temp is used for read barrier.
6374static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
6375 if (kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00006376 !kUseBakerReadBarrier &&
6377 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain7c1559a2015-12-15 10:55:36 +00006378 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006379 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
6380 return 1;
6381 }
6382 return 0;
6383}
6384
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006385// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006386// interface pointer, one for loading the current interface.
6387// The other checks have one temp for loading the object's class.
6388static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
6389 if (type_check_kind == TypeCheckKind::kInterfaceCheck && !kPoisonHeapReferences) {
6390 return 2;
6391 }
6392 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006393}
6394
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006395void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006396 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006397 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01006398 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006399 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006400 case TypeCheckKind::kExactCheck:
6401 case TypeCheckKind::kAbstractClassCheck:
6402 case TypeCheckKind::kClassHierarchyCheck:
6403 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006404 call_kind =
6405 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01006406 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006407 break;
6408 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006409 case TypeCheckKind::kUnresolvedCheck:
6410 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006411 call_kind = LocationSummary::kCallOnSlowPath;
6412 break;
6413 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006414
Vladimir Markoca6fff82017-10-03 14:49:14 +01006415 LocationSummary* locations =
6416 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01006417 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006418 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006419 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006420 locations->SetInAt(0, Location::RequiresRegister());
6421 locations->SetInAt(1, Location::Any());
6422 // Note that TypeCheckSlowPathX86 uses this "out" register too.
6423 locations->SetOut(Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006424 // When read barriers are enabled, we need a temporary register for some cases.
6425 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006426}
6427
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006428void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006429 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006430 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006431 Location obj_loc = locations->InAt(0);
6432 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006433 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006434 Location out_loc = locations->Out();
6435 Register out = out_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006436 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
6437 DCHECK_LE(num_temps, 1u);
6438 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006439 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006440 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6441 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6442 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07006443 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006444 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006445
6446 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006447 // Avoid null check if we know obj is not null.
6448 if (instruction->MustDoNullCheck()) {
6449 __ testl(obj, obj);
6450 __ j(kEqual, &zero);
6451 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006452
Roland Levillain7c1559a2015-12-15 10:55:36 +00006453 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006454 case TypeCheckKind::kExactCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006455 // /* HeapReference<Class> */ out = obj->klass_
6456 GenerateReferenceLoadTwoRegisters(instruction,
6457 out_loc,
6458 obj_loc,
6459 class_offset,
6460 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006461 if (cls.IsRegister()) {
6462 __ cmpl(out, cls.AsRegister<Register>());
6463 } else {
6464 DCHECK(cls.IsStackSlot()) << cls;
6465 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6466 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006467
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006468 // Classes must be equal for the instanceof to succeed.
6469 __ j(kNotEqual, &zero);
6470 __ movl(out, Immediate(1));
6471 __ jmp(&done);
6472 break;
6473 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006474
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006475 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006476 // /* HeapReference<Class> */ out = obj->klass_
6477 GenerateReferenceLoadTwoRegisters(instruction,
6478 out_loc,
6479 obj_loc,
6480 class_offset,
6481 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006482 // If the class is abstract, we eagerly fetch the super class of the
6483 // object to avoid doing a comparison we know will fail.
6484 NearLabel loop;
6485 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006486 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006487 GenerateReferenceLoadOneRegister(instruction,
6488 out_loc,
6489 super_offset,
6490 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006491 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006492 __ testl(out, out);
6493 // If `out` is null, we use it for the result, and jump to `done`.
6494 __ j(kEqual, &done);
6495 if (cls.IsRegister()) {
6496 __ cmpl(out, cls.AsRegister<Register>());
6497 } else {
6498 DCHECK(cls.IsStackSlot()) << cls;
6499 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6500 }
6501 __ j(kNotEqual, &loop);
6502 __ movl(out, Immediate(1));
6503 if (zero.IsLinked()) {
6504 __ jmp(&done);
6505 }
6506 break;
6507 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006508
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006509 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006510 // /* HeapReference<Class> */ out = obj->klass_
6511 GenerateReferenceLoadTwoRegisters(instruction,
6512 out_loc,
6513 obj_loc,
6514 class_offset,
6515 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006516 // Walk over the class hierarchy to find a match.
6517 NearLabel loop, success;
6518 __ Bind(&loop);
6519 if (cls.IsRegister()) {
6520 __ cmpl(out, cls.AsRegister<Register>());
6521 } else {
6522 DCHECK(cls.IsStackSlot()) << cls;
6523 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6524 }
6525 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006526 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006527 GenerateReferenceLoadOneRegister(instruction,
6528 out_loc,
6529 super_offset,
6530 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006531 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006532 __ testl(out, out);
6533 __ j(kNotEqual, &loop);
6534 // If `out` is null, we use it for the result, and jump to `done`.
6535 __ jmp(&done);
6536 __ Bind(&success);
6537 __ movl(out, Immediate(1));
6538 if (zero.IsLinked()) {
6539 __ jmp(&done);
6540 }
6541 break;
6542 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006543
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006544 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006545 // /* HeapReference<Class> */ out = obj->klass_
6546 GenerateReferenceLoadTwoRegisters(instruction,
6547 out_loc,
6548 obj_loc,
6549 class_offset,
6550 kCompilerReadBarrierOption);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006551 // Do an exact check.
6552 NearLabel exact_check;
6553 if (cls.IsRegister()) {
6554 __ cmpl(out, cls.AsRegister<Register>());
6555 } else {
6556 DCHECK(cls.IsStackSlot()) << cls;
6557 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6558 }
6559 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006560 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006561 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006562 GenerateReferenceLoadOneRegister(instruction,
6563 out_loc,
6564 component_offset,
6565 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006566 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006567 __ testl(out, out);
6568 // If `out` is null, we use it for the result, and jump to `done`.
6569 __ j(kEqual, &done);
6570 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
6571 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006572 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006573 __ movl(out, Immediate(1));
6574 __ jmp(&done);
6575 break;
6576 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006577
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006578 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006579 // No read barrier since the slow path will retry upon failure.
6580 // /* HeapReference<Class> */ out = obj->klass_
6581 GenerateReferenceLoadTwoRegisters(instruction,
6582 out_loc,
6583 obj_loc,
6584 class_offset,
6585 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006586 if (cls.IsRegister()) {
6587 __ cmpl(out, cls.AsRegister<Register>());
6588 } else {
6589 DCHECK(cls.IsStackSlot()) << cls;
6590 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6591 }
6592 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01006593 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
6594 instruction, /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006595 codegen_->AddSlowPath(slow_path);
6596 __ j(kNotEqual, slow_path->GetEntryLabel());
6597 __ movl(out, Immediate(1));
6598 if (zero.IsLinked()) {
6599 __ jmp(&done);
6600 }
6601 break;
6602 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006603
Calin Juravle98893e12015-10-02 21:05:03 +01006604 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006605 case TypeCheckKind::kInterfaceCheck: {
6606 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006607 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00006608 // cases.
6609 //
6610 // We cannot directly call the InstanceofNonTrivial runtime
6611 // entry point without resorting to a type checking slow path
6612 // here (i.e. by calling InvokeRuntime directly), as it would
6613 // require to assign fixed registers for the inputs of this
6614 // HInstanceOf instruction (following the runtime calling
6615 // convention), which might be cluttered by the potential first
6616 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006617 //
6618 // TODO: Introduce a new runtime entry point taking the object
6619 // to test (instead of its class) as argument, and let it deal
6620 // with the read barrier issues. This will let us refactor this
6621 // case of the `switch` code as it was previously (with a direct
6622 // call to the runtime not using a type checking slow path).
6623 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006624 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01006625 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
6626 instruction, /* is_fatal */ false);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006627 codegen_->AddSlowPath(slow_path);
6628 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006629 if (zero.IsLinked()) {
6630 __ jmp(&done);
6631 }
6632 break;
6633 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006634 }
6635
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006636 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006637 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006638 __ xorl(out, out);
6639 }
6640
6641 if (done.IsLinked()) {
6642 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006643 }
6644
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006645 if (slow_path != nullptr) {
6646 __ Bind(slow_path->GetExitLabel());
6647 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006648}
6649
Mathieu Chartier5ac321b2016-11-09 16:33:54 -08006650static bool IsTypeCheckSlowPathFatal(TypeCheckKind type_check_kind, bool throws_into_catch) {
6651 switch (type_check_kind) {
6652 case TypeCheckKind::kExactCheck:
6653 case TypeCheckKind::kAbstractClassCheck:
6654 case TypeCheckKind::kClassHierarchyCheck:
6655 case TypeCheckKind::kArrayObjectCheck:
6656 return !throws_into_catch && !kEmitCompilerReadBarrier;
6657 case TypeCheckKind::kInterfaceCheck:
6658 return !throws_into_catch && !kEmitCompilerReadBarrier && !kPoisonHeapReferences;
6659 case TypeCheckKind::kArrayCheck:
6660 case TypeCheckKind::kUnresolvedCheck:
6661 return false;
6662 }
6663 LOG(FATAL) << "Unreachable";
6664 UNREACHABLE();
6665}
6666
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006667void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006668 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006669 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Mathieu Chartier5ac321b2016-11-09 16:33:54 -08006670 LocationSummary::CallKind call_kind =
6671 IsTypeCheckSlowPathFatal(type_check_kind, throws_into_catch)
6672 ? LocationSummary::kNoCall
6673 : LocationSummary::kCallOnSlowPath;
Vladimir Markoca6fff82017-10-03 14:49:14 +01006674 LocationSummary* locations =
6675 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006676 locations->SetInAt(0, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006677 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
6678 // Require a register for the interface check since there is a loop that compares the class to
6679 // a memory address.
6680 locations->SetInAt(1, Location::RequiresRegister());
6681 } else {
6682 locations->SetInAt(1, Location::Any());
6683 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006684 // Note that TypeCheckSlowPathX86 uses this "temp" register too.
6685 locations->AddTemp(Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006686 // When read barriers are enabled, we need an additional temporary register for some cases.
6687 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
6688}
6689
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006690void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006691 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006692 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006693 Location obj_loc = locations->InAt(0);
6694 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006695 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006696 Location temp_loc = locations->GetTemp(0);
6697 Register temp = temp_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006698 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
6699 DCHECK_GE(num_temps, 1u);
6700 DCHECK_LE(num_temps, 2u);
6701 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
6702 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6703 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6704 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6705 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
6706 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
6707 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
6708 const uint32_t object_array_data_offset =
6709 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006710
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006711 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
6712 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
6713 // read barriers is done for performance and code size reasons.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006714 bool is_type_check_slow_path_fatal =
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006715 IsTypeCheckSlowPathFatal(type_check_kind, instruction->CanThrowIntoCatchBlock());
6716
Roland Levillain0d5a2812015-11-13 10:07:31 +00006717 SlowPathCode* type_check_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006718 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
6719 instruction, is_type_check_slow_path_fatal);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006720 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006721
Roland Levillain0d5a2812015-11-13 10:07:31 +00006722 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006723 // Avoid null check if we know obj is not null.
6724 if (instruction->MustDoNullCheck()) {
6725 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006726 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006727 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006728
Roland Levillain0d5a2812015-11-13 10:07:31 +00006729 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006730 case TypeCheckKind::kExactCheck:
6731 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006732 // /* HeapReference<Class> */ temp = obj->klass_
6733 GenerateReferenceLoadTwoRegisters(instruction,
6734 temp_loc,
6735 obj_loc,
6736 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006737 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006738
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006739 if (cls.IsRegister()) {
6740 __ cmpl(temp, cls.AsRegister<Register>());
6741 } else {
6742 DCHECK(cls.IsStackSlot()) << cls;
6743 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6744 }
6745 // Jump to slow path for throwing the exception or doing a
6746 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006747 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006748 break;
6749 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006750
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006751 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006752 // /* HeapReference<Class> */ temp = obj->klass_
6753 GenerateReferenceLoadTwoRegisters(instruction,
6754 temp_loc,
6755 obj_loc,
6756 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006757 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006758
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006759 // If the class is abstract, we eagerly fetch the super class of the
6760 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006761 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006762 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006763 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006764 GenerateReferenceLoadOneRegister(instruction,
6765 temp_loc,
6766 super_offset,
6767 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006768 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006769
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006770 // If the class reference currently in `temp` is null, jump to the slow path to throw the
6771 // exception.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006772 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006773 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006774
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006775 // Otherwise, compare the classes
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006776 if (cls.IsRegister()) {
6777 __ cmpl(temp, cls.AsRegister<Register>());
6778 } else {
6779 DCHECK(cls.IsStackSlot()) << cls;
6780 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6781 }
6782 __ j(kNotEqual, &loop);
6783 break;
6784 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006785
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006786 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006787 // /* HeapReference<Class> */ temp = obj->klass_
6788 GenerateReferenceLoadTwoRegisters(instruction,
6789 temp_loc,
6790 obj_loc,
6791 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006792 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006793
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006794 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006795 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006796 __ Bind(&loop);
6797 if (cls.IsRegister()) {
6798 __ cmpl(temp, cls.AsRegister<Register>());
6799 } else {
6800 DCHECK(cls.IsStackSlot()) << cls;
6801 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6802 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006803 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006804
Roland Levillain0d5a2812015-11-13 10:07:31 +00006805 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006806 GenerateReferenceLoadOneRegister(instruction,
6807 temp_loc,
6808 super_offset,
6809 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006810 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006811
6812 // If the class reference currently in `temp` is not null, jump
6813 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006814 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006815 __ j(kNotZero, &loop);
6816 // Otherwise, jump to the slow path to throw the exception.;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006817 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006818 break;
6819 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006820
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006821 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006822 // /* HeapReference<Class> */ temp = obj->klass_
6823 GenerateReferenceLoadTwoRegisters(instruction,
6824 temp_loc,
6825 obj_loc,
6826 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006827 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006828
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006829 // Do an exact check.
6830 if (cls.IsRegister()) {
6831 __ cmpl(temp, cls.AsRegister<Register>());
6832 } else {
6833 DCHECK(cls.IsStackSlot()) << cls;
6834 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6835 }
6836 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006837
6838 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006839 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006840 GenerateReferenceLoadOneRegister(instruction,
6841 temp_loc,
6842 component_offset,
6843 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006844 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006845
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006846 // If the component type is null (i.e. the object not an array), jump to the slow path to
6847 // throw the exception. Otherwise proceed with the check.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006848 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006849 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006850
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006851 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006852 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006853 break;
6854 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006855
Calin Juravle98893e12015-10-02 21:05:03 +01006856 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006857 // We always go into the type check slow path for the unresolved check case.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006858 // We cannot directly call the CheckCast runtime entry point
6859 // without resorting to a type checking slow path here (i.e. by
6860 // calling InvokeRuntime directly), as it would require to
6861 // assign fixed registers for the inputs of this HInstanceOf
6862 // instruction (following the runtime calling convention), which
6863 // might be cluttered by the potential first read barrier
6864 // emission at the beginning of this method.
6865 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006866 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006867
6868 case TypeCheckKind::kInterfaceCheck: {
6869 // Fast path for the interface check. Since we compare with a memory location in the inner
6870 // loop we would need to have cls poisoned. However unpoisoning cls would reset the
6871 // conditional flags and cause the conditional jump to be incorrect. Therefore we just jump
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006872 // to the slow path if we are running under poisoning.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006873 if (!kPoisonHeapReferences) {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006874 // Try to avoid read barriers to improve the fast path. We can not get false positives by
6875 // doing this.
6876 // /* HeapReference<Class> */ temp = obj->klass_
6877 GenerateReferenceLoadTwoRegisters(instruction,
6878 temp_loc,
6879 obj_loc,
6880 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006881 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006882
6883 // /* HeapReference<Class> */ temp = temp->iftable_
6884 GenerateReferenceLoadTwoRegisters(instruction,
6885 temp_loc,
6886 temp_loc,
6887 iftable_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006888 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08006889 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006890 __ movl(maybe_temp2_loc.AsRegister<Register>(), Address(temp, array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08006891 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006892 NearLabel start_loop;
6893 __ Bind(&start_loop);
Mathieu Chartier6beced42016-11-15 15:51:31 -08006894 // Need to subtract first to handle the empty array case.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006895 __ subl(maybe_temp2_loc.AsRegister<Register>(), Immediate(2));
Mathieu Chartier6beced42016-11-15 15:51:31 -08006896 __ j(kNegative, type_check_slow_path->GetEntryLabel());
6897 // Go to next interface if the classes do not match.
6898 __ cmpl(cls.AsRegister<Register>(),
6899 CodeGeneratorX86::ArrayAddress(temp,
6900 maybe_temp2_loc,
6901 TIMES_4,
6902 object_array_data_offset));
6903 __ j(kNotEqual, &start_loop);
6904 } else {
6905 __ jmp(type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006906 }
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006907 break;
6908 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006909 }
6910 __ Bind(&done);
6911
Roland Levillain0d5a2812015-11-13 10:07:31 +00006912 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006913}
6914
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006915void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006916 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6917 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006918 InvokeRuntimeCallingConvention calling_convention;
6919 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6920}
6921
6922void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006923 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject
6924 : kQuickUnlockObject,
Alexandre Rames8158f282015-08-07 10:26:17 +01006925 instruction,
Serban Constantinescuba45db02016-07-12 22:53:02 +01006926 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006927 if (instruction->IsEnter()) {
6928 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6929 } else {
6930 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6931 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006932}
6933
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006934void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6935void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6936void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6937
6938void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6939 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006940 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006941 DCHECK(instruction->GetResultType() == DataType::Type::kInt32
6942 || instruction->GetResultType() == DataType::Type::kInt64);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006943 locations->SetInAt(0, Location::RequiresRegister());
6944 locations->SetInAt(1, Location::Any());
6945 locations->SetOut(Location::SameAsFirstInput());
6946}
6947
6948void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
6949 HandleBitwiseOperation(instruction);
6950}
6951
6952void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
6953 HandleBitwiseOperation(instruction);
6954}
6955
6956void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
6957 HandleBitwiseOperation(instruction);
6958}
6959
6960void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6961 LocationSummary* locations = instruction->GetLocations();
6962 Location first = locations->InAt(0);
6963 Location second = locations->InAt(1);
6964 DCHECK(first.Equals(locations->Out()));
6965
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006966 if (instruction->GetResultType() == DataType::Type::kInt32) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006967 if (second.IsRegister()) {
6968 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006969 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006970 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006971 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006972 } else {
6973 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006974 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006975 }
6976 } else if (second.IsConstant()) {
6977 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006978 __ andl(first.AsRegister<Register>(),
6979 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006980 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006981 __ orl(first.AsRegister<Register>(),
6982 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006983 } else {
6984 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00006985 __ xorl(first.AsRegister<Register>(),
6986 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006987 }
6988 } else {
6989 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006990 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006991 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006992 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006993 } else {
6994 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006995 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006996 }
6997 }
6998 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006999 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007000 if (second.IsRegisterPair()) {
7001 if (instruction->IsAnd()) {
7002 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7003 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7004 } else if (instruction->IsOr()) {
7005 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7006 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7007 } else {
7008 DCHECK(instruction->IsXor());
7009 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7010 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7011 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007012 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007013 if (instruction->IsAnd()) {
7014 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7015 __ andl(first.AsRegisterPairHigh<Register>(),
7016 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7017 } else if (instruction->IsOr()) {
7018 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7019 __ orl(first.AsRegisterPairHigh<Register>(),
7020 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7021 } else {
7022 DCHECK(instruction->IsXor());
7023 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7024 __ xorl(first.AsRegisterPairHigh<Register>(),
7025 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7026 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007027 } else {
7028 DCHECK(second.IsConstant()) << second;
7029 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007030 int32_t low_value = Low32Bits(value);
7031 int32_t high_value = High32Bits(value);
7032 Immediate low(low_value);
7033 Immediate high(high_value);
7034 Register first_low = first.AsRegisterPairLow<Register>();
7035 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007036 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007037 if (low_value == 0) {
7038 __ xorl(first_low, first_low);
7039 } else if (low_value != -1) {
7040 __ andl(first_low, low);
7041 }
7042 if (high_value == 0) {
7043 __ xorl(first_high, first_high);
7044 } else if (high_value != -1) {
7045 __ andl(first_high, high);
7046 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007047 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007048 if (low_value != 0) {
7049 __ orl(first_low, low);
7050 }
7051 if (high_value != 0) {
7052 __ orl(first_high, high);
7053 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007054 } else {
7055 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007056 if (low_value != 0) {
7057 __ xorl(first_low, low);
7058 }
7059 if (high_value != 0) {
7060 __ xorl(first_high, high);
7061 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007062 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007063 }
7064 }
7065}
7066
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007067void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(
7068 HInstruction* instruction,
7069 Location out,
7070 uint32_t offset,
7071 Location maybe_temp,
7072 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007073 Register out_reg = out.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007074 if (read_barrier_option == kWithReadBarrier) {
7075 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007076 if (kUseBakerReadBarrier) {
7077 // Load with fast path based Baker's read barrier.
7078 // /* HeapReference<Object> */ out = *(out + offset)
7079 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00007080 instruction, out, out_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007081 } else {
7082 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007083 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain7c1559a2015-12-15 10:55:36 +00007084 // in the following move operation, as we will need it for the
7085 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00007086 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007087 __ movl(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007088 // /* HeapReference<Object> */ out = *(out + offset)
7089 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007090 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007091 }
7092 } else {
7093 // Plain load with no read barrier.
7094 // /* HeapReference<Object> */ out = *(out + offset)
7095 __ movl(out_reg, Address(out_reg, offset));
7096 __ MaybeUnpoisonHeapReference(out_reg);
7097 }
7098}
7099
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007100void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(
7101 HInstruction* instruction,
7102 Location out,
7103 Location obj,
7104 uint32_t offset,
7105 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007106 Register out_reg = out.AsRegister<Register>();
7107 Register obj_reg = obj.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007108 if (read_barrier_option == kWithReadBarrier) {
7109 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007110 if (kUseBakerReadBarrier) {
7111 // Load with fast path based Baker's read barrier.
7112 // /* HeapReference<Object> */ out = *(obj + offset)
7113 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00007114 instruction, out, obj_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007115 } else {
7116 // Load with slow path based read barrier.
7117 // /* HeapReference<Object> */ out = *(obj + offset)
7118 __ movl(out_reg, Address(obj_reg, offset));
7119 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
7120 }
7121 } else {
7122 // Plain load with no read barrier.
7123 // /* HeapReference<Object> */ out = *(obj + offset)
7124 __ movl(out_reg, Address(obj_reg, offset));
7125 __ MaybeUnpoisonHeapReference(out_reg);
7126 }
7127}
7128
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007129void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(
7130 HInstruction* instruction,
7131 Location root,
7132 const Address& address,
7133 Label* fixup_label,
7134 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007135 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007136 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07007137 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007138 if (kUseBakerReadBarrier) {
7139 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
7140 // Baker's read barrier are used:
7141 //
Roland Levillaind966ce72017-02-09 16:20:14 +00007142 // root = obj.field;
7143 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
7144 // if (temp != null) {
7145 // root = temp(root)
Roland Levillain7c1559a2015-12-15 10:55:36 +00007146 // }
7147
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007148 // /* GcRoot<mirror::Object> */ root = *address
7149 __ movl(root_reg, address);
7150 if (fixup_label != nullptr) {
7151 __ Bind(fixup_label);
7152 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007153 static_assert(
7154 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
7155 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
7156 "have different sizes.");
7157 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
7158 "art::mirror::CompressedReference<mirror::Object> and int32_t "
7159 "have different sizes.");
7160
Vladimir Marko953437b2016-08-24 08:30:46 +00007161 // Slow path marking the GC root `root`.
Vladimir Marko174b2e22017-10-12 13:34:49 +01007162 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathX86(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007163 instruction, root, /* unpoison_ref_before_marking */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007164 codegen_->AddSlowPath(slow_path);
7165
Roland Levillaind966ce72017-02-09 16:20:14 +00007166 // Test the entrypoint (`Thread::Current()->pReadBarrierMarkReg ## root.reg()`).
7167 const int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +01007168 Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(root.reg());
Roland Levillaind966ce72017-02-09 16:20:14 +00007169 __ fs()->cmpl(Address::Absolute(entry_point_offset), Immediate(0));
7170 // The entrypoint is null when the GC is not marking.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007171 __ j(kNotEqual, slow_path->GetEntryLabel());
7172 __ Bind(slow_path->GetExitLabel());
7173 } else {
7174 // GC root loaded through a slow path for read barriers other
7175 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007176 // /* GcRoot<mirror::Object>* */ root = address
7177 __ leal(root_reg, address);
7178 if (fixup_label != nullptr) {
7179 __ Bind(fixup_label);
7180 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007181 // /* mirror::Object* */ root = root->Read()
7182 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
7183 }
7184 } else {
7185 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007186 // /* GcRoot<mirror::Object> */ root = *address
7187 __ movl(root_reg, address);
7188 if (fixup_label != nullptr) {
7189 __ Bind(fixup_label);
7190 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007191 // Note that GC roots are not affected by heap poisoning, thus we
7192 // do not have to unpoison `root_reg` here.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007193 }
7194}
7195
7196void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
7197 Location ref,
7198 Register obj,
7199 uint32_t offset,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007200 bool needs_null_check) {
7201 DCHECK(kEmitCompilerReadBarrier);
7202 DCHECK(kUseBakerReadBarrier);
7203
7204 // /* HeapReference<Object> */ ref = *(obj + offset)
7205 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007206 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007207}
7208
7209void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
7210 Location ref,
7211 Register obj,
7212 uint32_t data_offset,
7213 Location index,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007214 bool needs_null_check) {
7215 DCHECK(kEmitCompilerReadBarrier);
7216 DCHECK(kUseBakerReadBarrier);
7217
Roland Levillain3d312422016-06-23 13:53:42 +01007218 static_assert(
7219 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
7220 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00007221 // /* HeapReference<Object> */ ref =
7222 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007223 Address src = CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007224 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007225}
7226
7227void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
7228 Location ref,
7229 Register obj,
7230 const Address& src,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007231 bool needs_null_check,
7232 bool always_update_field,
7233 Register* temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007234 DCHECK(kEmitCompilerReadBarrier);
7235 DCHECK(kUseBakerReadBarrier);
7236
7237 // In slow path based read barriers, the read barrier call is
7238 // inserted after the original load. However, in fast path based
7239 // Baker's read barriers, we need to perform the load of
7240 // mirror::Object::monitor_ *before* the original reference load.
7241 // This load-load ordering is required by the read barrier.
7242 // The fast path/slow path (for Baker's algorithm) should look like:
7243 //
7244 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7245 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7246 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007247 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain7c1559a2015-12-15 10:55:36 +00007248 // if (is_gray) {
7249 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7250 // }
7251 //
7252 // Note: the original implementation in ReadBarrier::Barrier is
7253 // slightly more complex as:
7254 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007255 // the high-bits of rb_state, which are expected to be all zeroes
7256 // (we use CodeGeneratorX86::GenerateMemoryBarrier instead here,
7257 // which is a no-op thanks to the x86 memory model);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007258 // - it performs additional checks that we do not do here for
7259 // performance reasons.
7260
7261 Register ref_reg = ref.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +00007262 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7263
Vladimir Marko953437b2016-08-24 08:30:46 +00007264 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007265 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
7266 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko953437b2016-08-24 08:30:46 +00007267 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
7268 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
7269 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
7270
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007271 // if (rb_state == ReadBarrier::GrayState())
Vladimir Marko953437b2016-08-24 08:30:46 +00007272 // ref = ReadBarrier::Mark(ref);
7273 // At this point, just do the "if" and make sure that flags are preserved until the branch.
7274 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain7c1559a2015-12-15 10:55:36 +00007275 if (needs_null_check) {
7276 MaybeRecordImplicitNullCheck(instruction);
7277 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007278
7279 // Load fence to prevent load-load reordering.
7280 // Note that this is a no-op, thanks to the x86 memory model.
7281 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
7282
7283 // The actual reference load.
7284 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00007285 __ movl(ref_reg, src); // Flags are unaffected.
7286
7287 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
7288 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007289 SlowPathCode* slow_path;
7290 if (always_update_field) {
7291 DCHECK(temp != nullptr);
Vladimir Marko174b2e22017-10-12 13:34:49 +01007292 slow_path = new (GetScopedAllocator()) ReadBarrierMarkAndUpdateFieldSlowPathX86(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007293 instruction, ref, obj, src, /* unpoison_ref_before_marking */ true, *temp);
7294 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01007295 slow_path = new (GetScopedAllocator()) ReadBarrierMarkSlowPathX86(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007296 instruction, ref, /* unpoison_ref_before_marking */ true);
7297 }
Vladimir Marko953437b2016-08-24 08:30:46 +00007298 AddSlowPath(slow_path);
7299
7300 // We have done the "if" of the gray bit check above, now branch based on the flags.
7301 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain7c1559a2015-12-15 10:55:36 +00007302
7303 // Object* ref = ref_addr->AsMirrorPtr()
7304 __ MaybeUnpoisonHeapReference(ref_reg);
7305
Roland Levillain7c1559a2015-12-15 10:55:36 +00007306 __ Bind(slow_path->GetExitLabel());
7307}
7308
7309void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
7310 Location out,
7311 Location ref,
7312 Location obj,
7313 uint32_t offset,
7314 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007315 DCHECK(kEmitCompilerReadBarrier);
7316
Roland Levillain7c1559a2015-12-15 10:55:36 +00007317 // Insert a slow path based read barrier *after* the reference load.
7318 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007319 // If heap poisoning is enabled, the unpoisoning of the loaded
7320 // reference will be carried out by the runtime within the slow
7321 // path.
7322 //
7323 // Note that `ref` currently does not get unpoisoned (when heap
7324 // poisoning is enabled), which is alright as the `ref` argument is
7325 // not used by the artReadBarrierSlow entry point.
7326 //
7327 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01007328 SlowPathCode* slow_path = new (GetScopedAllocator())
Roland Levillain0d5a2812015-11-13 10:07:31 +00007329 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
7330 AddSlowPath(slow_path);
7331
Roland Levillain0d5a2812015-11-13 10:07:31 +00007332 __ jmp(slow_path->GetEntryLabel());
7333 __ Bind(slow_path->GetExitLabel());
7334}
7335
Roland Levillain7c1559a2015-12-15 10:55:36 +00007336void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7337 Location out,
7338 Location ref,
7339 Location obj,
7340 uint32_t offset,
7341 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007342 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007343 // Baker's read barriers shall be handled by the fast path
7344 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
7345 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007346 // If heap poisoning is enabled, unpoisoning will be taken care of
7347 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007348 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007349 } else if (kPoisonHeapReferences) {
7350 __ UnpoisonHeapReference(out.AsRegister<Register>());
7351 }
7352}
7353
Roland Levillain7c1559a2015-12-15 10:55:36 +00007354void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7355 Location out,
7356 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007357 DCHECK(kEmitCompilerReadBarrier);
7358
Roland Levillain7c1559a2015-12-15 10:55:36 +00007359 // Insert a slow path based read barrier *after* the GC root load.
7360 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007361 // Note that GC roots are not affected by heap poisoning, so we do
7362 // not need to do anything special for this here.
7363 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007364 new (GetScopedAllocator()) ReadBarrierForRootSlowPathX86(instruction, out, root);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007365 AddSlowPath(slow_path);
7366
Roland Levillain0d5a2812015-11-13 10:07:31 +00007367 __ jmp(slow_path->GetEntryLabel());
7368 __ Bind(slow_path->GetExitLabel());
7369}
7370
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007371void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007372 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007373 LOG(FATAL) << "Unreachable";
7374}
7375
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007376void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007377 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007378 LOG(FATAL) << "Unreachable";
7379}
7380
Mark Mendellfe57faa2015-09-18 09:26:15 -04007381// Simple implementation of packed switch - generate cascaded compare/jumps.
7382void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7383 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007384 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendellfe57faa2015-09-18 09:26:15 -04007385 locations->SetInAt(0, Location::RequiresRegister());
7386}
7387
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007388void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
7389 int32_t lower_bound,
7390 uint32_t num_entries,
7391 HBasicBlock* switch_block,
7392 HBasicBlock* default_block) {
7393 // Figure out the correct compare values and jump conditions.
7394 // Handle the first compare/branch as a special case because it might
7395 // jump to the default case.
7396 DCHECK_GT(num_entries, 2u);
7397 Condition first_condition;
7398 uint32_t index;
7399 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
7400 if (lower_bound != 0) {
7401 first_condition = kLess;
7402 __ cmpl(value_reg, Immediate(lower_bound));
7403 __ j(first_condition, codegen_->GetLabelOf(default_block));
7404 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007405
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007406 index = 1;
7407 } else {
7408 // Handle all the compare/jumps below.
7409 first_condition = kBelow;
7410 index = 0;
7411 }
7412
7413 // Handle the rest of the compare/jumps.
7414 for (; index + 1 < num_entries; index += 2) {
7415 int32_t compare_to_value = lower_bound + index + 1;
7416 __ cmpl(value_reg, Immediate(compare_to_value));
7417 // Jump to successors[index] if value < case_value[index].
7418 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
7419 // Jump to successors[index + 1] if value == case_value[index + 1].
7420 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
7421 }
7422
7423 if (index != num_entries) {
7424 // There are an odd number of entries. Handle the last one.
7425 DCHECK_EQ(index + 1, num_entries);
7426 __ cmpl(value_reg, Immediate(lower_bound + index));
7427 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007428 }
7429
7430 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007431 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
7432 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007433 }
7434}
7435
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007436void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7437 int32_t lower_bound = switch_instr->GetStartValue();
7438 uint32_t num_entries = switch_instr->GetNumEntries();
7439 LocationSummary* locations = switch_instr->GetLocations();
7440 Register value_reg = locations->InAt(0).AsRegister<Register>();
7441
7442 GenPackedSwitchWithCompares(value_reg,
7443 lower_bound,
7444 num_entries,
7445 switch_instr->GetBlock(),
7446 switch_instr->GetDefaultBlock());
7447}
7448
Mark Mendell805b3b52015-09-18 14:10:29 -04007449void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7450 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007451 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendell805b3b52015-09-18 14:10:29 -04007452 locations->SetInAt(0, Location::RequiresRegister());
7453
7454 // Constant area pointer.
7455 locations->SetInAt(1, Location::RequiresRegister());
7456
7457 // And the temporary we need.
7458 locations->AddTemp(Location::RequiresRegister());
7459}
7460
7461void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7462 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007463 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04007464 LocationSummary* locations = switch_instr->GetLocations();
7465 Register value_reg = locations->InAt(0).AsRegister<Register>();
7466 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7467
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007468 if (num_entries <= kPackedSwitchJumpTableThreshold) {
7469 GenPackedSwitchWithCompares(value_reg,
7470 lower_bound,
7471 num_entries,
7472 switch_instr->GetBlock(),
7473 default_block);
7474 return;
7475 }
7476
Mark Mendell805b3b52015-09-18 14:10:29 -04007477 // Optimizing has a jump area.
7478 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7479 Register constant_area = locations->InAt(1).AsRegister<Register>();
7480
7481 // Remove the bias, if needed.
7482 if (lower_bound != 0) {
7483 __ leal(temp_reg, Address(value_reg, -lower_bound));
7484 value_reg = temp_reg;
7485 }
7486
7487 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007488 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04007489 __ cmpl(value_reg, Immediate(num_entries - 1));
7490 __ j(kAbove, codegen_->GetLabelOf(default_block));
7491
7492 // We are in the range of the table.
7493 // Load (target-constant_area) from the jump table, indexing by the value.
7494 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
7495
7496 // Compute the actual target address by adding in constant_area.
7497 __ addl(temp_reg, constant_area);
7498
7499 // And jump.
7500 __ jmp(temp_reg);
7501}
7502
Mark Mendell0616ae02015-04-17 12:49:27 -04007503void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
7504 HX86ComputeBaseMethodAddress* insn) {
7505 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007506 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Mark Mendell0616ae02015-04-17 12:49:27 -04007507 locations->SetOut(Location::RequiresRegister());
7508}
7509
7510void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
7511 HX86ComputeBaseMethodAddress* insn) {
7512 LocationSummary* locations = insn->GetLocations();
7513 Register reg = locations->Out().AsRegister<Register>();
7514
7515 // Generate call to next instruction.
7516 Label next_instruction;
7517 __ call(&next_instruction);
7518 __ Bind(&next_instruction);
7519
7520 // Remember this offset for later use with constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007521 codegen_->AddMethodAddressOffset(insn, GetAssembler()->CodeSize());
Mark Mendell0616ae02015-04-17 12:49:27 -04007522
7523 // Grab the return address off the stack.
7524 __ popl(reg);
7525}
7526
7527void LocationsBuilderX86::VisitX86LoadFromConstantTable(
7528 HX86LoadFromConstantTable* insn) {
7529 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007530 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Mark Mendell0616ae02015-04-17 12:49:27 -04007531
7532 locations->SetInAt(0, Location::RequiresRegister());
7533 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
7534
7535 // If we don't need to be materialized, we only need the inputs to be set.
David Brazdilb3e773e2016-01-26 11:28:37 +00007536 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007537 return;
7538 }
7539
7540 switch (insn->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007541 case DataType::Type::kFloat32:
7542 case DataType::Type::kFloat64:
Mark Mendell0616ae02015-04-17 12:49:27 -04007543 locations->SetOut(Location::RequiresFpuRegister());
7544 break;
7545
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007546 case DataType::Type::kInt32:
Mark Mendell0616ae02015-04-17 12:49:27 -04007547 locations->SetOut(Location::RequiresRegister());
7548 break;
7549
7550 default:
7551 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7552 }
7553}
7554
7555void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
David Brazdilb3e773e2016-01-26 11:28:37 +00007556 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007557 return;
7558 }
7559
7560 LocationSummary* locations = insn->GetLocations();
7561 Location out = locations->Out();
7562 Register const_area = locations->InAt(0).AsRegister<Register>();
7563 HConstant *value = insn->GetConstant();
7564
7565 switch (insn->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007566 case DataType::Type::kFloat32:
Mark Mendell0616ae02015-04-17 12:49:27 -04007567 __ movss(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007568 codegen_->LiteralFloatAddress(
7569 value->AsFloatConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04007570 break;
7571
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007572 case DataType::Type::kFloat64:
Mark Mendell0616ae02015-04-17 12:49:27 -04007573 __ movsd(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007574 codegen_->LiteralDoubleAddress(
7575 value->AsDoubleConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04007576 break;
7577
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007578 case DataType::Type::kInt32:
Mark Mendell0616ae02015-04-17 12:49:27 -04007579 __ movl(out.AsRegister<Register>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007580 codegen_->LiteralInt32Address(
7581 value->AsIntConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04007582 break;
7583
7584 default:
7585 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7586 }
7587}
7588
Mark Mendell0616ae02015-04-17 12:49:27 -04007589/**
7590 * Class to handle late fixup of offsets into constant area.
7591 */
Vladimir Marko5233f932015-09-29 19:01:15 +01007592class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04007593 public:
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007594 RIPFixup(CodeGeneratorX86& codegen,
7595 HX86ComputeBaseMethodAddress* base_method_address,
7596 size_t offset)
7597 : codegen_(&codegen),
7598 base_method_address_(base_method_address),
7599 offset_into_constant_area_(offset) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04007600
7601 protected:
7602 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
7603
7604 CodeGeneratorX86* codegen_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007605 HX86ComputeBaseMethodAddress* base_method_address_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007606
7607 private:
7608 void Process(const MemoryRegion& region, int pos) OVERRIDE {
7609 // Patch the correct offset for the instruction. The place to patch is the
7610 // last 4 bytes of the instruction.
7611 // The value to patch is the distance from the offset in the constant area
7612 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04007613 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007614 int32_t relative_position =
7615 constant_offset - codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell0616ae02015-04-17 12:49:27 -04007616
7617 // Patch in the right value.
7618 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
7619 }
7620
Mark Mendell0616ae02015-04-17 12:49:27 -04007621 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04007622 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007623};
7624
Mark Mendell805b3b52015-09-18 14:10:29 -04007625/**
7626 * Class to handle late fixup of offsets to a jump table that will be created in the
7627 * constant area.
7628 */
7629class JumpTableRIPFixup : public RIPFixup {
7630 public:
7631 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007632 : RIPFixup(codegen, switch_instr->GetBaseMethodAddress(), static_cast<size_t>(-1)),
7633 switch_instr_(switch_instr) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04007634
7635 void CreateJumpTable() {
7636 X86Assembler* assembler = codegen_->GetAssembler();
7637
7638 // Ensure that the reference to the jump table has the correct offset.
7639 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
7640 SetOffset(offset_in_constant_table);
7641
7642 // The label values in the jump table are computed relative to the
7643 // instruction addressing the constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007644 const int32_t relative_offset = codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell805b3b52015-09-18 14:10:29 -04007645
7646 // Populate the jump table with the correct values for the jump table.
7647 int32_t num_entries = switch_instr_->GetNumEntries();
7648 HBasicBlock* block = switch_instr_->GetBlock();
7649 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
7650 // The value that we want is the target offset - the position of the table.
7651 for (int32_t i = 0; i < num_entries; i++) {
7652 HBasicBlock* b = successors[i];
7653 Label* l = codegen_->GetLabelOf(b);
7654 DCHECK(l->IsBound());
7655 int32_t offset_to_block = l->Position() - relative_offset;
7656 assembler->AppendInt32(offset_to_block);
7657 }
7658 }
7659
7660 private:
7661 const HX86PackedSwitch* switch_instr_;
7662};
7663
7664void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
7665 // Generate the constant area if needed.
7666 X86Assembler* assembler = GetAssembler();
7667 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
7668 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
7669 // byte values.
7670 assembler->Align(4, 0);
7671 constant_area_start_ = assembler->CodeSize();
7672
7673 // Populate any jump tables.
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007674 for (JumpTableRIPFixup* jump_table : fixups_to_jump_tables_) {
Mark Mendell805b3b52015-09-18 14:10:29 -04007675 jump_table->CreateJumpTable();
7676 }
7677
7678 // And now add the constant area to the generated code.
7679 assembler->AddConstantArea();
7680 }
7681
7682 // And finish up.
7683 CodeGenerator::Finalize(allocator);
7684}
7685
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007686Address CodeGeneratorX86::LiteralDoubleAddress(double v,
7687 HX86ComputeBaseMethodAddress* method_base,
7688 Register reg) {
7689 AssemblerFixup* fixup =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007690 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddDouble(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04007691 return Address(reg, kDummy32BitOffset, fixup);
7692}
7693
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007694Address CodeGeneratorX86::LiteralFloatAddress(float v,
7695 HX86ComputeBaseMethodAddress* method_base,
7696 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007697 AssemblerFixup* fixup =
7698 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddFloat(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04007699 return Address(reg, kDummy32BitOffset, fixup);
7700}
7701
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007702Address CodeGeneratorX86::LiteralInt32Address(int32_t v,
7703 HX86ComputeBaseMethodAddress* method_base,
7704 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007705 AssemblerFixup* fixup =
7706 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddInt32(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04007707 return Address(reg, kDummy32BitOffset, fixup);
7708}
7709
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007710Address CodeGeneratorX86::LiteralInt64Address(int64_t v,
7711 HX86ComputeBaseMethodAddress* method_base,
7712 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007713 AssemblerFixup* fixup =
7714 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddInt64(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04007715 return Address(reg, kDummy32BitOffset, fixup);
7716}
7717
Aart Bika19616e2016-02-01 18:57:58 -08007718void CodeGeneratorX86::Load32BitValue(Register dest, int32_t value) {
7719 if (value == 0) {
7720 __ xorl(dest, dest);
7721 } else {
7722 __ movl(dest, Immediate(value));
7723 }
7724}
7725
7726void CodeGeneratorX86::Compare32BitValue(Register dest, int32_t value) {
7727 if (value == 0) {
7728 __ testl(dest, dest);
7729 } else {
7730 __ cmpl(dest, Immediate(value));
7731 }
7732}
7733
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007734void CodeGeneratorX86::GenerateIntCompare(Location lhs, Location rhs) {
7735 Register lhs_reg = lhs.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07007736 GenerateIntCompare(lhs_reg, rhs);
7737}
7738
7739void CodeGeneratorX86::GenerateIntCompare(Register lhs, Location rhs) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007740 if (rhs.IsConstant()) {
7741 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
jessicahandojo4877b792016-09-08 19:49:13 -07007742 Compare32BitValue(lhs, value);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007743 } else if (rhs.IsStackSlot()) {
jessicahandojo4877b792016-09-08 19:49:13 -07007744 __ cmpl(lhs, Address(ESP, rhs.GetStackIndex()));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007745 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07007746 __ cmpl(lhs, rhs.AsRegister<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007747 }
7748}
7749
7750Address CodeGeneratorX86::ArrayAddress(Register obj,
7751 Location index,
7752 ScaleFactor scale,
7753 uint32_t data_offset) {
7754 return index.IsConstant() ?
7755 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << scale) + data_offset) :
7756 Address(obj, index.AsRegister<Register>(), scale, data_offset);
7757}
7758
Mark Mendell805b3b52015-09-18 14:10:29 -04007759Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
7760 Register reg,
7761 Register value) {
7762 // Create a fixup to be used to create and address the jump table.
7763 JumpTableRIPFixup* table_fixup =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007764 new (GetGraph()->GetAllocator()) JumpTableRIPFixup(*this, switch_instr);
Mark Mendell805b3b52015-09-18 14:10:29 -04007765
7766 // We have to populate the jump tables.
7767 fixups_to_jump_tables_.push_back(table_fixup);
7768
7769 // We want a scaled address, as we are extracting the correct offset from the table.
7770 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
7771}
7772
Andreas Gampe85b62f22015-09-09 13:15:38 -07007773// TODO: target as memory.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007774void CodeGeneratorX86::MoveFromReturnRegister(Location target, DataType::Type type) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07007775 if (!target.IsValid()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007776 DCHECK_EQ(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007777 return;
7778 }
7779
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007780 DCHECK_NE(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007781
7782 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
7783 if (target.Equals(return_loc)) {
7784 return;
7785 }
7786
7787 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7788 // with the else branch.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007789 if (type == DataType::Type::kInt64) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007790 HParallelMove parallel_move(GetGraph()->GetAllocator());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007791 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), DataType::Type::kInt32, nullptr);
7792 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), DataType::Type::kInt32, nullptr);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007793 GetMoveResolver()->EmitNativeCode(&parallel_move);
7794 } else {
7795 // Let the parallel move resolver take care of all of this.
Vladimir Markoca6fff82017-10-03 14:49:14 +01007796 HParallelMove parallel_move(GetGraph()->GetAllocator());
Andreas Gampe85b62f22015-09-09 13:15:38 -07007797 parallel_move.AddMove(return_loc, target, type, nullptr);
7798 GetMoveResolver()->EmitNativeCode(&parallel_move);
7799 }
7800}
7801
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007802void CodeGeneratorX86::PatchJitRootUse(uint8_t* code,
7803 const uint8_t* roots_data,
7804 const PatchInfo<Label>& info,
7805 uint64_t index_in_table) const {
7806 uint32_t code_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
7807 uintptr_t address =
7808 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
7809 typedef __attribute__((__aligned__(1))) uint32_t unaligned_uint32_t;
7810 reinterpret_cast<unaligned_uint32_t*>(code + code_offset)[0] =
7811 dchecked_integral_cast<uint32_t>(address);
7812}
7813
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007814void CodeGeneratorX86::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
7815 for (const PatchInfo<Label>& info : jit_string_patches_) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01007816 StringReference string_reference(&info.dex_file, dex::StringIndex(info.index));
7817 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007818 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007819 }
7820
7821 for (const PatchInfo<Label>& info : jit_class_patches_) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01007822 TypeReference type_reference(&info.dex_file, dex::TypeIndex(info.index));
7823 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007824 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007825 }
7826}
7827
Roland Levillain4d027112015-07-01 15:41:14 +01007828#undef __
7829
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007830} // namespace x86
7831} // namespace art