blob: b9ecfeebb6a61404d01544e3c0b422784f754748 [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
Vladimir Markoaad75c62016-10-03 08:46:48 +0000243 __ jmp(GetExitLabel());
244 }
245
246 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathX86"; }
247
248 private:
249 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86);
250};
251
Andreas Gampe85b62f22015-09-09 13:15:38 -0700252class LoadClassSlowPathX86 : public SlowPathCode {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000253 public:
254 LoadClassSlowPathX86(HLoadClass* cls,
255 HInstruction* at,
256 uint32_t dex_pc,
257 bool do_clinit)
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000258 : SlowPathCode(at), cls_(cls), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000259 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
260 }
261
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000262 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000263 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000264 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
265 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000266 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000267
268 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000269 dex::TypeIndex type_index = cls_->GetTypeIndex();
270 __ movl(calling_convention.GetRegisterAt(0), Immediate(type_index.index_));
Serban Constantinescuba45db02016-07-12 22:53:02 +0100271 x86_codegen->InvokeRuntime(do_clinit_ ? kQuickInitializeStaticStorage
272 : kQuickInitializeType,
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000273 instruction_,
274 dex_pc_,
275 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000276 if (do_clinit_) {
277 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
278 } else {
279 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
280 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000281
282 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000283 Location out = locations->Out();
284 if (out.IsValid()) {
285 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
286 x86_codegen->Move32(out, Location::RegisterLocation(EAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000287 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000288 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000289 __ jmp(GetExitLabel());
290 }
291
Alexandre Rames9931f312015-06-19 14:47:01 +0100292 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathX86"; }
293
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000294 private:
295 // The class this slow path will load.
296 HLoadClass* const cls_;
297
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000298 // The dex PC of `at_`.
299 const uint32_t dex_pc_;
300
301 // Whether to initialize the class.
302 const bool do_clinit_;
303
304 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
305};
306
Andreas Gampe85b62f22015-09-09 13:15:38 -0700307class TypeCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000308 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000309 TypeCheckSlowPathX86(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000310 : SlowPathCode(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000311
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000312 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000313 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000314 DCHECK(instruction_->IsCheckCast()
315 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000316
317 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
318 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000319
Vladimir Markoe619f6c2017-12-12 16:00:01 +0000320 if (kPoisonHeapReferences &&
321 instruction_->IsCheckCast() &&
322 instruction_->AsCheckCast()->GetTypeCheckKind() == TypeCheckKind::kInterfaceCheck) {
323 // First, unpoison the `cls` reference that was poisoned for direct memory comparison.
324 __ UnpoisonHeapReference(locations->InAt(1).AsRegister<Register>());
325 }
326
Vladimir Marko87584542017-12-12 17:47:52 +0000327 if (!is_fatal_ || instruction_->CanThrowIntoCatchBlock()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000328 SaveLiveRegisters(codegen, locations);
329 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000330
331 // We're moving two locations to locations that could overlap, so we need a parallel
332 // move resolver.
333 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800334 x86_codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800335 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100336 DataType::Type::kReference,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800337 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800338 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100339 DataType::Type::kReference);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000340 if (instruction_->IsInstanceOf()) {
Serban Constantinescuba45db02016-07-12 22:53:02 +0100341 x86_codegen->InvokeRuntime(kQuickInstanceofNonTrivial,
Alexandre Rames8158f282015-08-07 10:26:17 +0100342 instruction_,
343 instruction_->GetDexPc(),
344 this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800345 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000346 } else {
347 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800348 x86_codegen->InvokeRuntime(kQuickCheckInstanceOf,
349 instruction_,
350 instruction_->GetDexPc(),
351 this);
352 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000353 }
354
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000355 if (!is_fatal_) {
356 if (instruction_->IsInstanceOf()) {
357 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
358 }
359 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray75374372015-09-17 17:12:19 +0000360
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000361 __ jmp(GetExitLabel());
362 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000363 }
364
Alexandre Rames9931f312015-06-19 14:47:01 +0100365 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathX86"; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000366 bool IsFatal() const OVERRIDE { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100367
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000368 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000369 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000370
371 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
372};
373
Andreas Gampe85b62f22015-09-09 13:15:38 -0700374class DeoptimizationSlowPathX86 : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700375 public:
Aart Bik42249c32016-01-07 15:33:50 -0800376 explicit DeoptimizationSlowPathX86(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000377 : SlowPathCode(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700378
379 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +0100380 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700381 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100382 LocationSummary* locations = instruction_->GetLocations();
383 SaveLiveRegisters(codegen, locations);
384 InvokeRuntimeCallingConvention calling_convention;
385 x86_codegen->Load32BitValue(
386 calling_convention.GetRegisterAt(0),
387 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescuba45db02016-07-12 22:53:02 +0100388 x86_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100389 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700390 }
391
Alexandre Rames9931f312015-06-19 14:47:01 +0100392 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86"; }
393
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700394 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700395 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86);
396};
397
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100398class ArraySetSlowPathX86 : public SlowPathCode {
399 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000400 explicit ArraySetSlowPathX86(HInstruction* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100401
402 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
403 LocationSummary* locations = instruction_->GetLocations();
404 __ Bind(GetEntryLabel());
405 SaveLiveRegisters(codegen, locations);
406
407 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100408 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100409 parallel_move.AddMove(
410 locations->InAt(0),
411 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100412 DataType::Type::kReference,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100413 nullptr);
414 parallel_move.AddMove(
415 locations->InAt(1),
416 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100417 DataType::Type::kInt32,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100418 nullptr);
419 parallel_move.AddMove(
420 locations->InAt(2),
421 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100422 DataType::Type::kReference,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100423 nullptr);
424 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
425
426 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100427 x86_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000428 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100429 RestoreLiveRegisters(codegen, locations);
430 __ jmp(GetExitLabel());
431 }
432
433 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathX86"; }
434
435 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100436 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathX86);
437};
438
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100439// Slow path marking an object reference `ref` during a read
440// barrier. The field `obj.field` in the object `obj` holding this
441// reference does not get updated by this slow path after marking (see
442// ReadBarrierMarkAndUpdateFieldSlowPathX86 below for that).
443//
444// This means that after the execution of this slow path, `ref` will
445// always be up-to-date, but `obj.field` may not; i.e., after the
446// flip, `ref` will be a to-space reference, but `obj.field` will
447// probably still be a from-space reference (unless it gets updated by
448// another thread, or if another thread installed another object
449// reference (different from `ref`) in `obj.field`).
Roland Levillain7c1559a2015-12-15 10:55:36 +0000450class ReadBarrierMarkSlowPathX86 : public SlowPathCode {
451 public:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100452 ReadBarrierMarkSlowPathX86(HInstruction* instruction,
453 Location ref,
454 bool unpoison_ref_before_marking)
455 : SlowPathCode(instruction),
456 ref_(ref),
457 unpoison_ref_before_marking_(unpoison_ref_before_marking) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000458 DCHECK(kEmitCompilerReadBarrier);
459 }
460
461 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathX86"; }
462
463 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
464 LocationSummary* locations = instruction_->GetLocations();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100465 Register ref_reg = ref_.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +0000466 DCHECK(locations->CanCall());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100467 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
Roland Levillain7c1559a2015-12-15 10:55:36 +0000468 DCHECK(instruction_->IsInstanceFieldGet() ||
469 instruction_->IsStaticFieldGet() ||
470 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100471 instruction_->IsArraySet() ||
Roland Levillain7c1559a2015-12-15 10:55:36 +0000472 instruction_->IsLoadClass() ||
473 instruction_->IsLoadString() ||
474 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100475 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100476 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
477 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain7c1559a2015-12-15 10:55:36 +0000478 << "Unexpected instruction in read barrier marking slow path: "
479 << instruction_->DebugName();
480
481 __ Bind(GetEntryLabel());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100482 if (unpoison_ref_before_marking_) {
Vladimir Marko953437b2016-08-24 08:30:46 +0000483 // Object* ref = ref_addr->AsMirrorPtr()
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100484 __ MaybeUnpoisonHeapReference(ref_reg);
Vladimir Marko953437b2016-08-24 08:30:46 +0000485 }
Roland Levillain4359e612016-07-20 11:32:19 +0100486 // No need to save live registers; it's taken care of by the
487 // entrypoint. Also, there is no need to update the stack mask,
488 // as this runtime call will not trigger a garbage collection.
Roland Levillain7c1559a2015-12-15 10:55:36 +0000489 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100490 DCHECK_NE(ref_reg, ESP);
491 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCpuRegisters) << ref_reg;
Roland Levillain02b75802016-07-13 11:54:35 +0100492 // "Compact" slow path, saving two moves.
493 //
494 // Instead of using the standard runtime calling convention (input
495 // and output in EAX):
496 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100497 // EAX <- ref
Roland Levillain02b75802016-07-13 11:54:35 +0100498 // EAX <- ReadBarrierMark(EAX)
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100499 // ref <- EAX
Roland Levillain02b75802016-07-13 11:54:35 +0100500 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100501 // we just use rX (the register containing `ref`) as input and output
Roland Levillain02b75802016-07-13 11:54:35 +0100502 // of a dedicated entrypoint:
503 //
504 // rX <- ReadBarrierMarkRegX(rX)
505 //
Roland Levillain97c46462017-05-11 14:04:03 +0100506 int32_t entry_point_offset = Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(ref_reg);
Roland Levillaindec8f632016-07-22 17:10:06 +0100507 // This runtime call does not require a stack map.
508 x86_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillain7c1559a2015-12-15 10:55:36 +0000509 __ jmp(GetExitLabel());
510 }
511
512 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100513 // The location (register) of the marked object reference.
514 const Location ref_;
515 // Should the reference in `ref_` be unpoisoned prior to marking it?
516 const bool unpoison_ref_before_marking_;
Roland Levillain7c1559a2015-12-15 10:55:36 +0000517
518 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86);
519};
520
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100521// Slow path marking an object reference `ref` during a read barrier,
522// and if needed, atomically updating the field `obj.field` in the
523// object `obj` holding this reference after marking (contrary to
524// ReadBarrierMarkSlowPathX86 above, which never tries to update
525// `obj.field`).
526//
527// This means that after the execution of this slow path, both `ref`
528// and `obj.field` will be up-to-date; i.e., after the flip, both will
529// hold the same to-space reference (unless another thread installed
530// another object reference (different from `ref`) in `obj.field`).
531class ReadBarrierMarkAndUpdateFieldSlowPathX86 : public SlowPathCode {
532 public:
533 ReadBarrierMarkAndUpdateFieldSlowPathX86(HInstruction* instruction,
534 Location ref,
535 Register obj,
536 const Address& field_addr,
537 bool unpoison_ref_before_marking,
538 Register temp)
539 : SlowPathCode(instruction),
540 ref_(ref),
541 obj_(obj),
542 field_addr_(field_addr),
543 unpoison_ref_before_marking_(unpoison_ref_before_marking),
544 temp_(temp) {
545 DCHECK(kEmitCompilerReadBarrier);
546 }
547
548 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkAndUpdateFieldSlowPathX86"; }
549
550 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
551 LocationSummary* locations = instruction_->GetLocations();
552 Register ref_reg = ref_.AsRegister<Register>();
553 DCHECK(locations->CanCall());
554 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
555 // This slow path is only used by the UnsafeCASObject intrinsic.
556 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
557 << "Unexpected instruction in read barrier marking and field updating slow path: "
558 << instruction_->DebugName();
559 DCHECK(instruction_->GetLocations()->Intrinsified());
560 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
561
562 __ Bind(GetEntryLabel());
563 if (unpoison_ref_before_marking_) {
564 // Object* ref = ref_addr->AsMirrorPtr()
565 __ MaybeUnpoisonHeapReference(ref_reg);
566 }
567
568 // Save the old (unpoisoned) reference.
569 __ movl(temp_, ref_reg);
570
571 // No need to save live registers; it's taken care of by the
572 // entrypoint. Also, there is no need to update the stack mask,
573 // as this runtime call will not trigger a garbage collection.
574 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
575 DCHECK_NE(ref_reg, ESP);
576 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCpuRegisters) << ref_reg;
577 // "Compact" slow path, saving two moves.
578 //
579 // Instead of using the standard runtime calling convention (input
580 // and output in EAX):
581 //
582 // EAX <- ref
583 // EAX <- ReadBarrierMark(EAX)
584 // ref <- EAX
585 //
586 // we just use rX (the register containing `ref`) as input and output
587 // of a dedicated entrypoint:
588 //
589 // rX <- ReadBarrierMarkRegX(rX)
590 //
Roland Levillain97c46462017-05-11 14:04:03 +0100591 int32_t entry_point_offset = Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(ref_reg);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100592 // This runtime call does not require a stack map.
593 x86_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
594
595 // If the new reference is different from the old reference,
596 // update the field in the holder (`*field_addr`).
597 //
598 // Note that this field could also hold a different object, if
599 // another thread had concurrently changed it. In that case, the
600 // LOCK CMPXCHGL instruction in the compare-and-set (CAS)
601 // operation below would abort the CAS, leaving the field as-is.
602 NearLabel done;
603 __ cmpl(temp_, ref_reg);
604 __ j(kEqual, &done);
605
606 // Update the the holder's field atomically. This may fail if
607 // mutator updates before us, but it's OK. This is achieved
608 // using a strong compare-and-set (CAS) operation with relaxed
609 // memory synchronization ordering, where the expected value is
610 // the old reference and the desired value is the new reference.
611 // This operation is implemented with a 32-bit LOCK CMPXLCHG
612 // instruction, which requires the expected value (the old
613 // reference) to be in EAX. Save EAX beforehand, and move the
614 // expected value (stored in `temp_`) into EAX.
615 __ pushl(EAX);
616 __ movl(EAX, temp_);
617
618 // Convenience aliases.
619 Register base = obj_;
620 Register expected = EAX;
621 Register value = ref_reg;
622
623 bool base_equals_value = (base == value);
624 if (kPoisonHeapReferences) {
625 if (base_equals_value) {
626 // If `base` and `value` are the same register location, move
627 // `value` to a temporary register. This way, poisoning
628 // `value` won't invalidate `base`.
629 value = temp_;
630 __ movl(value, base);
631 }
632
633 // Check that the register allocator did not assign the location
634 // of `expected` (EAX) to `value` nor to `base`, so that heap
635 // poisoning (when enabled) works as intended below.
636 // - If `value` were equal to `expected`, both references would
637 // be poisoned twice, meaning they would not be poisoned at
638 // all, as heap poisoning uses address negation.
639 // - If `base` were equal to `expected`, poisoning `expected`
640 // would invalidate `base`.
641 DCHECK_NE(value, expected);
642 DCHECK_NE(base, expected);
643
644 __ PoisonHeapReference(expected);
645 __ PoisonHeapReference(value);
646 }
647
648 __ LockCmpxchgl(field_addr_, value);
649
650 // If heap poisoning is enabled, we need to unpoison the values
651 // that were poisoned earlier.
652 if (kPoisonHeapReferences) {
653 if (base_equals_value) {
654 // `value` has been moved to a temporary register, no need
655 // to unpoison it.
656 } else {
657 __ UnpoisonHeapReference(value);
658 }
659 // No need to unpoison `expected` (EAX), as it is be overwritten below.
660 }
661
662 // Restore EAX.
663 __ popl(EAX);
664
665 __ Bind(&done);
666 __ jmp(GetExitLabel());
667 }
668
669 private:
670 // The location (register) of the marked object reference.
671 const Location ref_;
672 // The register containing the object holding the marked object reference field.
673 const Register obj_;
674 // The address of the marked reference field. The base of this address must be `obj_`.
675 const Address field_addr_;
676
677 // Should the reference in `ref_` be unpoisoned prior to marking it?
678 const bool unpoison_ref_before_marking_;
679
680 const Register temp_;
681
682 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathX86);
683};
684
Roland Levillain0d5a2812015-11-13 10:07:31 +0000685// Slow path generating a read barrier for a heap reference.
686class ReadBarrierForHeapReferenceSlowPathX86 : public SlowPathCode {
687 public:
688 ReadBarrierForHeapReferenceSlowPathX86(HInstruction* instruction,
689 Location out,
690 Location ref,
691 Location obj,
692 uint32_t offset,
693 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000694 : SlowPathCode(instruction),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000695 out_(out),
696 ref_(ref),
697 obj_(obj),
698 offset_(offset),
699 index_(index) {
700 DCHECK(kEmitCompilerReadBarrier);
701 // If `obj` is equal to `out` or `ref`, it means the initial object
702 // has been overwritten by (or after) the heap object reference load
703 // to be instrumented, e.g.:
704 //
705 // __ movl(out, Address(out, offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000706 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000707 //
708 // In that case, we have lost the information about the original
709 // object, and the emitted read barrier cannot work properly.
710 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
711 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
712 }
713
714 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
715 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
716 LocationSummary* locations = instruction_->GetLocations();
717 Register reg_out = out_.AsRegister<Register>();
718 DCHECK(locations->CanCall());
719 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain3d312422016-06-23 13:53:42 +0100720 DCHECK(instruction_->IsInstanceFieldGet() ||
721 instruction_->IsStaticFieldGet() ||
722 instruction_->IsArrayGet() ||
723 instruction_->IsInstanceOf() ||
724 instruction_->IsCheckCast() ||
Andreas Gamped9911ee2017-03-27 13:27:24 -0700725 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain7c1559a2015-12-15 10:55:36 +0000726 << "Unexpected instruction in read barrier for heap reference slow path: "
727 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000728
729 __ Bind(GetEntryLabel());
730 SaveLiveRegisters(codegen, locations);
731
732 // We may have to change the index's value, but as `index_` is a
733 // constant member (like other "inputs" of this slow path),
734 // introduce a copy of it, `index`.
735 Location index = index_;
736 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100737 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain0d5a2812015-11-13 10:07:31 +0000738 if (instruction_->IsArrayGet()) {
739 // Compute the actual memory offset and store it in `index`.
740 Register index_reg = index_.AsRegister<Register>();
741 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
742 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
743 // We are about to change the value of `index_reg` (see the
744 // calls to art::x86::X86Assembler::shll and
745 // art::x86::X86Assembler::AddImmediate below), but it has
746 // not been saved by the previous call to
747 // art::SlowPathCode::SaveLiveRegisters, as it is a
748 // callee-save register --
749 // art::SlowPathCode::SaveLiveRegisters does not consider
750 // callee-save registers, as it has been designed with the
751 // assumption that callee-save registers are supposed to be
752 // handled by the called function. So, as a callee-save
753 // register, `index_reg` _would_ eventually be saved onto
754 // the stack, but it would be too late: we would have
755 // changed its value earlier. Therefore, we manually save
756 // it here into another freely available register,
757 // `free_reg`, chosen of course among the caller-save
758 // registers (as a callee-save `free_reg` register would
759 // exhibit the same problem).
760 //
761 // Note we could have requested a temporary register from
762 // the register allocator instead; but we prefer not to, as
763 // this is a slow path, and we know we can find a
764 // caller-save register that is available.
765 Register free_reg = FindAvailableCallerSaveRegister(codegen);
766 __ movl(free_reg, index_reg);
767 index_reg = free_reg;
768 index = Location::RegisterLocation(index_reg);
769 } else {
770 // The initial register stored in `index_` has already been
771 // saved in the call to art::SlowPathCode::SaveLiveRegisters
772 // (as it is not a callee-save register), so we can freely
773 // use it.
774 }
775 // Shifting the index value contained in `index_reg` by the scale
776 // factor (2) cannot overflow in practice, as the runtime is
777 // unable to allocate object arrays with a size larger than
778 // 2^26 - 1 (that is, 2^28 - 4 bytes).
779 __ shll(index_reg, Immediate(TIMES_4));
780 static_assert(
781 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
782 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
783 __ AddImmediate(index_reg, Immediate(offset_));
784 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100785 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
786 // intrinsics, `index_` is not shifted by a scale factor of 2
787 // (as in the case of ArrayGet), as it is actually an offset
788 // to an object field within an object.
789 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000790 DCHECK(instruction_->GetLocations()->Intrinsified());
791 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
792 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
793 << instruction_->AsInvoke()->GetIntrinsic();
794 DCHECK_EQ(offset_, 0U);
795 DCHECK(index_.IsRegisterPair());
796 // UnsafeGet's offset location is a register pair, the low
797 // part contains the correct offset.
798 index = index_.ToLow();
799 }
800 }
801
802 // We're moving two or three locations to locations that could
803 // overlap, so we need a parallel move resolver.
804 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100805 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Roland Levillain0d5a2812015-11-13 10:07:31 +0000806 parallel_move.AddMove(ref_,
807 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100808 DataType::Type::kReference,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000809 nullptr);
810 parallel_move.AddMove(obj_,
811 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100812 DataType::Type::kReference,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000813 nullptr);
814 if (index.IsValid()) {
815 parallel_move.AddMove(index,
816 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100817 DataType::Type::kInt32,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000818 nullptr);
819 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
820 } else {
821 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
822 __ movl(calling_convention.GetRegisterAt(2), Immediate(offset_));
823 }
Serban Constantinescuba45db02016-07-12 22:53:02 +0100824 x86_codegen->InvokeRuntime(kQuickReadBarrierSlow, instruction_, instruction_->GetDexPc(), this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000825 CheckEntrypointTypes<
826 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
827 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
828
829 RestoreLiveRegisters(codegen, locations);
830 __ jmp(GetExitLabel());
831 }
832
833 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathX86"; }
834
835 private:
836 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
837 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
838 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
839 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
840 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
841 return static_cast<Register>(i);
842 }
843 }
844 // We shall never fail to find a free caller-save register, as
845 // there are more than two core caller-save registers on x86
846 // (meaning it is possible to find one which is different from
847 // `ref` and `obj`).
848 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
849 LOG(FATAL) << "Could not find a free caller-save register";
850 UNREACHABLE();
851 }
852
Roland Levillain0d5a2812015-11-13 10:07:31 +0000853 const Location out_;
854 const Location ref_;
855 const Location obj_;
856 const uint32_t offset_;
857 // An additional location containing an index to an array.
858 // Only used for HArrayGet and the UnsafeGetObject &
859 // UnsafeGetObjectVolatile intrinsics.
860 const Location index_;
861
862 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86);
863};
864
865// Slow path generating a read barrier for a GC root.
866class ReadBarrierForRootSlowPathX86 : public SlowPathCode {
867 public:
868 ReadBarrierForRootSlowPathX86(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000869 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000870 DCHECK(kEmitCompilerReadBarrier);
871 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000872
873 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
874 LocationSummary* locations = instruction_->GetLocations();
875 Register reg_out = out_.AsRegister<Register>();
876 DCHECK(locations->CanCall());
877 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000878 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
879 << "Unexpected instruction in read barrier for GC root slow path: "
880 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000881
882 __ Bind(GetEntryLabel());
883 SaveLiveRegisters(codegen, locations);
884
885 InvokeRuntimeCallingConvention calling_convention;
886 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
887 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100888 x86_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000889 instruction_,
890 instruction_->GetDexPc(),
891 this);
892 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
893 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
894
895 RestoreLiveRegisters(codegen, locations);
896 __ jmp(GetExitLabel());
897 }
898
899 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathX86"; }
900
901 private:
Roland Levillain0d5a2812015-11-13 10:07:31 +0000902 const Location out_;
903 const Location root_;
904
905 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86);
906};
907
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100908#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100909// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
910#define __ down_cast<X86Assembler*>(GetAssembler())-> // NOLINT
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100911
Aart Bike9f37602015-10-09 11:15:55 -0700912inline Condition X86Condition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700913 switch (cond) {
914 case kCondEQ: return kEqual;
915 case kCondNE: return kNotEqual;
916 case kCondLT: return kLess;
917 case kCondLE: return kLessEqual;
918 case kCondGT: return kGreater;
919 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700920 case kCondB: return kBelow;
921 case kCondBE: return kBelowEqual;
922 case kCondA: return kAbove;
923 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700924 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100925 LOG(FATAL) << "Unreachable";
926 UNREACHABLE();
927}
928
Aart Bike9f37602015-10-09 11:15:55 -0700929// Maps signed condition to unsigned condition and FP condition to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100930inline Condition X86UnsignedOrFPCondition(IfCondition cond) {
931 switch (cond) {
932 case kCondEQ: return kEqual;
933 case kCondNE: return kNotEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700934 // Signed to unsigned, and FP to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100935 case kCondLT: return kBelow;
936 case kCondLE: return kBelowEqual;
937 case kCondGT: return kAbove;
938 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700939 // Unsigned remain unchanged.
940 case kCondB: return kBelow;
941 case kCondBE: return kBelowEqual;
942 case kCondA: return kAbove;
943 case kCondAE: return kAboveEqual;
Roland Levillain4fa13f62015-07-06 18:11:54 +0100944 }
945 LOG(FATAL) << "Unreachable";
946 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700947}
948
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100949void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100950 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100951}
952
953void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100954 stream << XmmRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100955}
956
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100957size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
958 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
959 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100960}
961
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100962size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
963 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
964 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100965}
966
Mark Mendell7c8d0092015-01-26 11:21:33 -0500967size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Aart Bikb13c65b2017-03-21 20:14:07 -0700968 if (GetGraph()->HasSIMD()) {
Aart Bik5576f372017-03-23 16:17:37 -0700969 __ movups(Address(ESP, stack_index), XmmRegister(reg_id));
Aart Bikb13c65b2017-03-21 20:14:07 -0700970 } else {
971 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
972 }
Mark Mendell7c8d0092015-01-26 11:21:33 -0500973 return GetFloatingPointSpillSlotSize();
974}
975
976size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Aart Bikb13c65b2017-03-21 20:14:07 -0700977 if (GetGraph()->HasSIMD()) {
Aart Bik5576f372017-03-23 16:17:37 -0700978 __ movups(XmmRegister(reg_id), Address(ESP, stack_index));
Aart Bikb13c65b2017-03-21 20:14:07 -0700979 } else {
980 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
981 }
Mark Mendell7c8d0092015-01-26 11:21:33 -0500982 return GetFloatingPointSpillSlotSize();
983}
984
Calin Juravle175dc732015-08-25 15:42:32 +0100985void CodeGeneratorX86::InvokeRuntime(QuickEntrypointEnum entrypoint,
986 HInstruction* instruction,
987 uint32_t dex_pc,
988 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +0100989 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100990 GenerateInvokeRuntime(GetThreadOffset<kX86PointerSize>(entrypoint).Int32Value());
991 if (EntrypointRequiresStackMap(entrypoint)) {
992 RecordPcInfo(instruction, dex_pc, slow_path);
993 }
Alexandre Rames8158f282015-08-07 10:26:17 +0100994}
995
Roland Levillaindec8f632016-07-22 17:10:06 +0100996void CodeGeneratorX86::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
997 HInstruction* instruction,
998 SlowPathCode* slow_path) {
999 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +01001000 GenerateInvokeRuntime(entry_point_offset);
1001}
1002
1003void CodeGeneratorX86::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001004 __ fs()->call(Address::Absolute(entry_point_offset));
1005}
1006
Mark Mendellfb8d2792015-03-31 22:16:59 -04001007CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +00001008 const X86InstructionSetFeatures& isa_features,
1009 const CompilerOptions& compiler_options,
1010 OptimizingCompilerStats* stats)
Mark Mendell5f874182015-03-04 15:42:45 -05001011 : CodeGenerator(graph,
1012 kNumberOfCpuRegisters,
1013 kNumberOfXmmRegisters,
1014 kNumberOfRegisterPairs,
1015 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1016 arraysize(kCoreCalleeSaves))
1017 | (1 << kFakeReturnRegister),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001018 0,
1019 compiler_options,
1020 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +01001021 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001022 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001023 instruction_visitor_(graph, this),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001024 move_resolver_(graph->GetAllocator(), this),
1025 assembler_(graph->GetAllocator()),
Vladimir Marko58155012015-08-19 12:49:41 +00001026 isa_features_(isa_features),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001027 boot_image_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1028 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1029 boot_image_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1030 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001031 boot_image_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001032 string_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1033 jit_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1034 jit_class_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko93205e32016-04-13 11:59:46 +01001035 constant_area_start_(-1),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001036 fixups_to_jump_tables_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001037 method_address_offset_(std::less<uint32_t>(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001038 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001039 // Use a fake return address register to mimic Quick.
1040 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001041}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001042
David Brazdil58282f42016-01-14 12:45:10 +00001043void CodeGeneratorX86::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001044 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001045 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001046}
1047
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001048InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001049 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001050 assembler_(codegen->GetAssembler()),
1051 codegen_(codegen) {}
1052
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001053static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001054 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001055}
1056
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001057void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001058 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001059 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +00001060 bool skip_overflow_check =
1061 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001062 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +00001063
Nicolas Geoffray8d728322018-01-18 22:44:32 +00001064 if (GetCompilerOptions().CountHotnessInCompiledCode()) {
1065 __ addw(Address(kMethodRegisterArgument, ArtMethod::HotnessCountOffset().Int32Value()),
1066 Immediate(1));
1067 }
1068
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001069 if (!skip_overflow_check) {
Vladimir Marko33bff252017-11-01 14:35:42 +00001070 size_t reserved_bytes = GetStackOverflowReservedBytes(InstructionSet::kX86);
1071 __ testl(EAX, Address(ESP, -static_cast<int32_t>(reserved_bytes)));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001072 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +01001073 }
1074
Mark Mendell5f874182015-03-04 15:42:45 -05001075 if (HasEmptyFrame()) {
1076 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001077 }
Mark Mendell5f874182015-03-04 15:42:45 -05001078
1079 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
1080 Register reg = kCoreCalleeSaves[i];
1081 if (allocated_registers_.ContainsCoreRegister(reg)) {
1082 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001083 __ cfi().AdjustCFAOffset(kX86WordSize);
1084 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -05001085 }
1086 }
1087
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001088 int adjust = GetFrameSize() - FrameEntrySpillSize();
1089 __ subl(ESP, Immediate(adjust));
1090 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001091 // Save the current method if we need it. Note that we do not
1092 // do this in HCurrentMethod, as the instruction might have been removed
1093 // in the SSA graph.
1094 if (RequiresCurrentMethod()) {
1095 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
1096 }
Nicolas Geoffrayf7893532017-06-15 12:34:36 +01001097
1098 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1099 // Initialize should_deoptimize flag to 0.
1100 __ movl(Address(ESP, GetStackOffsetOfShouldDeoptimizeFlag()), Immediate(0));
1101 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001102}
1103
1104void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001105 __ cfi().RememberState();
1106 if (!HasEmptyFrame()) {
1107 int adjust = GetFrameSize() - FrameEntrySpillSize();
1108 __ addl(ESP, Immediate(adjust));
1109 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -05001110
David Srbeckyc34dc932015-04-12 09:27:43 +01001111 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1112 Register reg = kCoreCalleeSaves[i];
1113 if (allocated_registers_.ContainsCoreRegister(reg)) {
1114 __ popl(reg);
1115 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
1116 __ cfi().Restore(DWARFReg(reg));
1117 }
Mark Mendell5f874182015-03-04 15:42:45 -05001118 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001119 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001120 __ ret();
1121 __ cfi().RestoreState();
1122 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001123}
1124
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001125void CodeGeneratorX86::Bind(HBasicBlock* block) {
1126 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001127}
1128
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001129Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(DataType::Type type) const {
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001130 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001131 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001132 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001133 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001134 case DataType::Type::kInt8:
1135 case DataType::Type::kUint16:
1136 case DataType::Type::kInt16:
Aart Bik66c158e2018-01-31 12:55:04 -08001137 case DataType::Type::kUint32:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001138 case DataType::Type::kInt32:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001139 return Location::RegisterLocation(EAX);
1140
Aart Bik66c158e2018-01-31 12:55:04 -08001141 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001142 case DataType::Type::kInt64:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001143 return Location::RegisterPairLocation(EAX, EDX);
1144
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001145 case DataType::Type::kVoid:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001146 return Location::NoLocation();
1147
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001148 case DataType::Type::kFloat64:
1149 case DataType::Type::kFloat32:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001150 return Location::FpuRegisterLocation(XMM0);
1151 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001152
1153 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001154}
1155
1156Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
1157 return Location::RegisterLocation(kMethodRegisterArgument);
1158}
1159
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001160Location InvokeDexCallingConventionVisitorX86::GetNextLocation(DataType::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001161 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001162 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001163 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001164 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001165 case DataType::Type::kInt8:
1166 case DataType::Type::kUint16:
1167 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001168 case DataType::Type::kInt32: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001169 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001170 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001171 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001172 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001173 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001174 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001175 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001176 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001177
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001178 case DataType::Type::kInt64: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001179 uint32_t index = gp_index_;
1180 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001181 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001182 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001183 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
1184 calling_convention.GetRegisterPairAt(index));
1185 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001186 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001187 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1188 }
1189 }
1190
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001191 case DataType::Type::kFloat32: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001192 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001193 stack_index_++;
1194 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1195 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1196 } else {
1197 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1198 }
1199 }
1200
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001201 case DataType::Type::kFloat64: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001202 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001203 stack_index_ += 2;
1204 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1205 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1206 } else {
1207 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001208 }
1209 }
1210
Aart Bik66c158e2018-01-31 12:55:04 -08001211 case DataType::Type::kUint32:
1212 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001213 case DataType::Type::kVoid:
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001214 LOG(FATAL) << "Unexpected parameter type " << type;
1215 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001216 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00001217 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001218}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001219
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001220void CodeGeneratorX86::Move32(Location destination, Location source) {
1221 if (source.Equals(destination)) {
1222 return;
1223 }
1224 if (destination.IsRegister()) {
1225 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001226 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001227 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001228 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001229 } else {
1230 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001231 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001232 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001233 } else if (destination.IsFpuRegister()) {
1234 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001235 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001236 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001237 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001238 } else {
1239 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001240 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001241 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001242 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001243 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001244 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001245 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001246 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001247 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05001248 } else if (source.IsConstant()) {
1249 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001250 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05001251 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001252 } else {
1253 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001254 __ pushl(Address(ESP, source.GetStackIndex()));
1255 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001256 }
1257 }
1258}
1259
1260void CodeGeneratorX86::Move64(Location destination, Location source) {
1261 if (source.Equals(destination)) {
1262 return;
1263 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001264 if (destination.IsRegisterPair()) {
1265 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001266 EmitParallelMoves(
1267 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1268 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001269 DataType::Type::kInt32,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001270 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001271 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001272 DataType::Type::kInt32);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001273 } else if (source.IsFpuRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001274 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
1275 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
1276 __ psrlq(src_reg, Immediate(32));
1277 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001278 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001279 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001280 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001281 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
1282 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001283 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
1284 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001285 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05001286 if (source.IsFpuRegister()) {
1287 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
1288 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001289 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001290 } else if (source.IsRegisterPair()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001291 size_t elem_size = DataType::Size(DataType::Type::kInt32);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001292 // Create stack space for 2 elements.
1293 __ subl(ESP, Immediate(2 * elem_size));
1294 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
1295 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
1296 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
1297 // And remove the temporary stack space we allocated.
1298 __ addl(ESP, Immediate(2 * elem_size));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001299 } else {
1300 LOG(FATAL) << "Unimplemented";
1301 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001302 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001303 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001304 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001305 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001306 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001307 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001308 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001309 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001310 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001311 } else if (source.IsConstant()) {
1312 HConstant* constant = source.GetConstant();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001313 DCHECK(constant->IsLongConstant() || constant->IsDoubleConstant());
1314 int64_t value = GetInt64ValueOf(constant);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001315 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001316 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
1317 Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001318 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001319 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001320 EmitParallelMoves(
1321 Location::StackSlot(source.GetStackIndex()),
1322 Location::StackSlot(destination.GetStackIndex()),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001323 DataType::Type::kInt32,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001324 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001325 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001326 DataType::Type::kInt32);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001327 }
1328 }
1329}
1330
Calin Juravle175dc732015-08-25 15:42:32 +01001331void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
1332 DCHECK(location.IsRegister());
1333 __ movl(location.AsRegister<Register>(), Immediate(value));
1334}
1335
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001336void CodeGeneratorX86::MoveLocation(Location dst, Location src, DataType::Type dst_type) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001337 HParallelMove move(GetGraph()->GetAllocator());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001338 if (dst_type == DataType::Type::kInt64 && !src.IsConstant() && !src.IsFpuRegister()) {
1339 move.AddMove(src.ToLow(), dst.ToLow(), DataType::Type::kInt32, nullptr);
1340 move.AddMove(src.ToHigh(), dst.ToHigh(), DataType::Type::kInt32, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001341 } else {
David Brazdil74eb1b22015-12-14 11:44:01 +00001342 move.AddMove(src, dst, dst_type, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001343 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001344 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001345}
1346
1347void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1348 if (location.IsRegister()) {
1349 locations->AddTemp(location);
1350 } else if (location.IsRegisterPair()) {
1351 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1352 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1353 } else {
1354 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1355 }
1356}
1357
David Brazdilfc6a86a2015-06-26 10:33:45 +00001358void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08001359 if (successor->IsExitBlock()) {
1360 DCHECK(got->GetPrevious()->AlwaysThrows());
1361 return; // no code needed
1362 }
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001363
1364 HBasicBlock* block = got->GetBlock();
1365 HInstruction* previous = got->GetPrevious();
1366
1367 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001368 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray8d728322018-01-18 22:44:32 +00001369 if (codegen_->GetCompilerOptions().CountHotnessInCompiledCode()) {
1370 __ pushl(EAX);
1371 __ movl(EAX, Address(ESP, kX86WordSize));
1372 __ addw(Address(EAX, ArtMethod::HotnessCountOffset().Int32Value()), Immediate(1));
1373 __ popl(EAX);
1374 }
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001375 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1376 return;
1377 }
1378
1379 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1380 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1381 }
1382 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001383 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001384 }
1385}
1386
David Brazdilfc6a86a2015-06-26 10:33:45 +00001387void LocationsBuilderX86::VisitGoto(HGoto* got) {
1388 got->SetLocations(nullptr);
1389}
1390
1391void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1392 HandleGoto(got, got->GetSuccessor());
1393}
1394
1395void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1396 try_boundary->SetLocations(nullptr);
1397}
1398
1399void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1400 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1401 if (!successor->IsExitBlock()) {
1402 HandleGoto(try_boundary, successor);
1403 }
1404}
1405
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001406void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001407 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001408}
1409
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001410void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001411}
1412
Mark Mendell152408f2015-12-31 12:28:50 -05001413template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001414void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001415 LabelType* true_label,
1416 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001417 if (cond->IsFPConditionTrueIfNaN()) {
1418 __ j(kUnordered, true_label);
1419 } else if (cond->IsFPConditionFalseIfNaN()) {
1420 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001421 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001422 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001423}
1424
Mark Mendell152408f2015-12-31 12:28:50 -05001425template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001426void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001427 LabelType* true_label,
1428 LabelType* false_label) {
Mark Mendellc4701932015-04-10 13:18:51 -04001429 LocationSummary* locations = cond->GetLocations();
1430 Location left = locations->InAt(0);
1431 Location right = locations->InAt(1);
1432 IfCondition if_cond = cond->GetCondition();
1433
Mark Mendellc4701932015-04-10 13:18:51 -04001434 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001435 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001436 IfCondition true_high_cond = if_cond;
1437 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001438 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001439
1440 // Set the conditions for the test, remembering that == needs to be
1441 // decided using the low words.
1442 switch (if_cond) {
1443 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001444 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001445 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001446 break;
1447 case kCondLT:
1448 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001449 break;
1450 case kCondLE:
1451 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001452 break;
1453 case kCondGT:
1454 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001455 break;
1456 case kCondGE:
1457 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001458 break;
Aart Bike9f37602015-10-09 11:15:55 -07001459 case kCondB:
1460 false_high_cond = kCondA;
1461 break;
1462 case kCondBE:
1463 true_high_cond = kCondB;
1464 break;
1465 case kCondA:
1466 false_high_cond = kCondB;
1467 break;
1468 case kCondAE:
1469 true_high_cond = kCondA;
1470 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001471 }
1472
1473 if (right.IsConstant()) {
1474 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001475 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001476 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001477
Aart Bika19616e2016-02-01 18:57:58 -08001478 codegen_->Compare32BitValue(left_high, val_high);
Mark Mendellc4701932015-04-10 13:18:51 -04001479 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001480 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001481 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001482 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001483 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001484 __ j(X86Condition(true_high_cond), true_label);
1485 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001486 }
1487 // Must be equal high, so compare the lows.
Aart Bika19616e2016-02-01 18:57:58 -08001488 codegen_->Compare32BitValue(left_low, val_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001489 } else if (right.IsRegisterPair()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001490 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001491 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001492
1493 __ cmpl(left_high, right_high);
1494 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001495 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001496 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001497 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001498 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001499 __ j(X86Condition(true_high_cond), true_label);
1500 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001501 }
1502 // Must be equal high, so compare the lows.
1503 __ cmpl(left_low, right_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001504 } else {
1505 DCHECK(right.IsDoubleStackSlot());
1506 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
1507 if (if_cond == kCondNE) {
1508 __ j(X86Condition(true_high_cond), true_label);
1509 } else if (if_cond == kCondEQ) {
1510 __ j(X86Condition(false_high_cond), false_label);
1511 } else {
1512 __ j(X86Condition(true_high_cond), true_label);
1513 __ j(X86Condition(false_high_cond), false_label);
1514 }
1515 // Must be equal high, so compare the lows.
1516 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Mark Mendellc4701932015-04-10 13:18:51 -04001517 }
1518 // The last comparison might be unsigned.
1519 __ j(final_condition, true_label);
1520}
1521
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001522void InstructionCodeGeneratorX86::GenerateFPCompare(Location lhs,
1523 Location rhs,
1524 HInstruction* insn,
1525 bool is_double) {
1526 HX86LoadFromConstantTable* const_area = insn->InputAt(1)->AsX86LoadFromConstantTable();
1527 if (is_double) {
1528 if (rhs.IsFpuRegister()) {
1529 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1530 } else if (const_area != nullptr) {
1531 DCHECK(const_area->IsEmittedAtUseSite());
1532 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(),
1533 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001534 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
1535 const_area->GetBaseMethodAddress(),
1536 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001537 } else {
1538 DCHECK(rhs.IsDoubleStackSlot());
1539 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1540 }
1541 } else {
1542 if (rhs.IsFpuRegister()) {
1543 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1544 } else if (const_area != nullptr) {
1545 DCHECK(const_area->IsEmittedAtUseSite());
1546 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(),
1547 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001548 const_area->GetConstant()->AsFloatConstant()->GetValue(),
1549 const_area->GetBaseMethodAddress(),
1550 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001551 } else {
1552 DCHECK(rhs.IsStackSlot());
1553 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1554 }
1555 }
1556}
1557
Mark Mendell152408f2015-12-31 12:28:50 -05001558template<class LabelType>
David Brazdil0debae72015-11-12 18:37:00 +00001559void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendell152408f2015-12-31 12:28:50 -05001560 LabelType* true_target_in,
1561 LabelType* false_target_in) {
David Brazdil0debae72015-11-12 18:37:00 +00001562 // Generated branching requires both targets to be explicit. If either of the
1563 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Mark Mendell152408f2015-12-31 12:28:50 -05001564 LabelType fallthrough_target;
1565 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1566 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
David Brazdil0debae72015-11-12 18:37:00 +00001567
Mark Mendellc4701932015-04-10 13:18:51 -04001568 LocationSummary* locations = condition->GetLocations();
1569 Location left = locations->InAt(0);
1570 Location right = locations->InAt(1);
1571
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001572 DataType::Type type = condition->InputAt(0)->GetType();
Mark Mendellc4701932015-04-10 13:18:51 -04001573 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001574 case DataType::Type::kInt64:
Mark Mendellc4701932015-04-10 13:18:51 -04001575 GenerateLongComparesAndJumps(condition, true_target, false_target);
1576 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001577 case DataType::Type::kFloat32:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001578 GenerateFPCompare(left, right, condition, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001579 GenerateFPJumps(condition, true_target, false_target);
1580 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001581 case DataType::Type::kFloat64:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001582 GenerateFPCompare(left, right, condition, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001583 GenerateFPJumps(condition, true_target, false_target);
1584 break;
1585 default:
1586 LOG(FATAL) << "Unexpected compare type " << type;
1587 }
1588
David Brazdil0debae72015-11-12 18:37:00 +00001589 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001590 __ jmp(false_target);
1591 }
David Brazdil0debae72015-11-12 18:37:00 +00001592
1593 if (fallthrough_target.IsLinked()) {
1594 __ Bind(&fallthrough_target);
1595 }
Mark Mendellc4701932015-04-10 13:18:51 -04001596}
1597
David Brazdil0debae72015-11-12 18:37:00 +00001598static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1599 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1600 // are set only strictly before `branch`. We can't use the eflags on long/FP
1601 // conditions if they are materialized due to the complex branching.
1602 return cond->IsCondition() &&
1603 cond->GetNext() == branch &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001604 cond->InputAt(0)->GetType() != DataType::Type::kInt64 &&
1605 !DataType::IsFloatingPointType(cond->InputAt(0)->GetType());
David Brazdil0debae72015-11-12 18:37:00 +00001606}
1607
Mark Mendell152408f2015-12-31 12:28:50 -05001608template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001609void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001610 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001611 LabelType* true_target,
1612 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001613 HInstruction* cond = instruction->InputAt(condition_input_index);
1614
1615 if (true_target == nullptr && false_target == nullptr) {
1616 // Nothing to do. The code always falls through.
1617 return;
1618 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001619 // Constant condition, statically compared against "true" (integer value 1).
1620 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001621 if (true_target != nullptr) {
1622 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001623 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001624 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001625 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001626 if (false_target != nullptr) {
1627 __ jmp(false_target);
1628 }
1629 }
1630 return;
1631 }
1632
1633 // The following code generates these patterns:
1634 // (1) true_target == nullptr && false_target != nullptr
1635 // - opposite condition true => branch to false_target
1636 // (2) true_target != nullptr && false_target == nullptr
1637 // - condition true => branch to true_target
1638 // (3) true_target != nullptr && false_target != nullptr
1639 // - condition true => branch to true_target
1640 // - branch to false_target
1641 if (IsBooleanValueOrMaterializedCondition(cond)) {
1642 if (AreEflagsSetFrom(cond, instruction)) {
1643 if (true_target == nullptr) {
1644 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
1645 } else {
1646 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
1647 }
1648 } else {
1649 // Materialized condition, compare against 0.
1650 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1651 if (lhs.IsRegister()) {
1652 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1653 } else {
1654 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1655 }
1656 if (true_target == nullptr) {
1657 __ j(kEqual, false_target);
1658 } else {
1659 __ j(kNotEqual, true_target);
1660 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001661 }
1662 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001663 // Condition has not been materialized, use its inputs as the comparison and
1664 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001665 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001666
1667 // If this is a long or FP comparison that has been folded into
1668 // the HCondition, generate the comparison directly.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001669 DataType::Type type = condition->InputAt(0)->GetType();
1670 if (type == DataType::Type::kInt64 || DataType::IsFloatingPointType(type)) {
David Brazdil0debae72015-11-12 18:37:00 +00001671 GenerateCompareTestAndBranch(condition, true_target, false_target);
1672 return;
1673 }
1674
1675 Location lhs = condition->GetLocations()->InAt(0);
1676 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001677 // LHS is guaranteed to be in a register (see LocationsBuilderX86::HandleCondition).
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001678 codegen_->GenerateIntCompare(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00001679 if (true_target == nullptr) {
1680 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1681 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001682 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001683 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001684 }
David Brazdil0debae72015-11-12 18:37:00 +00001685
1686 // If neither branch falls through (case 3), the conditional branch to `true_target`
1687 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1688 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001689 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001690 }
1691}
1692
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001693void LocationsBuilderX86::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001694 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00001695 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001696 locations->SetInAt(0, Location::Any());
1697 }
1698}
1699
1700void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001701 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1702 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1703 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1704 nullptr : codegen_->GetLabelOf(true_successor);
1705 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1706 nullptr : codegen_->GetLabelOf(false_successor);
1707 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001708}
1709
1710void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001711 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001712 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01001713 InvokeRuntimeCallingConvention calling_convention;
1714 RegisterSet caller_saves = RegisterSet::Empty();
1715 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1716 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00001717 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001718 locations->SetInAt(0, Location::Any());
1719 }
1720}
1721
1722void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001723 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001724 GenerateTestAndBranch<Label>(deoptimize,
1725 /* condition_input_index */ 0,
1726 slow_path->GetEntryLabel(),
1727 /* false_target */ nullptr);
1728}
1729
Mingyao Yang063fc772016-08-02 11:02:54 -07001730void LocationsBuilderX86::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001731 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yang063fc772016-08-02 11:02:54 -07001732 LocationSummary(flag, LocationSummary::kNoCall);
1733 locations->SetOut(Location::RequiresRegister());
1734}
1735
1736void InstructionCodeGeneratorX86::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
1737 __ movl(flag->GetLocations()->Out().AsRegister<Register>(),
1738 Address(ESP, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
1739}
1740
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001741static bool SelectCanUseCMOV(HSelect* select) {
1742 // There are no conditional move instructions for XMMs.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001743 if (DataType::IsFloatingPointType(select->GetType())) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001744 return false;
1745 }
1746
1747 // A FP condition doesn't generate the single CC that we need.
1748 // In 32 bit mode, a long condition doesn't generate a single CC either.
1749 HInstruction* condition = select->GetCondition();
1750 if (condition->IsCondition()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001751 DataType::Type compare_type = condition->InputAt(0)->GetType();
1752 if (compare_type == DataType::Type::kInt64 ||
1753 DataType::IsFloatingPointType(compare_type)) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001754 return false;
1755 }
1756 }
1757
1758 // We can generate a CMOV for this Select.
1759 return true;
1760}
1761
David Brazdil74eb1b22015-12-14 11:44:01 +00001762void LocationsBuilderX86::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001763 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001764 if (DataType::IsFloatingPointType(select->GetType())) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001765 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001766 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001767 } else {
1768 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001769 if (SelectCanUseCMOV(select)) {
1770 if (select->InputAt(1)->IsConstant()) {
1771 // Cmov can't handle a constant value.
1772 locations->SetInAt(1, Location::RequiresRegister());
1773 } else {
1774 locations->SetInAt(1, Location::Any());
1775 }
1776 } else {
1777 locations->SetInAt(1, Location::Any());
1778 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001779 }
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001780 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1781 locations->SetInAt(2, Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00001782 }
1783 locations->SetOut(Location::SameAsFirstInput());
1784}
1785
1786void InstructionCodeGeneratorX86::VisitSelect(HSelect* select) {
1787 LocationSummary* locations = select->GetLocations();
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001788 DCHECK(locations->InAt(0).Equals(locations->Out()));
1789 if (SelectCanUseCMOV(select)) {
1790 // If both the condition and the source types are integer, we can generate
1791 // a CMOV to implement Select.
1792
1793 HInstruction* select_condition = select->GetCondition();
1794 Condition cond = kNotEqual;
1795
1796 // Figure out how to test the 'condition'.
1797 if (select_condition->IsCondition()) {
1798 HCondition* condition = select_condition->AsCondition();
1799 if (!condition->IsEmittedAtUseSite()) {
1800 // This was a previously materialized condition.
1801 // Can we use the existing condition code?
1802 if (AreEflagsSetFrom(condition, select)) {
1803 // Materialization was the previous instruction. Condition codes are right.
1804 cond = X86Condition(condition->GetCondition());
1805 } else {
1806 // No, we have to recreate the condition code.
1807 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1808 __ testl(cond_reg, cond_reg);
1809 }
1810 } else {
1811 // We can't handle FP or long here.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001812 DCHECK_NE(condition->InputAt(0)->GetType(), DataType::Type::kInt64);
1813 DCHECK(!DataType::IsFloatingPointType(condition->InputAt(0)->GetType()));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001814 LocationSummary* cond_locations = condition->GetLocations();
Roland Levillain0b671c02016-08-19 12:02:34 +01001815 codegen_->GenerateIntCompare(cond_locations->InAt(0), cond_locations->InAt(1));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001816 cond = X86Condition(condition->GetCondition());
1817 }
1818 } else {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01001819 // Must be a Boolean condition, which needs to be compared to 0.
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001820 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1821 __ testl(cond_reg, cond_reg);
1822 }
1823
1824 // If the condition is true, overwrite the output, which already contains false.
1825 Location false_loc = locations->InAt(0);
1826 Location true_loc = locations->InAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001827 if (select->GetType() == DataType::Type::kInt64) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001828 // 64 bit conditional move.
1829 Register false_high = false_loc.AsRegisterPairHigh<Register>();
1830 Register false_low = false_loc.AsRegisterPairLow<Register>();
1831 if (true_loc.IsRegisterPair()) {
1832 __ cmovl(cond, false_high, true_loc.AsRegisterPairHigh<Register>());
1833 __ cmovl(cond, false_low, true_loc.AsRegisterPairLow<Register>());
1834 } else {
1835 __ cmovl(cond, false_high, Address(ESP, true_loc.GetHighStackIndex(kX86WordSize)));
1836 __ cmovl(cond, false_low, Address(ESP, true_loc.GetStackIndex()));
1837 }
1838 } else {
1839 // 32 bit conditional move.
1840 Register false_reg = false_loc.AsRegister<Register>();
1841 if (true_loc.IsRegister()) {
1842 __ cmovl(cond, false_reg, true_loc.AsRegister<Register>());
1843 } else {
1844 __ cmovl(cond, false_reg, Address(ESP, true_loc.GetStackIndex()));
1845 }
1846 }
1847 } else {
1848 NearLabel false_target;
1849 GenerateTestAndBranch<NearLabel>(
1850 select, /* condition_input_index */ 2, /* true_target */ nullptr, &false_target);
1851 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1852 __ Bind(&false_target);
1853 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001854}
1855
David Srbecky0cf44932015-12-09 14:09:59 +00001856void LocationsBuilderX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001857 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00001858}
1859
David Srbeckyd28f4a02016-03-14 17:14:24 +00001860void InstructionCodeGeneratorX86::VisitNativeDebugInfo(HNativeDebugInfo*) {
1861 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001862}
1863
1864void CodeGeneratorX86::GenerateNop() {
1865 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001866}
1867
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001868void LocationsBuilderX86::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001869 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01001870 new (GetGraph()->GetAllocator()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001871 // Handle the long/FP comparisons made in instruction simplification.
1872 switch (cond->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001873 case DataType::Type::kInt64: {
Mark Mendellc4701932015-04-10 13:18:51 -04001874 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell8659e842016-02-16 10:41:46 -05001875 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001876 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001877 locations->SetOut(Location::RequiresRegister());
1878 }
1879 break;
1880 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001881 case DataType::Type::kFloat32:
1882 case DataType::Type::kFloat64: {
Mark Mendellc4701932015-04-10 13:18:51 -04001883 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001884 if (cond->InputAt(1)->IsX86LoadFromConstantTable()) {
1885 DCHECK(cond->InputAt(1)->IsEmittedAtUseSite());
1886 } else if (cond->InputAt(1)->IsConstant()) {
1887 locations->SetInAt(1, Location::RequiresFpuRegister());
1888 } else {
1889 locations->SetInAt(1, Location::Any());
1890 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001891 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001892 locations->SetOut(Location::RequiresRegister());
1893 }
1894 break;
1895 }
1896 default:
1897 locations->SetInAt(0, Location::RequiresRegister());
1898 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001899 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001900 // We need a byte register.
1901 locations->SetOut(Location::RegisterLocation(ECX));
1902 }
1903 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001904 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001905}
1906
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001907void InstructionCodeGeneratorX86::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001908 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001909 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001910 }
Mark Mendellc4701932015-04-10 13:18:51 -04001911
1912 LocationSummary* locations = cond->GetLocations();
1913 Location lhs = locations->InAt(0);
1914 Location rhs = locations->InAt(1);
1915 Register reg = locations->Out().AsRegister<Register>();
Mark Mendell152408f2015-12-31 12:28:50 -05001916 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001917
1918 switch (cond->InputAt(0)->GetType()) {
1919 default: {
1920 // Integer case.
1921
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01001922 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04001923 __ xorl(reg, reg);
Roland Levillain0b671c02016-08-19 12:02:34 +01001924 codegen_->GenerateIntCompare(lhs, rhs);
Aart Bike9f37602015-10-09 11:15:55 -07001925 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001926 return;
1927 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001928 case DataType::Type::kInt64:
Mark Mendellc4701932015-04-10 13:18:51 -04001929 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1930 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001931 case DataType::Type::kFloat32:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001932 GenerateFPCompare(lhs, rhs, cond, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001933 GenerateFPJumps(cond, &true_label, &false_label);
1934 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001935 case DataType::Type::kFloat64:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001936 GenerateFPCompare(lhs, rhs, cond, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001937 GenerateFPJumps(cond, &true_label, &false_label);
1938 break;
1939 }
1940
1941 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001942 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001943
Roland Levillain4fa13f62015-07-06 18:11:54 +01001944 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001945 __ Bind(&false_label);
1946 __ xorl(reg, reg);
1947 __ jmp(&done_label);
1948
Roland Levillain4fa13f62015-07-06 18:11:54 +01001949 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001950 __ Bind(&true_label);
1951 __ movl(reg, Immediate(1));
1952 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001953}
1954
1955void LocationsBuilderX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001956 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001957}
1958
1959void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001960 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001961}
1962
1963void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001964 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001965}
1966
1967void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001968 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001969}
1970
1971void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001972 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001973}
1974
1975void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001976 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001977}
1978
1979void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001980 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001981}
1982
1983void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001984 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001985}
1986
1987void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001988 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001989}
1990
1991void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001992 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001993}
1994
1995void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001996 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001997}
1998
1999void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002000 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002001}
2002
Aart Bike9f37602015-10-09 11:15:55 -07002003void LocationsBuilderX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002004 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002005}
2006
2007void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002008 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002009}
2010
2011void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002012 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002013}
2014
2015void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002016 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002017}
2018
2019void LocationsBuilderX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002020 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002021}
2022
2023void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002024 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002025}
2026
2027void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002028 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002029}
2030
2031void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002032 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002033}
2034
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002035void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002036 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002037 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002038 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002039}
2040
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002041void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002042 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002043}
2044
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002045void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
2046 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002047 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002048 locations->SetOut(Location::ConstantLocation(constant));
2049}
2050
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002051void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002052 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002053}
2054
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002055void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002056 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002057 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002058 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002059}
2060
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002061void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002062 // Will be generated at use site.
2063}
2064
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002065void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
2066 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002067 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002068 locations->SetOut(Location::ConstantLocation(constant));
2069}
2070
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002071void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002072 // Will be generated at use site.
2073}
2074
2075void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
2076 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002077 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002078 locations->SetOut(Location::ConstantLocation(constant));
2079}
2080
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002081void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002082 // Will be generated at use site.
2083}
2084
Igor Murashkind01745e2017-04-05 16:40:31 -07002085void LocationsBuilderX86::VisitConstructorFence(HConstructorFence* constructor_fence) {
2086 constructor_fence->SetLocations(nullptr);
2087}
2088
2089void InstructionCodeGeneratorX86::VisitConstructorFence(
2090 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
2091 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
2092}
2093
Calin Juravle27df7582015-04-17 19:12:31 +01002094void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2095 memory_barrier->SetLocations(nullptr);
2096}
2097
2098void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00002099 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002100}
2101
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002102void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002103 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002104}
2105
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002106void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002107 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002108}
2109
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002110void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002111 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002112 new (GetGraph()->GetAllocator()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002113 switch (ret->InputAt(0)->GetType()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002114 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002115 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002116 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002117 case DataType::Type::kInt8:
2118 case DataType::Type::kUint16:
2119 case DataType::Type::kInt16:
2120 case DataType::Type::kInt32:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002121 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002122 break;
2123
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002124 case DataType::Type::kInt64:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002125 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002126 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002127 break;
2128
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002129 case DataType::Type::kFloat32:
2130 case DataType::Type::kFloat64:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002131 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002132 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002133 break;
2134
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002135 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002136 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002137 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002138}
2139
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002140void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002141 if (kIsDebugBuild) {
2142 switch (ret->InputAt(0)->GetType()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002143 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002144 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002145 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002146 case DataType::Type::kInt8:
2147 case DataType::Type::kUint16:
2148 case DataType::Type::kInt16:
2149 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002150 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002151 break;
2152
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002153 case DataType::Type::kInt64:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002154 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
2155 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002156 break;
2157
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002158 case DataType::Type::kFloat32:
2159 case DataType::Type::kFloat64:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002160 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002161 break;
2162
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002163 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002164 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002165 }
2166 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002167 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002168}
2169
Calin Juravle175dc732015-08-25 15:42:32 +01002170void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2171 // The trampoline uses the same calling convention as dex calling conventions,
2172 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2173 // the method_idx.
2174 HandleInvoke(invoke);
2175}
2176
2177void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2178 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2179}
2180
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002181void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002182 // Explicit clinit checks triggered by static invokes must have been pruned by
2183 // art::PrepareForRegisterAllocation.
2184 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002185
Mark Mendellfb8d2792015-03-31 22:16:59 -04002186 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002187 if (intrinsic.TryDispatch(invoke)) {
Vladimir Marko65979462017-05-19 17:25:12 +01002188 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeMethodLoadKind()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00002189 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002190 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04002191 return;
2192 }
2193
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002194 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002195
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002196 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
Vladimir Marko65979462017-05-19 17:25:12 +01002197 if (invoke->HasPcRelativeMethodLoadKind()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00002198 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002199 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002200}
2201
Mark Mendell09ed1a32015-03-25 08:30:06 -04002202static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
2203 if (invoke->GetLocations()->Intrinsified()) {
2204 IntrinsicCodeGeneratorX86 intrinsic(codegen);
2205 intrinsic.Dispatch(invoke);
2206 return true;
2207 }
2208 return false;
2209}
2210
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002211void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002212 // Explicit clinit checks triggered by static invokes must have been pruned by
2213 // art::PrepareForRegisterAllocation.
2214 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002215
Mark Mendell09ed1a32015-03-25 08:30:06 -04002216 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2217 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002218 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002219
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002220 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04002221 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002222 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002223}
2224
2225void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00002226 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
2227 if (intrinsic.TryDispatch(invoke)) {
2228 return;
2229 }
2230
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002231 HandleInvoke(invoke);
2232}
2233
2234void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002235 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002236 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002237}
2238
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002239void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002240 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2241 return;
2242 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002243
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002244 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002245 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002246}
2247
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002248void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002249 // This call to HandleInvoke allocates a temporary (core) register
2250 // which is also used to transfer the hidden argument from FP to
2251 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002252 HandleInvoke(invoke);
2253 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002254 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002255}
2256
2257void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
2258 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002259 LocationSummary* locations = invoke->GetLocations();
2260 Register temp = locations->GetTemp(0).AsRegister<Register>();
2261 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002262 Location receiver = locations->InAt(0);
2263 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2264
Roland Levillain0d5a2812015-11-13 10:07:31 +00002265 // Set the hidden argument. This is safe to do this here, as XMM7
2266 // won't be modified thereafter, before the `call` instruction.
2267 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002268 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002269 __ movd(hidden_reg, temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002270
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002271 if (receiver.IsStackSlot()) {
2272 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002273 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002274 __ movl(temp, Address(temp, class_offset));
2275 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002276 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002277 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002278 }
Roland Levillain4d027112015-07-01 15:41:14 +01002279 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002280 // Instead of simply (possibly) unpoisoning `temp` here, we should
2281 // emit a read barrier for the previous class reference load.
2282 // However this is not required in practice, as this is an
2283 // intermediate/temporary reference and because the current
2284 // concurrent copying collector keeps the from-space memory
2285 // intact/accessible until the end of the marking phase (the
2286 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002287 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002288 // temp = temp->GetAddressOfIMT()
2289 __ movl(temp,
2290 Address(temp, mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002291 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002292 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002293 invoke->GetImtIndex(), kX86PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002294 __ movl(temp, Address(temp, method_offset));
2295 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002296 __ call(Address(temp,
Andreas Gampe542451c2016-07-26 09:02:02 -07002297 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002298
2299 DCHECK(!codegen_->IsLeafMethod());
2300 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2301}
2302
Orion Hodsonac141392017-01-13 11:53:47 +00002303void LocationsBuilderX86::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2304 HandleInvoke(invoke);
2305}
2306
2307void InstructionCodeGeneratorX86::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2308 codegen_->GenerateInvokePolymorphicCall(invoke);
2309}
2310
Roland Levillain88cb1752014-10-20 16:36:47 +01002311void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2312 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002313 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Roland Levillain88cb1752014-10-20 16:36:47 +01002314 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002315 case DataType::Type::kInt32:
2316 case DataType::Type::kInt64:
Roland Levillain88cb1752014-10-20 16:36:47 +01002317 locations->SetInAt(0, Location::RequiresRegister());
2318 locations->SetOut(Location::SameAsFirstInput());
2319 break;
2320
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002321 case DataType::Type::kFloat32:
Roland Levillain5368c212014-11-27 15:03:41 +00002322 locations->SetInAt(0, Location::RequiresFpuRegister());
2323 locations->SetOut(Location::SameAsFirstInput());
2324 locations->AddTemp(Location::RequiresRegister());
2325 locations->AddTemp(Location::RequiresFpuRegister());
2326 break;
2327
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002328 case DataType::Type::kFloat64:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002329 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002330 locations->SetOut(Location::SameAsFirstInput());
2331 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002332 break;
2333
2334 default:
2335 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2336 }
2337}
2338
2339void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2340 LocationSummary* locations = neg->GetLocations();
2341 Location out = locations->Out();
2342 Location in = locations->InAt(0);
2343 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002344 case DataType::Type::kInt32:
Roland Levillain88cb1752014-10-20 16:36:47 +01002345 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002346 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002347 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002348 break;
2349
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002350 case DataType::Type::kInt64:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002351 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002352 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002353 __ negl(out.AsRegisterPairLow<Register>());
2354 // Negation is similar to subtraction from zero. The least
2355 // significant byte triggers a borrow when it is different from
2356 // zero; to take it into account, add 1 to the most significant
2357 // byte if the carry flag (CF) is set to 1 after the first NEGL
2358 // operation.
2359 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2360 __ negl(out.AsRegisterPairHigh<Register>());
2361 break;
2362
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002363 case DataType::Type::kFloat32: {
Roland Levillain5368c212014-11-27 15:03:41 +00002364 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002365 Register constant = locations->GetTemp(0).AsRegister<Register>();
2366 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002367 // Implement float negation with an exclusive or with value
2368 // 0x80000000 (mask for bit 31, representing the sign of a
2369 // single-precision floating-point number).
2370 __ movl(constant, Immediate(INT32_C(0x80000000)));
2371 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002372 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002373 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002374 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002375
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002376 case DataType::Type::kFloat64: {
Roland Levillain5368c212014-11-27 15:03:41 +00002377 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002378 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002379 // Implement double negation with an exclusive or with value
2380 // 0x8000000000000000 (mask for bit 63, representing the sign of
2381 // a double-precision floating-point number).
2382 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002383 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002384 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002385 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002386
2387 default:
2388 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2389 }
2390}
2391
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002392void LocationsBuilderX86::VisitX86FPNeg(HX86FPNeg* neg) {
2393 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002394 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002395 DCHECK(DataType::IsFloatingPointType(neg->GetType()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002396 locations->SetInAt(0, Location::RequiresFpuRegister());
2397 locations->SetInAt(1, Location::RequiresRegister());
2398 locations->SetOut(Location::SameAsFirstInput());
2399 locations->AddTemp(Location::RequiresFpuRegister());
2400}
2401
2402void InstructionCodeGeneratorX86::VisitX86FPNeg(HX86FPNeg* neg) {
2403 LocationSummary* locations = neg->GetLocations();
2404 Location out = locations->Out();
2405 DCHECK(locations->InAt(0).Equals(out));
2406
2407 Register constant_area = locations->InAt(1).AsRegister<Register>();
2408 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002409 if (neg->GetType() == DataType::Type::kFloat32) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00002410 __ movss(mask, codegen_->LiteralInt32Address(INT32_C(0x80000000),
2411 neg->GetBaseMethodAddress(),
2412 constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002413 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
2414 } else {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00002415 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000),
2416 neg->GetBaseMethodAddress(),
2417 constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002418 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
2419 }
2420}
2421
Roland Levillaindff1f282014-11-05 14:15:05 +00002422void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002423 DataType::Type result_type = conversion->GetResultType();
2424 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002425 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
2426 << input_type << " -> " << result_type;
Roland Levillain624279f2014-12-04 11:54:28 +00002427
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002428 // The float-to-long and double-to-long type conversions rely on a
2429 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002430 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002431 ((input_type == DataType::Type::kFloat32 || input_type == DataType::Type::kFloat64)
2432 && result_type == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002433 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002434 : LocationSummary::kNoCall;
2435 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002436 new (GetGraph()->GetAllocator()) LocationSummary(conversion, call_kind);
Roland Levillain624279f2014-12-04 11:54:28 +00002437
Roland Levillaindff1f282014-11-05 14:15:05 +00002438 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002439 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002440 case DataType::Type::kInt8:
Roland Levillain51d3fc42014-11-13 14:11:42 +00002441 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002442 case DataType::Type::kUint8:
2443 case DataType::Type::kInt8:
2444 case DataType::Type::kUint16:
2445 case DataType::Type::kInt16:
2446 case DataType::Type::kInt32:
2447 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2448 // Make the output overlap to please the register allocator. This greatly simplifies
2449 // the validation of the linear scan implementation
2450 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2451 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002452 case DataType::Type::kInt64: {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002453 HInstruction* input = conversion->InputAt(0);
2454 Location input_location = input->IsConstant()
2455 ? Location::ConstantLocation(input->AsConstant())
2456 : Location::RegisterPairLocation(EAX, EDX);
2457 locations->SetInAt(0, input_location);
2458 // Make the output overlap to please the register allocator. This greatly simplifies
2459 // the validation of the linear scan implementation
2460 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2461 break;
2462 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002463
2464 default:
2465 LOG(FATAL) << "Unexpected type conversion from " << input_type
2466 << " to " << result_type;
2467 }
2468 break;
2469
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002470 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002471 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002472 DCHECK(DataType::IsIntegralType(input_type)) << input_type;
2473 locations->SetInAt(0, Location::Any());
2474 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Roland Levillain01a8d712014-11-14 16:27:39 +00002475 break;
2476
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002477 case DataType::Type::kInt32:
Roland Levillain946e1432014-11-11 17:35:19 +00002478 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002479 case DataType::Type::kInt64:
Roland Levillain946e1432014-11-11 17:35:19 +00002480 locations->SetInAt(0, Location::Any());
2481 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2482 break;
2483
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002484 case DataType::Type::kFloat32:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002485 locations->SetInAt(0, Location::RequiresFpuRegister());
2486 locations->SetOut(Location::RequiresRegister());
2487 locations->AddTemp(Location::RequiresFpuRegister());
2488 break;
2489
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002490 case DataType::Type::kFloat64:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002491 locations->SetInAt(0, Location::RequiresFpuRegister());
2492 locations->SetOut(Location::RequiresRegister());
2493 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002494 break;
2495
2496 default:
2497 LOG(FATAL) << "Unexpected type conversion from " << input_type
2498 << " to " << result_type;
2499 }
2500 break;
2501
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002502 case DataType::Type::kInt64:
Roland Levillaindff1f282014-11-05 14:15:05 +00002503 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002504 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002505 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002506 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002507 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002508 case DataType::Type::kInt16:
2509 case DataType::Type::kInt32:
Roland Levillaindff1f282014-11-05 14:15:05 +00002510 locations->SetInAt(0, Location::RegisterLocation(EAX));
2511 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2512 break;
2513
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002514 case DataType::Type::kFloat32:
2515 case DataType::Type::kFloat64: {
Vladimir Marko949c91f2015-01-27 10:48:44 +00002516 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002517 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2518 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2519
Vladimir Marko949c91f2015-01-27 10:48:44 +00002520 // The runtime helper puts the result in EAX, EDX.
2521 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002522 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002523 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002524
2525 default:
2526 LOG(FATAL) << "Unexpected type conversion from " << input_type
2527 << " to " << result_type;
2528 }
2529 break;
2530
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002531 case DataType::Type::kFloat32:
Roland Levillaincff13742014-11-17 14:32:17 +00002532 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002533 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002534 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002535 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002536 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002537 case DataType::Type::kInt16:
2538 case DataType::Type::kInt32:
Roland Levillaincff13742014-11-17 14:32:17 +00002539 locations->SetInAt(0, Location::RequiresRegister());
2540 locations->SetOut(Location::RequiresFpuRegister());
2541 break;
2542
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002543 case DataType::Type::kInt64:
Roland Levillain232ade02015-04-20 15:14:36 +01002544 locations->SetInAt(0, Location::Any());
2545 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002546 break;
2547
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002548 case DataType::Type::kFloat64:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002549 locations->SetInAt(0, Location::RequiresFpuRegister());
2550 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002551 break;
2552
2553 default:
2554 LOG(FATAL) << "Unexpected type conversion from " << input_type
2555 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08002556 }
Roland Levillaincff13742014-11-17 14:32:17 +00002557 break;
2558
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002559 case DataType::Type::kFloat64:
Roland Levillaincff13742014-11-17 14:32:17 +00002560 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002561 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002562 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002563 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002564 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002565 case DataType::Type::kInt16:
2566 case DataType::Type::kInt32:
Roland Levillaincff13742014-11-17 14:32:17 +00002567 locations->SetInAt(0, Location::RequiresRegister());
2568 locations->SetOut(Location::RequiresFpuRegister());
2569 break;
2570
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002571 case DataType::Type::kInt64:
Roland Levillain232ade02015-04-20 15:14:36 +01002572 locations->SetInAt(0, Location::Any());
2573 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002574 break;
2575
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002576 case DataType::Type::kFloat32:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002577 locations->SetInAt(0, Location::RequiresFpuRegister());
2578 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002579 break;
2580
2581 default:
2582 LOG(FATAL) << "Unexpected type conversion from " << input_type
2583 << " to " << result_type;
2584 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002585 break;
2586
2587 default:
2588 LOG(FATAL) << "Unexpected type conversion from " << input_type
2589 << " to " << result_type;
2590 }
2591}
2592
2593void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2594 LocationSummary* locations = conversion->GetLocations();
2595 Location out = locations->Out();
2596 Location in = locations->InAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002597 DataType::Type result_type = conversion->GetResultType();
2598 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002599 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
2600 << input_type << " -> " << result_type;
Roland Levillaindff1f282014-11-05 14:15:05 +00002601 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002602 case DataType::Type::kUint8:
2603 switch (input_type) {
2604 case DataType::Type::kInt8:
2605 case DataType::Type::kUint16:
2606 case DataType::Type::kInt16:
2607 case DataType::Type::kInt32:
2608 if (in.IsRegister()) {
2609 __ movzxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
2610 } else {
2611 DCHECK(in.GetConstant()->IsIntConstant());
2612 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2613 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint8_t>(value)));
2614 }
2615 break;
2616 case DataType::Type::kInt64:
2617 if (in.IsRegisterPair()) {
2618 __ movzxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2619 } else {
2620 DCHECK(in.GetConstant()->IsLongConstant());
2621 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2622 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint8_t>(value)));
2623 }
2624 break;
2625
2626 default:
2627 LOG(FATAL) << "Unexpected type conversion from " << input_type
2628 << " to " << result_type;
2629 }
2630 break;
2631
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002632 case DataType::Type::kInt8:
Roland Levillain51d3fc42014-11-13 14:11:42 +00002633 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002634 case DataType::Type::kUint8:
2635 case DataType::Type::kUint16:
2636 case DataType::Type::kInt16:
2637 case DataType::Type::kInt32:
2638 if (in.IsRegister()) {
2639 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
2640 } else {
2641 DCHECK(in.GetConstant()->IsIntConstant());
2642 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2643 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2644 }
2645 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002646 case DataType::Type::kInt64:
Vladimir Markob52bbde2016-02-12 12:06:05 +00002647 if (in.IsRegisterPair()) {
2648 __ movsxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2649 } else {
2650 DCHECK(in.GetConstant()->IsLongConstant());
2651 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2652 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2653 }
2654 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002655
2656 default:
2657 LOG(FATAL) << "Unexpected type conversion from " << input_type
2658 << " to " << result_type;
2659 }
2660 break;
2661
2662 case DataType::Type::kUint16:
2663 switch (input_type) {
2664 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002665 case DataType::Type::kInt16:
2666 case DataType::Type::kInt32:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002667 if (in.IsRegister()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002668 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
2669 } else if (in.IsStackSlot()) {
2670 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002671 } else {
2672 DCHECK(in.GetConstant()->IsIntConstant());
2673 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002674 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
2675 }
2676 break;
2677 case DataType::Type::kInt64:
2678 if (in.IsRegisterPair()) {
2679 __ movzxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2680 } else if (in.IsDoubleStackSlot()) {
2681 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2682 } else {
2683 DCHECK(in.GetConstant()->IsLongConstant());
2684 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2685 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002686 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002687 break;
2688
2689 default:
2690 LOG(FATAL) << "Unexpected type conversion from " << input_type
2691 << " to " << result_type;
2692 }
2693 break;
2694
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002695 case DataType::Type::kInt16:
Roland Levillain01a8d712014-11-14 16:27:39 +00002696 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002697 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002698 case DataType::Type::kInt32:
Roland Levillain01a8d712014-11-14 16:27:39 +00002699 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002700 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002701 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002702 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002703 } else {
2704 DCHECK(in.GetConstant()->IsIntConstant());
2705 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002706 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002707 }
2708 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002709 case DataType::Type::kInt64:
2710 if (in.IsRegisterPair()) {
2711 __ movsxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2712 } else if (in.IsDoubleStackSlot()) {
2713 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2714 } else {
2715 DCHECK(in.GetConstant()->IsLongConstant());
2716 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2717 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
2718 }
2719 break;
Roland Levillain01a8d712014-11-14 16:27:39 +00002720
2721 default:
2722 LOG(FATAL) << "Unexpected type conversion from " << input_type
2723 << " to " << result_type;
2724 }
2725 break;
2726
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002727 case DataType::Type::kInt32:
Roland Levillain946e1432014-11-11 17:35:19 +00002728 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002729 case DataType::Type::kInt64:
Roland Levillain946e1432014-11-11 17:35:19 +00002730 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002731 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002732 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002733 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002734 } else {
2735 DCHECK(in.IsConstant());
2736 DCHECK(in.GetConstant()->IsLongConstant());
2737 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002738 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002739 }
2740 break;
2741
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002742 case DataType::Type::kFloat32: {
Roland Levillain3f8f9362014-12-02 17:45:01 +00002743 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2744 Register output = out.AsRegister<Register>();
2745 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002746 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002747
2748 __ movl(output, Immediate(kPrimIntMax));
2749 // temp = int-to-float(output)
2750 __ cvtsi2ss(temp, output);
2751 // if input >= temp goto done
2752 __ comiss(input, temp);
2753 __ j(kAboveEqual, &done);
2754 // if input == NaN goto nan
2755 __ j(kUnordered, &nan);
2756 // output = float-to-int-truncate(input)
2757 __ cvttss2si(output, input);
2758 __ jmp(&done);
2759 __ Bind(&nan);
2760 // output = 0
2761 __ xorl(output, output);
2762 __ Bind(&done);
2763 break;
2764 }
2765
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002766 case DataType::Type::kFloat64: {
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002767 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2768 Register output = out.AsRegister<Register>();
2769 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002770 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002771
2772 __ movl(output, Immediate(kPrimIntMax));
2773 // temp = int-to-double(output)
2774 __ cvtsi2sd(temp, output);
2775 // if input >= temp goto done
2776 __ comisd(input, temp);
2777 __ j(kAboveEqual, &done);
2778 // if input == NaN goto nan
2779 __ j(kUnordered, &nan);
2780 // output = double-to-int-truncate(input)
2781 __ cvttsd2si(output, input);
2782 __ jmp(&done);
2783 __ Bind(&nan);
2784 // output = 0
2785 __ xorl(output, output);
2786 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002787 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002788 }
Roland Levillain946e1432014-11-11 17:35:19 +00002789
2790 default:
2791 LOG(FATAL) << "Unexpected type conversion from " << input_type
2792 << " to " << result_type;
2793 }
2794 break;
2795
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002796 case DataType::Type::kInt64:
Roland Levillaindff1f282014-11-05 14:15:05 +00002797 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002798 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002799 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002800 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002801 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002802 case DataType::Type::kInt16:
2803 case DataType::Type::kInt32:
Roland Levillaindff1f282014-11-05 14:15:05 +00002804 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2805 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002806 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002807 __ cdq();
2808 break;
2809
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002810 case DataType::Type::kFloat32:
Serban Constantinescuba45db02016-07-12 22:53:02 +01002811 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002812 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002813 break;
2814
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002815 case DataType::Type::kFloat64:
Serban Constantinescuba45db02016-07-12 22:53:02 +01002816 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002817 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002818 break;
2819
2820 default:
2821 LOG(FATAL) << "Unexpected type conversion from " << input_type
2822 << " to " << result_type;
2823 }
2824 break;
2825
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002826 case DataType::Type::kFloat32:
Roland Levillaincff13742014-11-17 14:32:17 +00002827 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002828 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002829 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002830 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002831 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002832 case DataType::Type::kInt16:
2833 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002834 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002835 break;
2836
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002837 case DataType::Type::kInt64: {
Roland Levillain232ade02015-04-20 15:14:36 +01002838 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002839
Roland Levillain232ade02015-04-20 15:14:36 +01002840 // Create stack space for the call to
2841 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2842 // TODO: enhance register allocator to ask for stack temporaries.
2843 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002844 adjustment = DataType::Size(DataType::Type::kInt64);
Roland Levillain232ade02015-04-20 15:14:36 +01002845 __ subl(ESP, Immediate(adjustment));
2846 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002847
Roland Levillain232ade02015-04-20 15:14:36 +01002848 // Load the value to the FP stack, using temporaries if needed.
2849 PushOntoFPStack(in, 0, adjustment, false, true);
2850
2851 if (out.IsStackSlot()) {
2852 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2853 } else {
2854 __ fstps(Address(ESP, 0));
2855 Location stack_temp = Location::StackSlot(0);
2856 codegen_->Move32(out, stack_temp);
2857 }
2858
2859 // Remove the temporary stack space we allocated.
2860 if (adjustment != 0) {
2861 __ addl(ESP, Immediate(adjustment));
2862 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002863 break;
2864 }
2865
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002866 case DataType::Type::kFloat64:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002867 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002868 break;
2869
2870 default:
2871 LOG(FATAL) << "Unexpected type conversion from " << input_type
2872 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08002873 }
Roland Levillaincff13742014-11-17 14:32:17 +00002874 break;
2875
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002876 case DataType::Type::kFloat64:
Roland Levillaincff13742014-11-17 14:32:17 +00002877 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002878 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002879 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002880 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002881 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002882 case DataType::Type::kInt16:
2883 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002884 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002885 break;
2886
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002887 case DataType::Type::kInt64: {
Roland Levillain232ade02015-04-20 15:14:36 +01002888 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002889
Roland Levillain232ade02015-04-20 15:14:36 +01002890 // Create stack space for the call to
2891 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2892 // TODO: enhance register allocator to ask for stack temporaries.
2893 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002894 adjustment = DataType::Size(DataType::Type::kInt64);
Roland Levillain232ade02015-04-20 15:14:36 +01002895 __ subl(ESP, Immediate(adjustment));
2896 }
2897
2898 // Load the value to the FP stack, using temporaries if needed.
2899 PushOntoFPStack(in, 0, adjustment, false, true);
2900
2901 if (out.IsDoubleStackSlot()) {
2902 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2903 } else {
2904 __ fstpl(Address(ESP, 0));
2905 Location stack_temp = Location::DoubleStackSlot(0);
2906 codegen_->Move64(out, stack_temp);
2907 }
2908
2909 // Remove the temporary stack space we allocated.
2910 if (adjustment != 0) {
2911 __ addl(ESP, Immediate(adjustment));
2912 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002913 break;
2914 }
2915
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002916 case DataType::Type::kFloat32:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002917 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002918 break;
2919
2920 default:
2921 LOG(FATAL) << "Unexpected type conversion from " << input_type
2922 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08002923 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002924 break;
2925
2926 default:
2927 LOG(FATAL) << "Unexpected type conversion from " << input_type
2928 << " to " << result_type;
2929 }
2930}
2931
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002932void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002933 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002934 new (GetGraph()->GetAllocator()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002935 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002936 case DataType::Type::kInt32: {
Mark Mendell09b84632015-02-13 17:48:38 -05002937 locations->SetInAt(0, Location::RequiresRegister());
2938 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2939 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2940 break;
2941 }
2942
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002943 case DataType::Type::kInt64: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002944 locations->SetInAt(0, Location::RequiresRegister());
2945 locations->SetInAt(1, Location::Any());
2946 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002947 break;
2948 }
2949
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002950 case DataType::Type::kFloat32:
2951 case DataType::Type::kFloat64: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002952 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002953 if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2954 DCHECK(add->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002955 } else if (add->InputAt(1)->IsConstant()) {
2956 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002957 } else {
2958 locations->SetInAt(1, Location::Any());
2959 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002960 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002961 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002962 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002963
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002964 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002965 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2966 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002967 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002968}
2969
2970void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2971 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002972 Location first = locations->InAt(0);
2973 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002974 Location out = locations->Out();
2975
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002976 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002977 case DataType::Type::kInt32: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002978 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002979 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2980 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002981 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2982 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05002983 } else {
2984 __ leal(out.AsRegister<Register>(), Address(
2985 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
2986 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002987 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002988 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
2989 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2990 __ addl(out.AsRegister<Register>(), Immediate(value));
2991 } else {
2992 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
2993 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002994 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05002995 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002996 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002997 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002998 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002999 }
3000
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003001 case DataType::Type::kInt64: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003002 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003003 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3004 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003005 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003006 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3007 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003008 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003009 } else {
3010 DCHECK(second.IsConstant()) << second;
3011 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3012 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3013 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003014 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003015 break;
3016 }
3017
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003018 case DataType::Type::kFloat32: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003019 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003020 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04003021 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3022 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003023 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003024 __ addss(first.AsFpuRegister<XmmRegister>(),
3025 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003026 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3027 const_area->GetBaseMethodAddress(),
3028 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003029 } else {
3030 DCHECK(second.IsStackSlot());
3031 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003032 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003033 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003034 }
3035
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003036 case DataType::Type::kFloat64: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003037 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003038 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04003039 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3040 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003041 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003042 __ addsd(first.AsFpuRegister<XmmRegister>(),
3043 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003044 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3045 const_area->GetBaseMethodAddress(),
3046 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003047 } else {
3048 DCHECK(second.IsDoubleStackSlot());
3049 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003050 }
3051 break;
3052 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003053
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003054 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003055 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003056 }
3057}
3058
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003059void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003060 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003061 new (GetGraph()->GetAllocator()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003062 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003063 case DataType::Type::kInt32:
3064 case DataType::Type::kInt64: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003065 locations->SetInAt(0, Location::RequiresRegister());
3066 locations->SetInAt(1, Location::Any());
3067 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003068 break;
3069 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003070 case DataType::Type::kFloat32:
3071 case DataType::Type::kFloat64: {
Calin Juravle11351682014-10-23 15:38:15 +01003072 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003073 if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3074 DCHECK(sub->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003075 } else if (sub->InputAt(1)->IsConstant()) {
3076 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003077 } else {
3078 locations->SetInAt(1, Location::Any());
3079 }
Calin Juravle11351682014-10-23 15:38:15 +01003080 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003081 break;
Calin Juravle11351682014-10-23 15:38:15 +01003082 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003083
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003084 default:
Calin Juravle11351682014-10-23 15:38:15 +01003085 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003086 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003087}
3088
3089void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
3090 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003091 Location first = locations->InAt(0);
3092 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01003093 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003094 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003095 case DataType::Type::kInt32: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003096 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003097 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003098 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003099 __ subl(first.AsRegister<Register>(),
3100 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003101 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003102 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003103 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003104 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003105 }
3106
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003107 case DataType::Type::kInt64: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003108 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01003109 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3110 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003111 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01003112 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003113 __ sbbl(first.AsRegisterPairHigh<Register>(),
3114 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003115 } else {
3116 DCHECK(second.IsConstant()) << second;
3117 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3118 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3119 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003120 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003121 break;
3122 }
3123
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003124 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003125 if (second.IsFpuRegister()) {
3126 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3127 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3128 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003129 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003130 __ subss(first.AsFpuRegister<XmmRegister>(),
3131 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003132 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3133 const_area->GetBaseMethodAddress(),
3134 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003135 } else {
3136 DCHECK(second.IsStackSlot());
3137 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3138 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003139 break;
Calin Juravle11351682014-10-23 15:38:15 +01003140 }
3141
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003142 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003143 if (second.IsFpuRegister()) {
3144 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3145 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3146 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003147 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003148 __ subsd(first.AsFpuRegister<XmmRegister>(),
3149 codegen_->LiteralDoubleAddress(
3150 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003151 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003152 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3153 } else {
3154 DCHECK(second.IsDoubleStackSlot());
3155 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3156 }
Calin Juravle11351682014-10-23 15:38:15 +01003157 break;
3158 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003159
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003160 default:
Calin Juravle11351682014-10-23 15:38:15 +01003161 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003162 }
3163}
3164
Calin Juravle34bacdf2014-10-07 20:23:36 +01003165void LocationsBuilderX86::VisitMul(HMul* mul) {
3166 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003167 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Calin Juravle34bacdf2014-10-07 20:23:36 +01003168 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003169 case DataType::Type::kInt32:
Calin Juravle34bacdf2014-10-07 20:23:36 +01003170 locations->SetInAt(0, Location::RequiresRegister());
3171 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003172 if (mul->InputAt(1)->IsIntConstant()) {
3173 // Can use 3 operand multiply.
3174 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3175 } else {
3176 locations->SetOut(Location::SameAsFirstInput());
3177 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003178 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003179 case DataType::Type::kInt64: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003180 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003181 locations->SetInAt(1, Location::Any());
3182 locations->SetOut(Location::SameAsFirstInput());
3183 // Needed for imul on 32bits with 64bits output.
3184 locations->AddTemp(Location::RegisterLocation(EAX));
3185 locations->AddTemp(Location::RegisterLocation(EDX));
3186 break;
3187 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003188 case DataType::Type::kFloat32:
3189 case DataType::Type::kFloat64: {
Calin Juravleb5bfa962014-10-21 18:02:24 +01003190 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003191 if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3192 DCHECK(mul->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003193 } else if (mul->InputAt(1)->IsConstant()) {
3194 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003195 } else {
3196 locations->SetInAt(1, Location::Any());
3197 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003198 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003199 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003200 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003201
3202 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003203 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003204 }
3205}
3206
3207void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
3208 LocationSummary* locations = mul->GetLocations();
3209 Location first = locations->InAt(0);
3210 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003211 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003212
3213 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003214 case DataType::Type::kInt32:
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003215 // The constant may have ended up in a register, so test explicitly to avoid
3216 // problems where the output may not be the same as the first operand.
3217 if (mul->InputAt(1)->IsIntConstant()) {
3218 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3219 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
3220 } else if (second.IsRegister()) {
3221 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003222 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003223 } else {
3224 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003225 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003226 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003227 }
3228 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003229
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003230 case DataType::Type::kInt64: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003231 Register in1_hi = first.AsRegisterPairHigh<Register>();
3232 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003233 Register eax = locations->GetTemp(0).AsRegister<Register>();
3234 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003235
3236 DCHECK_EQ(EAX, eax);
3237 DCHECK_EQ(EDX, edx);
3238
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003239 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01003240 // output: in1
3241 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3242 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3243 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003244 if (second.IsConstant()) {
3245 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003246
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003247 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3248 int32_t low_value = Low32Bits(value);
3249 int32_t high_value = High32Bits(value);
3250 Immediate low(low_value);
3251 Immediate high(high_value);
3252
3253 __ movl(eax, high);
3254 // eax <- in1.lo * in2.hi
3255 __ imull(eax, in1_lo);
3256 // in1.hi <- in1.hi * in2.lo
3257 __ imull(in1_hi, low);
3258 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3259 __ addl(in1_hi, eax);
3260 // move in2_lo to eax to prepare for double precision
3261 __ movl(eax, low);
3262 // edx:eax <- in1.lo * in2.lo
3263 __ mull(in1_lo);
3264 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3265 __ addl(in1_hi, edx);
3266 // in1.lo <- (in1.lo * in2.lo)[31:0];
3267 __ movl(in1_lo, eax);
3268 } else if (second.IsRegisterPair()) {
3269 Register in2_hi = second.AsRegisterPairHigh<Register>();
3270 Register in2_lo = second.AsRegisterPairLow<Register>();
3271
3272 __ movl(eax, in2_hi);
3273 // eax <- in1.lo * in2.hi
3274 __ imull(eax, in1_lo);
3275 // in1.hi <- in1.hi * in2.lo
3276 __ imull(in1_hi, in2_lo);
3277 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3278 __ addl(in1_hi, eax);
3279 // move in1_lo to eax to prepare for double precision
3280 __ movl(eax, in1_lo);
3281 // edx:eax <- in1.lo * in2.lo
3282 __ mull(in2_lo);
3283 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3284 __ addl(in1_hi, edx);
3285 // in1.lo <- (in1.lo * in2.lo)[31:0];
3286 __ movl(in1_lo, eax);
3287 } else {
3288 DCHECK(second.IsDoubleStackSlot()) << second;
3289 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
3290 Address in2_lo(ESP, second.GetStackIndex());
3291
3292 __ movl(eax, in2_hi);
3293 // eax <- in1.lo * in2.hi
3294 __ imull(eax, in1_lo);
3295 // in1.hi <- in1.hi * in2.lo
3296 __ imull(in1_hi, in2_lo);
3297 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3298 __ addl(in1_hi, eax);
3299 // move in1_lo to eax to prepare for double precision
3300 __ movl(eax, in1_lo);
3301 // edx:eax <- in1.lo * in2.lo
3302 __ mull(in2_lo);
3303 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3304 __ addl(in1_hi, edx);
3305 // in1.lo <- (in1.lo * in2.lo)[31:0];
3306 __ movl(in1_lo, eax);
3307 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003308
3309 break;
3310 }
3311
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003312 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003313 DCHECK(first.Equals(locations->Out()));
3314 if (second.IsFpuRegister()) {
3315 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3316 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3317 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003318 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003319 __ mulss(first.AsFpuRegister<XmmRegister>(),
3320 codegen_->LiteralFloatAddress(
3321 const_area->GetConstant()->AsFloatConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003322 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003323 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3324 } else {
3325 DCHECK(second.IsStackSlot());
3326 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3327 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003328 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003329 }
3330
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003331 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003332 DCHECK(first.Equals(locations->Out()));
3333 if (second.IsFpuRegister()) {
3334 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3335 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3336 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003337 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003338 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3339 codegen_->LiteralDoubleAddress(
3340 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003341 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003342 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3343 } else {
3344 DCHECK(second.IsDoubleStackSlot());
3345 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3346 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003347 break;
3348 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003349
3350 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003351 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003352 }
3353}
3354
Roland Levillain232ade02015-04-20 15:14:36 +01003355void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3356 uint32_t temp_offset,
3357 uint32_t stack_adjustment,
3358 bool is_fp,
3359 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003360 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003361 DCHECK(!is_wide);
3362 if (is_fp) {
3363 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3364 } else {
3365 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3366 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003367 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003368 DCHECK(is_wide);
3369 if (is_fp) {
3370 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3371 } else {
3372 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3373 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003374 } else {
3375 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003376 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003377 Location stack_temp = Location::StackSlot(temp_offset);
3378 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003379 if (is_fp) {
3380 __ flds(Address(ESP, temp_offset));
3381 } else {
3382 __ filds(Address(ESP, temp_offset));
3383 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003384 } else {
3385 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3386 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003387 if (is_fp) {
3388 __ fldl(Address(ESP, temp_offset));
3389 } else {
3390 __ fildl(Address(ESP, temp_offset));
3391 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003392 }
3393 }
3394}
3395
3396void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003397 DataType::Type type = rem->GetResultType();
3398 bool is_float = type == DataType::Type::kFloat32;
3399 size_t elem_size = DataType::Size(type);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003400 LocationSummary* locations = rem->GetLocations();
3401 Location first = locations->InAt(0);
3402 Location second = locations->InAt(1);
3403 Location out = locations->Out();
3404
3405 // Create stack space for 2 elements.
3406 // TODO: enhance register allocator to ask for stack temporaries.
3407 __ subl(ESP, Immediate(2 * elem_size));
3408
3409 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003410 const bool is_wide = !is_float;
3411 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
3412 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003413
3414 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003415 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003416 __ Bind(&retry);
3417 __ fprem();
3418
3419 // Move FP status to AX.
3420 __ fstsw();
3421
3422 // And see if the argument reduction is complete. This is signaled by the
3423 // C2 FPU flag bit set to 0.
3424 __ andl(EAX, Immediate(kC2ConditionMask));
3425 __ j(kNotEqual, &retry);
3426
3427 // We have settled on the final value. Retrieve it into an XMM register.
3428 // Store FP top of stack to real stack.
3429 if (is_float) {
3430 __ fsts(Address(ESP, 0));
3431 } else {
3432 __ fstl(Address(ESP, 0));
3433 }
3434
3435 // Pop the 2 items from the FP stack.
3436 __ fucompp();
3437
3438 // Load the value from the stack into an XMM register.
3439 DCHECK(out.IsFpuRegister()) << out;
3440 if (is_float) {
3441 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3442 } else {
3443 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3444 }
3445
3446 // And remove the temporary stack space we allocated.
3447 __ addl(ESP, Immediate(2 * elem_size));
3448}
3449
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003450
3451void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3452 DCHECK(instruction->IsDiv() || instruction->IsRem());
3453
3454 LocationSummary* locations = instruction->GetLocations();
3455 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003456 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003457
3458 Register out_register = locations->Out().AsRegister<Register>();
3459 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003460 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003461
3462 DCHECK(imm == 1 || imm == -1);
3463
3464 if (instruction->IsRem()) {
3465 __ xorl(out_register, out_register);
3466 } else {
3467 __ movl(out_register, input_register);
3468 if (imm == -1) {
3469 __ negl(out_register);
3470 }
3471 }
3472}
3473
3474
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003475void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003476 LocationSummary* locations = instruction->GetLocations();
3477
3478 Register out_register = locations->Out().AsRegister<Register>();
3479 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003480 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003481 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3482 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003483
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003484 Register num = locations->GetTemp(0).AsRegister<Register>();
3485
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003486 __ leal(num, Address(input_register, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003487 __ testl(input_register, input_register);
3488 __ cmovl(kGreaterEqual, num, input_register);
3489 int shift = CTZ(imm);
3490 __ sarl(num, Immediate(shift));
3491
3492 if (imm < 0) {
3493 __ negl(num);
3494 }
3495
3496 __ movl(out_register, num);
3497}
3498
3499void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3500 DCHECK(instruction->IsDiv() || instruction->IsRem());
3501
3502 LocationSummary* locations = instruction->GetLocations();
3503 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3504
3505 Register eax = locations->InAt(0).AsRegister<Register>();
3506 Register out = locations->Out().AsRegister<Register>();
3507 Register num;
3508 Register edx;
3509
3510 if (instruction->IsDiv()) {
3511 edx = locations->GetTemp(0).AsRegister<Register>();
3512 num = locations->GetTemp(1).AsRegister<Register>();
3513 } else {
3514 edx = locations->Out().AsRegister<Register>();
3515 num = locations->GetTemp(0).AsRegister<Register>();
3516 }
3517
3518 DCHECK_EQ(EAX, eax);
3519 DCHECK_EQ(EDX, edx);
3520 if (instruction->IsDiv()) {
3521 DCHECK_EQ(EAX, out);
3522 } else {
3523 DCHECK_EQ(EDX, out);
3524 }
3525
3526 int64_t magic;
3527 int shift;
3528 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3529
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003530 // Save the numerator.
3531 __ movl(num, eax);
3532
3533 // EAX = magic
3534 __ movl(eax, Immediate(magic));
3535
3536 // EDX:EAX = magic * numerator
3537 __ imull(num);
3538
3539 if (imm > 0 && magic < 0) {
3540 // EDX += num
3541 __ addl(edx, num);
3542 } else if (imm < 0 && magic > 0) {
3543 __ subl(edx, num);
3544 }
3545
3546 // Shift if needed.
3547 if (shift != 0) {
3548 __ sarl(edx, Immediate(shift));
3549 }
3550
3551 // EDX += 1 if EDX < 0
3552 __ movl(eax, edx);
3553 __ shrl(edx, Immediate(31));
3554 __ addl(edx, eax);
3555
3556 if (instruction->IsRem()) {
3557 __ movl(eax, num);
3558 __ imull(edx, Immediate(imm));
3559 __ subl(eax, edx);
3560 __ movl(edx, eax);
3561 } else {
3562 __ movl(eax, edx);
3563 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003564}
3565
Calin Juravlebacfec32014-11-14 15:54:36 +00003566void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3567 DCHECK(instruction->IsDiv() || instruction->IsRem());
3568
3569 LocationSummary* locations = instruction->GetLocations();
3570 Location out = locations->Out();
3571 Location first = locations->InAt(0);
3572 Location second = locations->InAt(1);
3573 bool is_div = instruction->IsDiv();
3574
3575 switch (instruction->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003576 case DataType::Type::kInt32: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003577 DCHECK_EQ(EAX, first.AsRegister<Register>());
3578 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003579
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003580 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003581 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003582
3583 if (imm == 0) {
3584 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3585 } else if (imm == 1 || imm == -1) {
3586 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003587 } else if (is_div && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003588 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003589 } else {
3590 DCHECK(imm <= -2 || imm >= 2);
3591 GenerateDivRemWithAnyConstant(instruction);
3592 }
3593 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01003594 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) DivRemMinusOneSlowPathX86(
David Srbecky9cd6d372016-02-09 15:24:47 +00003595 instruction, out.AsRegister<Register>(), is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003596 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003597
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003598 Register second_reg = second.AsRegister<Register>();
3599 // 0x80000000/-1 triggers an arithmetic exception!
3600 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3601 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003602
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003603 __ cmpl(second_reg, Immediate(-1));
3604 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003605
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003606 // edx:eax <- sign-extended of eax
3607 __ cdq();
3608 // eax = quotient, edx = remainder
3609 __ idivl(second_reg);
3610 __ Bind(slow_path->GetExitLabel());
3611 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003612 break;
3613 }
3614
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003615 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003616 InvokeRuntimeCallingConvention calling_convention;
3617 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3618 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3619 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3620 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3621 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3622 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3623
3624 if (is_div) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003625 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003626 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003627 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003628 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003629 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003630 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003631 break;
3632 }
3633
3634 default:
3635 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3636 }
3637}
3638
Calin Juravle7c4954d2014-10-28 16:57:40 +00003639void LocationsBuilderX86::VisitDiv(HDiv* div) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003640 LocationSummary::CallKind call_kind = (div->GetResultType() == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003641 ? LocationSummary::kCallOnMainOnly
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003642 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01003643 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(div, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003644
Calin Juravle7c4954d2014-10-28 16:57:40 +00003645 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003646 case DataType::Type::kInt32: {
Calin Juravled0d48522014-11-04 16:40:20 +00003647 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003648 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003649 locations->SetOut(Location::SameAsFirstInput());
3650 // Intel uses edx:eax as the dividend.
3651 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003652 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3653 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3654 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003655 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003656 locations->AddTemp(Location::RequiresRegister());
3657 }
Calin Juravled0d48522014-11-04 16:40:20 +00003658 break;
3659 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003660 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003661 InvokeRuntimeCallingConvention calling_convention;
3662 locations->SetInAt(0, Location::RegisterPairLocation(
3663 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3664 locations->SetInAt(1, Location::RegisterPairLocation(
3665 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3666 // Runtime helper puts the result in EAX, EDX.
3667 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003668 break;
3669 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003670 case DataType::Type::kFloat32:
3671 case DataType::Type::kFloat64: {
Calin Juravle7c4954d2014-10-28 16:57:40 +00003672 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003673 if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3674 DCHECK(div->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003675 } else if (div->InputAt(1)->IsConstant()) {
3676 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003677 } else {
3678 locations->SetInAt(1, Location::Any());
3679 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003680 locations->SetOut(Location::SameAsFirstInput());
3681 break;
3682 }
3683
3684 default:
3685 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3686 }
3687}
3688
3689void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3690 LocationSummary* locations = div->GetLocations();
3691 Location first = locations->InAt(0);
3692 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003693
3694 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003695 case DataType::Type::kInt32:
3696 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003697 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003698 break;
3699 }
3700
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003701 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003702 if (second.IsFpuRegister()) {
3703 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3704 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3705 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003706 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003707 __ divss(first.AsFpuRegister<XmmRegister>(),
3708 codegen_->LiteralFloatAddress(
3709 const_area->GetConstant()->AsFloatConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003710 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003711 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3712 } else {
3713 DCHECK(second.IsStackSlot());
3714 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3715 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003716 break;
3717 }
3718
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003719 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003720 if (second.IsFpuRegister()) {
3721 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3722 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3723 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003724 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003725 __ divsd(first.AsFpuRegister<XmmRegister>(),
3726 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003727 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3728 const_area->GetBaseMethodAddress(),
3729 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003730 } else {
3731 DCHECK(second.IsDoubleStackSlot());
3732 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3733 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003734 break;
3735 }
3736
3737 default:
3738 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3739 }
3740}
3741
Calin Juravlebacfec32014-11-14 15:54:36 +00003742void LocationsBuilderX86::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003743 DataType::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003744
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003745 LocationSummary::CallKind call_kind = (rem->GetResultType() == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003746 ? LocationSummary::kCallOnMainOnly
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003747 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01003748 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003749
Calin Juravled2ec87d2014-12-08 14:24:46 +00003750 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003751 case DataType::Type::kInt32: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003752 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003753 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003754 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003755 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3756 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3757 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003758 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003759 locations->AddTemp(Location::RequiresRegister());
3760 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003761 break;
3762 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003763 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003764 InvokeRuntimeCallingConvention calling_convention;
3765 locations->SetInAt(0, Location::RegisterPairLocation(
3766 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3767 locations->SetInAt(1, Location::RegisterPairLocation(
3768 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3769 // Runtime helper puts the result in EAX, EDX.
3770 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3771 break;
3772 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003773 case DataType::Type::kFloat64:
3774 case DataType::Type::kFloat32: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003775 locations->SetInAt(0, Location::Any());
3776 locations->SetInAt(1, Location::Any());
3777 locations->SetOut(Location::RequiresFpuRegister());
3778 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003779 break;
3780 }
3781
3782 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003783 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003784 }
3785}
3786
3787void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003788 DataType::Type type = rem->GetResultType();
Calin Juravlebacfec32014-11-14 15:54:36 +00003789 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003790 case DataType::Type::kInt32:
3791 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003792 GenerateDivRemIntegral(rem);
3793 break;
3794 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003795 case DataType::Type::kFloat32:
3796 case DataType::Type::kFloat64: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003797 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003798 break;
3799 }
3800 default:
3801 LOG(FATAL) << "Unexpected rem type " << type;
3802 }
3803}
3804
Aart Bik3dad3412018-02-28 12:01:46 -08003805void LocationsBuilderX86::VisitAbs(HAbs* abs) {
3806 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(abs);
3807 switch (abs->GetResultType()) {
3808 case DataType::Type::kInt32:
3809 locations->SetInAt(0, Location::RegisterLocation(EAX));
3810 locations->SetOut(Location::SameAsFirstInput());
3811 locations->AddTemp(Location::RegisterLocation(EDX));
3812 break;
3813 case DataType::Type::kInt64:
3814 locations->SetInAt(0, Location::RequiresRegister());
3815 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3816 locations->AddTemp(Location::RequiresRegister());
3817 break;
3818 case DataType::Type::kFloat32:
3819 locations->SetInAt(0, Location::RequiresFpuRegister());
3820 locations->SetOut(Location::SameAsFirstInput());
3821 locations->AddTemp(Location::RequiresFpuRegister());
3822 locations->AddTemp(Location::RequiresRegister());
3823 break;
3824 case DataType::Type::kFloat64:
3825 locations->SetInAt(0, Location::RequiresFpuRegister());
3826 locations->SetOut(Location::SameAsFirstInput());
3827 locations->AddTemp(Location::RequiresFpuRegister());
3828 break;
3829 default:
3830 LOG(FATAL) << "Unexpected type for HAbs " << abs->GetResultType();
3831 }
3832}
3833
3834void InstructionCodeGeneratorX86::VisitAbs(HAbs* abs) {
3835 LocationSummary* locations = abs->GetLocations();
3836 switch (abs->GetResultType()) {
3837 case DataType::Type::kInt32: {
3838 Register out = locations->Out().AsRegister<Register>();
3839 DCHECK_EQ(out, EAX);
3840 Register temp = locations->GetTemp(0).AsRegister<Register>();
3841 DCHECK_EQ(temp, EDX);
3842 // Sign extend EAX into EDX.
3843 __ cdq();
3844 // XOR EAX with sign.
3845 __ xorl(EAX, EDX);
3846 // Subtract out sign to correct.
3847 __ subl(EAX, EDX);
3848 // The result is in EAX.
3849 break;
3850 }
3851 case DataType::Type::kInt64: {
3852 Location input = locations->InAt(0);
3853 Register input_lo = input.AsRegisterPairLow<Register>();
3854 Register input_hi = input.AsRegisterPairHigh<Register>();
3855 Location output = locations->Out();
3856 Register output_lo = output.AsRegisterPairLow<Register>();
3857 Register output_hi = output.AsRegisterPairHigh<Register>();
3858 Register temp = locations->GetTemp(0).AsRegister<Register>();
3859 // Compute the sign into the temporary.
3860 __ movl(temp, input_hi);
3861 __ sarl(temp, Immediate(31));
3862 // Store the sign into the output.
3863 __ movl(output_lo, temp);
3864 __ movl(output_hi, temp);
3865 // XOR the input to the output.
3866 __ xorl(output_lo, input_lo);
3867 __ xorl(output_hi, input_hi);
3868 // Subtract the sign.
3869 __ subl(output_lo, temp);
3870 __ sbbl(output_hi, temp);
3871 break;
3872 }
3873 case DataType::Type::kFloat32: {
3874 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
3875 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3876 Register constant = locations->GetTemp(1).AsRegister<Register>();
3877 __ movl(constant, Immediate(INT32_C(0x7FFFFFFF)));
3878 __ movd(temp, constant);
3879 __ andps(out, temp);
3880 break;
3881 }
3882 case DataType::Type::kFloat64: {
3883 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
3884 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3885 // TODO: Use a constant from the constant table (requires extra input).
3886 __ LoadLongConstant(temp, INT64_C(0x7FFFFFFFFFFFFFFF));
3887 __ andpd(out, temp);
3888 break;
3889 }
3890 default:
3891 LOG(FATAL) << "Unexpected type for HAbs " << abs->GetResultType();
3892 }
3893}
3894
Calin Juravled0d48522014-11-04 16:40:20 +00003895void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003896 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003897 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003898 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003899 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003900 case DataType::Type::kInt8:
3901 case DataType::Type::kUint16:
3902 case DataType::Type::kInt16:
3903 case DataType::Type::kInt32: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003904 locations->SetInAt(0, Location::Any());
3905 break;
3906 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003907 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003908 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
3909 if (!instruction->IsConstant()) {
3910 locations->AddTemp(Location::RequiresRegister());
3911 }
3912 break;
3913 }
3914 default:
3915 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3916 }
Calin Juravled0d48522014-11-04 16:40:20 +00003917}
3918
3919void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01003920 SlowPathCode* slow_path =
3921 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003922 codegen_->AddSlowPath(slow_path);
3923
3924 LocationSummary* locations = instruction->GetLocations();
3925 Location value = locations->InAt(0);
3926
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003927 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003928 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003929 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003930 case DataType::Type::kInt8:
3931 case DataType::Type::kUint16:
3932 case DataType::Type::kInt16:
3933 case DataType::Type::kInt32: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003934 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003935 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003936 __ j(kEqual, slow_path->GetEntryLabel());
3937 } else if (value.IsStackSlot()) {
3938 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
3939 __ j(kEqual, slow_path->GetEntryLabel());
3940 } else {
3941 DCHECK(value.IsConstant()) << value;
3942 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01003943 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003944 }
3945 }
3946 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003947 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003948 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003949 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003950 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003951 __ movl(temp, value.AsRegisterPairLow<Register>());
3952 __ orl(temp, value.AsRegisterPairHigh<Register>());
3953 __ j(kEqual, slow_path->GetEntryLabel());
3954 } else {
3955 DCHECK(value.IsConstant()) << value;
3956 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3957 __ jmp(slow_path->GetEntryLabel());
3958 }
3959 }
3960 break;
3961 }
3962 default:
3963 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003964 }
Calin Juravled0d48522014-11-04 16:40:20 +00003965}
3966
Calin Juravle9aec02f2014-11-18 23:06:35 +00003967void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3968 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3969
3970 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003971 new (GetGraph()->GetAllocator()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003972
3973 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003974 case DataType::Type::kInt32:
3975 case DataType::Type::kInt64: {
Mark P Mendell73945692015-04-29 14:56:17 +00003976 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003977 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003978 // The shift count needs to be in CL or a constant.
3979 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003980 locations->SetOut(Location::SameAsFirstInput());
3981 break;
3982 }
3983 default:
3984 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3985 }
3986}
3987
3988void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3989 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3990
3991 LocationSummary* locations = op->GetLocations();
3992 Location first = locations->InAt(0);
3993 Location second = locations->InAt(1);
3994 DCHECK(first.Equals(locations->Out()));
3995
3996 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003997 case DataType::Type::kInt32: {
Mark P Mendell73945692015-04-29 14:56:17 +00003998 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003999 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004000 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004001 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004002 DCHECK_EQ(ECX, second_reg);
4003 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004004 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004005 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004006 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004007 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004008 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004009 }
4010 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004011 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00004012 if (shift == 0) {
4013 return;
4014 }
4015 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004016 if (op->IsShl()) {
4017 __ shll(first_reg, imm);
4018 } else if (op->IsShr()) {
4019 __ sarl(first_reg, imm);
4020 } else {
4021 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004022 }
4023 }
4024 break;
4025 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004026 case DataType::Type::kInt64: {
Mark P Mendell73945692015-04-29 14:56:17 +00004027 if (second.IsRegister()) {
4028 Register second_reg = second.AsRegister<Register>();
4029 DCHECK_EQ(ECX, second_reg);
4030 if (op->IsShl()) {
4031 GenerateShlLong(first, second_reg);
4032 } else if (op->IsShr()) {
4033 GenerateShrLong(first, second_reg);
4034 } else {
4035 GenerateUShrLong(first, second_reg);
4036 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004037 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00004038 // Shift by a constant.
Roland Levillain5b5b9312016-03-22 14:57:31 +00004039 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00004040 // Nothing to do if the shift is 0, as the input is already the output.
4041 if (shift != 0) {
4042 if (op->IsShl()) {
4043 GenerateShlLong(first, shift);
4044 } else if (op->IsShr()) {
4045 GenerateShrLong(first, shift);
4046 } else {
4047 GenerateUShrLong(first, shift);
4048 }
4049 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004050 }
4051 break;
4052 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004053 default:
4054 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
4055 }
4056}
4057
Mark P Mendell73945692015-04-29 14:56:17 +00004058void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
4059 Register low = loc.AsRegisterPairLow<Register>();
4060 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04004061 if (shift == 1) {
4062 // This is just an addition.
4063 __ addl(low, low);
4064 __ adcl(high, high);
4065 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00004066 // Shift by 32 is easy. High gets low, and low gets 0.
4067 codegen_->EmitParallelMoves(
4068 loc.ToLow(),
4069 loc.ToHigh(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004070 DataType::Type::kInt32,
Mark P Mendell73945692015-04-29 14:56:17 +00004071 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
4072 loc.ToLow(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004073 DataType::Type::kInt32);
Mark P Mendell73945692015-04-29 14:56:17 +00004074 } else if (shift > 32) {
4075 // Low part becomes 0. High part is low part << (shift-32).
4076 __ movl(high, low);
4077 __ shll(high, Immediate(shift - 32));
4078 __ xorl(low, low);
4079 } else {
4080 // Between 1 and 31.
4081 __ shld(high, low, Immediate(shift));
4082 __ shll(low, Immediate(shift));
4083 }
4084}
4085
Calin Juravle9aec02f2014-11-18 23:06:35 +00004086void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004087 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004088 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
4089 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
4090 __ testl(shifter, Immediate(32));
4091 __ j(kEqual, &done);
4092 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
4093 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
4094 __ Bind(&done);
4095}
4096
Mark P Mendell73945692015-04-29 14:56:17 +00004097void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
4098 Register low = loc.AsRegisterPairLow<Register>();
4099 Register high = loc.AsRegisterPairHigh<Register>();
4100 if (shift == 32) {
4101 // Need to copy the sign.
4102 DCHECK_NE(low, high);
4103 __ movl(low, high);
4104 __ sarl(high, Immediate(31));
4105 } else if (shift > 32) {
4106 DCHECK_NE(low, high);
4107 // High part becomes sign. Low part is shifted by shift - 32.
4108 __ movl(low, high);
4109 __ sarl(high, Immediate(31));
4110 __ sarl(low, Immediate(shift - 32));
4111 } else {
4112 // Between 1 and 31.
4113 __ shrd(low, high, Immediate(shift));
4114 __ sarl(high, Immediate(shift));
4115 }
4116}
4117
Calin Juravle9aec02f2014-11-18 23:06:35 +00004118void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004119 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004120 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4121 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
4122 __ testl(shifter, Immediate(32));
4123 __ j(kEqual, &done);
4124 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4125 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
4126 __ Bind(&done);
4127}
4128
Mark P Mendell73945692015-04-29 14:56:17 +00004129void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
4130 Register low = loc.AsRegisterPairLow<Register>();
4131 Register high = loc.AsRegisterPairHigh<Register>();
4132 if (shift == 32) {
4133 // Shift by 32 is easy. Low gets high, and high gets 0.
4134 codegen_->EmitParallelMoves(
4135 loc.ToHigh(),
4136 loc.ToLow(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004137 DataType::Type::kInt32,
Mark P Mendell73945692015-04-29 14:56:17 +00004138 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
4139 loc.ToHigh(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004140 DataType::Type::kInt32);
Mark P Mendell73945692015-04-29 14:56:17 +00004141 } else if (shift > 32) {
4142 // Low part is high >> (shift - 32). High part becomes 0.
4143 __ movl(low, high);
4144 __ shrl(low, Immediate(shift - 32));
4145 __ xorl(high, high);
4146 } else {
4147 // Between 1 and 31.
4148 __ shrd(low, high, Immediate(shift));
4149 __ shrl(high, Immediate(shift));
4150 }
4151}
4152
Calin Juravle9aec02f2014-11-18 23:06:35 +00004153void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004154 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004155 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4156 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
4157 __ testl(shifter, Immediate(32));
4158 __ j(kEqual, &done);
4159 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4160 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
4161 __ Bind(&done);
4162}
4163
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004164void LocationsBuilderX86::VisitRor(HRor* ror) {
4165 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004166 new (GetGraph()->GetAllocator()) LocationSummary(ror, LocationSummary::kNoCall);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004167
4168 switch (ror->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004169 case DataType::Type::kInt64:
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004170 // Add the temporary needed.
4171 locations->AddTemp(Location::RequiresRegister());
4172 FALLTHROUGH_INTENDED;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004173 case DataType::Type::kInt32:
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004174 locations->SetInAt(0, Location::RequiresRegister());
4175 // The shift count needs to be in CL (unless it is a constant).
4176 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
4177 locations->SetOut(Location::SameAsFirstInput());
4178 break;
4179 default:
4180 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4181 UNREACHABLE();
4182 }
4183}
4184
4185void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
4186 LocationSummary* locations = ror->GetLocations();
4187 Location first = locations->InAt(0);
4188 Location second = locations->InAt(1);
4189
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004190 if (ror->GetResultType() == DataType::Type::kInt32) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004191 Register first_reg = first.AsRegister<Register>();
4192 if (second.IsRegister()) {
4193 Register second_reg = second.AsRegister<Register>();
4194 __ rorl(first_reg, second_reg);
4195 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004196 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004197 __ rorl(first_reg, imm);
4198 }
4199 return;
4200 }
4201
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004202 DCHECK_EQ(ror->GetResultType(), DataType::Type::kInt64);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004203 Register first_reg_lo = first.AsRegisterPairLow<Register>();
4204 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
4205 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
4206 if (second.IsRegister()) {
4207 Register second_reg = second.AsRegister<Register>();
4208 DCHECK_EQ(second_reg, ECX);
4209 __ movl(temp_reg, first_reg_hi);
4210 __ shrd(first_reg_hi, first_reg_lo, second_reg);
4211 __ shrd(first_reg_lo, temp_reg, second_reg);
4212 __ movl(temp_reg, first_reg_hi);
4213 __ testl(second_reg, Immediate(32));
4214 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
4215 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
4216 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004217 int32_t shift_amt = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004218 if (shift_amt == 0) {
4219 // Already fine.
4220 return;
4221 }
4222 if (shift_amt == 32) {
4223 // Just swap.
4224 __ movl(temp_reg, first_reg_lo);
4225 __ movl(first_reg_lo, first_reg_hi);
4226 __ movl(first_reg_hi, temp_reg);
4227 return;
4228 }
4229
4230 Immediate imm(shift_amt);
4231 // Save the constents of the low value.
4232 __ movl(temp_reg, first_reg_lo);
4233
4234 // Shift right into low, feeding bits from high.
4235 __ shrd(first_reg_lo, first_reg_hi, imm);
4236
4237 // Shift right into high, feeding bits from the original low.
4238 __ shrd(first_reg_hi, temp_reg, imm);
4239
4240 // Swap if needed.
4241 if (shift_amt > 32) {
4242 __ movl(temp_reg, first_reg_lo);
4243 __ movl(first_reg_lo, first_reg_hi);
4244 __ movl(first_reg_hi, temp_reg);
4245 }
4246 }
4247}
4248
Calin Juravle9aec02f2014-11-18 23:06:35 +00004249void LocationsBuilderX86::VisitShl(HShl* shl) {
4250 HandleShift(shl);
4251}
4252
4253void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
4254 HandleShift(shl);
4255}
4256
4257void LocationsBuilderX86::VisitShr(HShr* shr) {
4258 HandleShift(shr);
4259}
4260
4261void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
4262 HandleShift(shr);
4263}
4264
4265void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
4266 HandleShift(ushr);
4267}
4268
4269void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
4270 HandleShift(ushr);
4271}
4272
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004273void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004274 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
4275 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004276 locations->SetOut(Location::RegisterLocation(EAX));
David Brazdil6de19382016-01-08 17:37:10 +00004277 if (instruction->IsStringAlloc()) {
4278 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4279 } else {
4280 InvokeRuntimeCallingConvention calling_convention;
4281 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00004282 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004283}
4284
4285void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004286 // Note: if heap poisoning is enabled, the entry point takes cares
4287 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004288 if (instruction->IsStringAlloc()) {
4289 // String is allocated through StringFactory. Call NewEmptyString entry point.
4290 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07004291 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004292 __ fs()->movl(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString)));
4293 __ call(Address(temp, code_offset.Int32Value()));
4294 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4295 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01004296 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00004297 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00004298 DCHECK(!codegen_->IsLeafMethod());
4299 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004300}
4301
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004302void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004303 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
4304 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004305 locations->SetOut(Location::RegisterLocation(EAX));
4306 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004307 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4308 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004309}
4310
4311void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004312 // Note: if heap poisoning is enabled, the entry point takes cares
4313 // of poisoning the reference.
Nicolas Geoffrayd0958442017-01-30 14:57:16 +00004314 QuickEntrypointEnum entrypoint =
4315 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
4316 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004317 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004318 DCHECK(!codegen_->IsLeafMethod());
4319}
4320
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004321void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004322 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004323 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004324 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4325 if (location.IsStackSlot()) {
4326 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4327 } else if (location.IsDoubleStackSlot()) {
4328 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004329 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004330 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004331}
4332
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004333void InstructionCodeGeneratorX86::VisitParameterValue(
4334 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4335}
4336
4337void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
4338 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004339 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004340 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4341}
4342
4343void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004344}
4345
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004346void LocationsBuilderX86::VisitClassTableGet(HClassTableGet* instruction) {
4347 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004348 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004349 locations->SetInAt(0, Location::RequiresRegister());
4350 locations->SetOut(Location::RequiresRegister());
4351}
4352
4353void InstructionCodeGeneratorX86::VisitClassTableGet(HClassTableGet* instruction) {
4354 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004355 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004356 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004357 instruction->GetIndex(), kX86PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004358 __ movl(locations->Out().AsRegister<Register>(),
4359 Address(locations->InAt(0).AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004360 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004361 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004362 instruction->GetIndex(), kX86PointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004363 __ movl(locations->Out().AsRegister<Register>(),
4364 Address(locations->InAt(0).AsRegister<Register>(),
4365 mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
4366 // temp = temp->GetImtEntryAt(method_offset);
4367 __ movl(locations->Out().AsRegister<Register>(),
4368 Address(locations->Out().AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004369 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004370}
4371
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004372void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004373 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004374 new (GetGraph()->GetAllocator()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004375 locations->SetInAt(0, Location::RequiresRegister());
4376 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004377}
4378
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004379void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
4380 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01004381 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004382 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01004383 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004384 switch (not_->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004385 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004386 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004387 break;
4388
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004389 case DataType::Type::kInt64:
Roland Levillain70566432014-10-24 16:20:17 +01004390 __ notl(out.AsRegisterPairLow<Register>());
4391 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004392 break;
4393
4394 default:
4395 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4396 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004397}
4398
David Brazdil66d126e2015-04-03 16:02:44 +01004399void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
4400 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004401 new (GetGraph()->GetAllocator()) LocationSummary(bool_not, LocationSummary::kNoCall);
David Brazdil66d126e2015-04-03 16:02:44 +01004402 locations->SetInAt(0, Location::RequiresRegister());
4403 locations->SetOut(Location::SameAsFirstInput());
4404}
4405
4406void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004407 LocationSummary* locations = bool_not->GetLocations();
4408 Location in = locations->InAt(0);
4409 Location out = locations->Out();
4410 DCHECK(in.Equals(out));
4411 __ xorl(out.AsRegister<Register>(), Immediate(1));
4412}
4413
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004414void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004415 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004416 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004417 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004418 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004419 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004420 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004421 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004422 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004423 case DataType::Type::kInt32:
4424 case DataType::Type::kInt64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00004425 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00004426 locations->SetInAt(1, Location::Any());
4427 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4428 break;
4429 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004430 case DataType::Type::kFloat32:
4431 case DataType::Type::kFloat64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00004432 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004433 if (compare->InputAt(1)->IsX86LoadFromConstantTable()) {
4434 DCHECK(compare->InputAt(1)->IsEmittedAtUseSite());
4435 } else if (compare->InputAt(1)->IsConstant()) {
4436 locations->SetInAt(1, Location::RequiresFpuRegister());
4437 } else {
4438 locations->SetInAt(1, Location::Any());
4439 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004440 locations->SetOut(Location::RequiresRegister());
4441 break;
4442 }
4443 default:
4444 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4445 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004446}
4447
4448void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004449 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004450 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004451 Location left = locations->InAt(0);
4452 Location right = locations->InAt(1);
4453
Mark Mendell0c9497d2015-08-21 09:30:05 -04004454 NearLabel less, greater, done;
Aart Bika19616e2016-02-01 18:57:58 -08004455 Condition less_cond = kLess;
4456
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004457 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004458 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004459 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004460 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004461 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004462 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004463 case DataType::Type::kInt32: {
Roland Levillain0b671c02016-08-19 12:02:34 +01004464 codegen_->GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08004465 break;
4466 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004467 case DataType::Type::kInt64: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004468 Register left_low = left.AsRegisterPairLow<Register>();
4469 Register left_high = left.AsRegisterPairHigh<Register>();
4470 int32_t val_low = 0;
4471 int32_t val_high = 0;
4472 bool right_is_const = false;
4473
4474 if (right.IsConstant()) {
4475 DCHECK(right.GetConstant()->IsLongConstant());
4476 right_is_const = true;
4477 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
4478 val_low = Low32Bits(val);
4479 val_high = High32Bits(val);
4480 }
4481
Calin Juravleddb7df22014-11-25 20:56:51 +00004482 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004483 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004484 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004485 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004486 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004487 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004488 codegen_->Compare32BitValue(left_high, val_high);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004489 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004490 __ j(kLess, &less); // Signed compare.
4491 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004492 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004493 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004494 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004495 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004496 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004497 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004498 codegen_->Compare32BitValue(left_low, val_low);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004499 }
Aart Bika19616e2016-02-01 18:57:58 -08004500 less_cond = kBelow; // for CF (unsigned).
Calin Juravleddb7df22014-11-25 20:56:51 +00004501 break;
4502 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004503 case DataType::Type::kFloat32: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004504 GenerateFPCompare(left, right, compare, false);
Calin Juravleddb7df22014-11-25 20:56:51 +00004505 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004506 less_cond = kBelow; // for CF (floats).
Calin Juravleddb7df22014-11-25 20:56:51 +00004507 break;
4508 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004509 case DataType::Type::kFloat64: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004510 GenerateFPCompare(left, right, compare, true);
Calin Juravleddb7df22014-11-25 20:56:51 +00004511 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004512 less_cond = kBelow; // for CF (floats).
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004513 break;
4514 }
4515 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004516 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004517 }
Aart Bika19616e2016-02-01 18:57:58 -08004518
Calin Juravleddb7df22014-11-25 20:56:51 +00004519 __ movl(out, Immediate(0));
4520 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08004521 __ j(less_cond, &less);
Calin Juravleddb7df22014-11-25 20:56:51 +00004522
4523 __ Bind(&greater);
4524 __ movl(out, Immediate(1));
4525 __ jmp(&done);
4526
4527 __ Bind(&less);
4528 __ movl(out, Immediate(-1));
4529
4530 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004531}
4532
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004533void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004534 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004535 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004536 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004537 locations->SetInAt(i, Location::Any());
4538 }
4539 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004540}
4541
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004542void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004543 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004544}
4545
Roland Levillain7c1559a2015-12-15 10:55:36 +00004546void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004547 /*
4548 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
4549 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
4550 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4551 */
4552 switch (kind) {
4553 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004554 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004555 break;
4556 }
4557 case MemBarrierKind::kAnyStore:
4558 case MemBarrierKind::kLoadAny:
4559 case MemBarrierKind::kStoreStore: {
4560 // nop
4561 break;
4562 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004563 case MemBarrierKind::kNTStoreStore:
4564 // Non-Temporal Store/Store needs an explicit fence.
4565 MemoryFence(/* non-temporal */ true);
4566 break;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004567 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004568}
4569
Vladimir Markodc151b22015-10-15 18:02:30 +01004570HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
4571 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004572 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004573 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004574}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004575
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004576Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4577 Register temp) {
4578 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Markoc53c0792015-11-19 15:48:33 +00004579 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004580 if (!invoke->GetLocations()->Intrinsified()) {
4581 return location.AsRegister<Register>();
4582 }
4583 // For intrinsics we allow any location, so it may be on the stack.
4584 if (!location.IsRegister()) {
4585 __ movl(temp, Address(ESP, location.GetStackIndex()));
4586 return temp;
4587 }
4588 // For register locations, check if the register was saved. If so, get it from the stack.
4589 // Note: There is a chance that the register was saved but not overwritten, so we could
4590 // save one load. However, since this is just an intrinsic slow path we prefer this
4591 // simple and more robust approach rather that trying to determine if that's the case.
4592 SlowPathCode* slow_path = GetCurrentSlowPath();
Vladimir Marko4ee8e292017-06-02 15:39:30 +00004593 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
4594 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4595 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4596 __ movl(temp, Address(ESP, stack_offset));
4597 return temp;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004598 }
4599 return location.AsRegister<Register>();
4600}
4601
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004602void CodeGeneratorX86::GenerateStaticOrDirectCall(
4603 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Vladimir Marko58155012015-08-19 12:49:41 +00004604 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4605 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004606 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00004607 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004608 uint32_t offset =
4609 GetThreadOffset<kX86PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
4610 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004611 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004612 }
Vladimir Marko58155012015-08-19 12:49:41 +00004613 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004614 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004615 break;
Vladimir Marko65979462017-05-19 17:25:12 +01004616 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
4617 DCHECK(GetCompilerOptions().IsBootImage());
4618 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4619 temp.AsRegister<Register>());
4620 __ leal(temp.AsRegister<Register>(), Address(base_reg, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004621 RecordBootImageMethodPatch(invoke);
Vladimir Marko65979462017-05-19 17:25:12 +01004622 break;
4623 }
Vladimir Marko58155012015-08-19 12:49:41 +00004624 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4625 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
4626 break;
Vladimir Markob066d432018-01-03 13:14:37 +00004627 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageRelRo: {
4628 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4629 temp.AsRegister<Register>());
4630 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
4631 RecordBootImageRelRoPatch(
4632 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress(),
4633 invoke->GetDispatchInfo().method_load_data);
4634 break;
4635 }
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004636 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004637 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4638 temp.AsRegister<Register>());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004639 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004640 RecordMethodBssEntryPatch(invoke);
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004641 break;
4642 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004643 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
4644 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
4645 return; // No code pointer retrieval; the runtime performs the call directly.
Vladimir Marko9b688a02015-05-06 14:12:42 +01004646 }
Vladimir Marko58155012015-08-19 12:49:41 +00004647 }
4648
4649 switch (invoke->GetCodePtrLocation()) {
4650 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4651 __ call(GetFrameEntryLabel());
4652 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004653 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4654 // (callee_method + offset_of_quick_compiled_code)()
4655 __ call(Address(callee_method.AsRegister<Register>(),
4656 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07004657 kX86PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004658 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04004659 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004660 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Mark Mendell09ed1a32015-03-25 08:30:06 -04004661
4662 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04004663}
4664
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004665void CodeGeneratorX86::GenerateVirtualCall(
4666 HInvokeVirtual* invoke, Location temp_in, SlowPathCode* slow_path) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004667 Register temp = temp_in.AsRegister<Register>();
4668 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4669 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004670
4671 // Use the calling convention instead of the location of the receiver, as
4672 // intrinsics may have put the receiver in a different register. In the intrinsics
4673 // slow path, the arguments have been moved to the right place, so here we are
4674 // guaranteed that the receiver is the first register of the calling convention.
4675 InvokeDexCallingConvention calling_convention;
4676 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004677 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004678 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004679 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004680 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004681 // Instead of simply (possibly) unpoisoning `temp` here, we should
4682 // emit a read barrier for the previous class reference load.
4683 // However this is not required in practice, as this is an
4684 // intermediate/temporary reference and because the current
4685 // concurrent copying collector keeps the from-space memory
4686 // intact/accessible until the end of the marking phase (the
4687 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004688 __ MaybeUnpoisonHeapReference(temp);
4689 // temp = temp->GetMethodAt(method_offset);
4690 __ movl(temp, Address(temp, method_offset));
4691 // call temp->GetEntryPoint();
4692 __ call(Address(
Andreas Gampe542451c2016-07-26 09:02:02 -07004693 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004694 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004695}
4696
Vladimir Markob066d432018-01-03 13:14:37 +00004697void CodeGeneratorX86::RecordBootImageRelRoPatch(HX86ComputeBaseMethodAddress* method_address,
4698 uint32_t boot_image_offset) {
4699 boot_image_method_patches_.emplace_back(
4700 method_address, /* target_dex_file */ nullptr, boot_image_offset);
4701 __ Bind(&boot_image_method_patches_.back().label);
4702}
4703
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004704void CodeGeneratorX86::RecordBootImageMethodPatch(HInvokeStaticOrDirect* invoke) {
Vladimir Marko65979462017-05-19 17:25:12 +01004705 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004706 HX86ComputeBaseMethodAddress* method_address =
Vladimir Marko65979462017-05-19 17:25:12 +01004707 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004708 boot_image_method_patches_.emplace_back(
4709 method_address, invoke->GetTargetMethod().dex_file, invoke->GetTargetMethod().index);
Vladimir Marko65979462017-05-19 17:25:12 +01004710 __ Bind(&boot_image_method_patches_.back().label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004711}
4712
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004713void CodeGeneratorX86::RecordMethodBssEntryPatch(HInvokeStaticOrDirect* invoke) {
4714 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
4715 HX86ComputeBaseMethodAddress* method_address =
4716 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004717 // Add the patch entry and bind its label at the end of the instruction.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004718 method_bss_entry_patches_.emplace_back(
4719 method_address, &GetGraph()->GetDexFile(), invoke->GetDexMethodIndex());
4720 __ Bind(&method_bss_entry_patches_.back().label);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004721}
4722
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004723void CodeGeneratorX86::RecordBootImageTypePatch(HLoadClass* load_class) {
4724 HX86ComputeBaseMethodAddress* method_address =
4725 load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
4726 boot_image_type_patches_.emplace_back(
4727 method_address, &load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00004728 __ Bind(&boot_image_type_patches_.back().label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004729}
4730
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004731Label* CodeGeneratorX86::NewTypeBssEntryPatch(HLoadClass* load_class) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004732 HX86ComputeBaseMethodAddress* method_address =
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004733 load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
4734 type_bss_entry_patches_.emplace_back(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004735 method_address, &load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00004736 return &type_bss_entry_patches_.back().label;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004737}
4738
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004739void CodeGeneratorX86::RecordBootImageStringPatch(HLoadString* load_string) {
4740 HX86ComputeBaseMethodAddress* method_address =
4741 load_string->InputAt(0)->AsX86ComputeBaseMethodAddress();
4742 boot_image_string_patches_.emplace_back(
4743 method_address, &load_string->GetDexFile(), load_string->GetStringIndex().index_);
4744 __ Bind(&boot_image_string_patches_.back().label);
Vladimir Marko65979462017-05-19 17:25:12 +01004745}
4746
Vladimir Markoaad75c62016-10-03 08:46:48 +00004747Label* CodeGeneratorX86::NewStringBssEntryPatch(HLoadString* load_string) {
4748 DCHECK(!GetCompilerOptions().IsBootImage());
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004749 HX86ComputeBaseMethodAddress* method_address =
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004750 load_string->InputAt(0)->AsX86ComputeBaseMethodAddress();
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004751 string_bss_entry_patches_.emplace_back(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004752 method_address, &load_string->GetDexFile(), load_string->GetStringIndex().index_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004753 return &string_bss_entry_patches_.back().label;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004754}
4755
Vladimir Markoaad75c62016-10-03 08:46:48 +00004756// The label points to the end of the "movl" or another instruction but the literal offset
4757// for method patch needs to point to the embedded constant which occupies the last 4 bytes.
4758constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
4759
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004760template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +00004761inline void CodeGeneratorX86::EmitPcRelativeLinkerPatches(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004762 const ArenaDeque<X86PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004763 ArenaVector<linker::LinkerPatch>* linker_patches) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004764 for (const X86PcRelativePatchInfo& info : infos) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00004765 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004766 linker_patches->push_back(Factory(literal_offset,
4767 info.target_dex_file,
4768 GetMethodAddressOffset(info.method_address),
4769 info.offset_or_index));
Vladimir Markoaad75c62016-10-03 08:46:48 +00004770 }
4771}
4772
Vladimir Markob066d432018-01-03 13:14:37 +00004773linker::LinkerPatch DataBimgRelRoPatchAdapter(size_t literal_offset,
4774 const DexFile* target_dex_file,
4775 uint32_t pc_insn_offset,
4776 uint32_t boot_image_offset) {
4777 DCHECK(target_dex_file == nullptr); // Unused for DataBimgRelRoPatch(), should be null.
4778 return linker::LinkerPatch::DataBimgRelRoPatch(literal_offset, pc_insn_offset, boot_image_offset);
4779}
4780
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004781void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Marko58155012015-08-19 12:49:41 +00004782 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004783 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01004784 boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004785 method_bss_entry_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +00004786 boot_image_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01004787 type_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004788 boot_image_string_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004789 string_bss_entry_patches_.size();
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004790 linker_patches->reserve(size);
Vladimir Marko764d4542017-05-16 10:31:41 +01004791 if (GetCompilerOptions().IsBootImage()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004792 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
4793 boot_image_method_patches_, linker_patches);
4794 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
4795 boot_image_type_patches_, linker_patches);
4796 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004797 boot_image_string_patches_, linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004798 } else {
Vladimir Markob066d432018-01-03 13:14:37 +00004799 EmitPcRelativeLinkerPatches<DataBimgRelRoPatchAdapter>(
4800 boot_image_method_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004801 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeClassTablePatch>(
4802 boot_image_type_patches_, linker_patches);
4803 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringInternTablePatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004804 boot_image_string_patches_, linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004805 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004806 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
4807 method_bss_entry_patches_, linker_patches);
4808 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
4809 type_bss_entry_patches_, linker_patches);
4810 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
4811 string_bss_entry_patches_, linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00004812 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00004813}
4814
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004815void CodeGeneratorX86::MarkGCCard(Register temp,
4816 Register card,
4817 Register object,
4818 Register value,
4819 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004820 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004821 if (value_can_be_null) {
4822 __ testl(value, value);
4823 __ j(kEqual, &is_null);
4824 }
Andreas Gampe542451c2016-07-26 09:02:02 -07004825 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86PointerSize>().Int32Value()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004826 __ movl(temp, object);
4827 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004828 __ movb(Address(temp, card, TIMES_1, 0),
4829 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004830 if (value_can_be_null) {
4831 __ Bind(&is_null);
4832 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004833}
4834
Calin Juravle52c48962014-12-16 17:02:57 +00004835void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4836 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00004837
4838 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004839 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004840 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004841 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
4842 kEmitCompilerReadBarrier
4843 ? LocationSummary::kCallOnSlowPath
4844 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004845 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004846 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004847 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004848 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004849
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004850 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004851 locations->SetOut(Location::RequiresFpuRegister());
4852 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004853 // The output overlaps in case of long: we don't want the low move
4854 // to overwrite the object's location. Likewise, in the case of
4855 // an object field get with read barriers enabled, we do not want
4856 // the move to overwrite the object's location, as we need it to emit
4857 // the read barrier.
4858 locations->SetOut(
4859 Location::RequiresRegister(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004860 (object_field_get_with_read_barrier || instruction->GetType() == DataType::Type::kInt64) ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00004861 Location::kOutputOverlap :
4862 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004863 }
Calin Juravle52c48962014-12-16 17:02:57 +00004864
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004865 if (field_info.IsVolatile() && (field_info.GetFieldType() == DataType::Type::kInt64)) {
Calin Juravle52c48962014-12-16 17:02:57 +00004866 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00004867 // So we use an XMM register as a temp to achieve atomicity (first
4868 // load the temp into the XMM and then copy the XMM into the
4869 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00004870 locations->AddTemp(Location::RequiresFpuRegister());
4871 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004872}
4873
Calin Juravle52c48962014-12-16 17:02:57 +00004874void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
4875 const FieldInfo& field_info) {
4876 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004877
Calin Juravle52c48962014-12-16 17:02:57 +00004878 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004879 Location base_loc = locations->InAt(0);
4880 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004881 Location out = locations->Out();
4882 bool is_volatile = field_info.IsVolatile();
Vladimir Marko61b92282017-10-11 13:23:17 +01004883 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
4884 DataType::Type load_type = instruction->GetType();
Calin Juravle52c48962014-12-16 17:02:57 +00004885 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4886
Vladimir Marko61b92282017-10-11 13:23:17 +01004887 switch (load_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004888 case DataType::Type::kBool:
4889 case DataType::Type::kUint8: {
Calin Juravle52c48962014-12-16 17:02:57 +00004890 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004891 break;
4892 }
4893
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004894 case DataType::Type::kInt8: {
Calin Juravle52c48962014-12-16 17:02:57 +00004895 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004896 break;
4897 }
4898
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004899 case DataType::Type::kUint16: {
4900 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004901 break;
4902 }
4903
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004904 case DataType::Type::kInt16: {
4905 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004906 break;
4907 }
4908
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004909 case DataType::Type::kInt32:
Calin Juravle52c48962014-12-16 17:02:57 +00004910 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004911 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00004912
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004913 case DataType::Type::kReference: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004914 // /* HeapReference<Object> */ out = *(base + offset)
4915 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004916 // Note that a potential implicit null check is handled in this
4917 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4918 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00004919 instruction, out, base, offset, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00004920 if (is_volatile) {
4921 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4922 }
4923 } else {
4924 __ movl(out.AsRegister<Register>(), Address(base, offset));
4925 codegen_->MaybeRecordImplicitNullCheck(instruction);
4926 if (is_volatile) {
4927 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4928 }
4929 // If read barriers are enabled, emit read barriers other than
4930 // Baker's using a slow path (and also unpoison the loaded
4931 // reference, if heap poisoning is enabled).
4932 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4933 }
4934 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004935 }
4936
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004937 case DataType::Type::kInt64: {
Calin Juravle52c48962014-12-16 17:02:57 +00004938 if (is_volatile) {
4939 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4940 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004941 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004942 __ movd(out.AsRegisterPairLow<Register>(), temp);
4943 __ psrlq(temp, Immediate(32));
4944 __ movd(out.AsRegisterPairHigh<Register>(), temp);
4945 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004946 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00004947 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004948 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004949 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
4950 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004951 break;
4952 }
4953
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004954 case DataType::Type::kFloat32: {
Calin Juravle52c48962014-12-16 17:02:57 +00004955 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004956 break;
4957 }
4958
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004959 case DataType::Type::kFloat64: {
Calin Juravle52c48962014-12-16 17:02:57 +00004960 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004961 break;
4962 }
4963
Aart Bik66c158e2018-01-31 12:55:04 -08004964 case DataType::Type::kUint32:
4965 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004966 case DataType::Type::kVoid:
Vladimir Marko61b92282017-10-11 13:23:17 +01004967 LOG(FATAL) << "Unreachable type " << load_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004968 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004969 }
Calin Juravle52c48962014-12-16 17:02:57 +00004970
Vladimir Marko61b92282017-10-11 13:23:17 +01004971 if (load_type == DataType::Type::kReference || load_type == DataType::Type::kInt64) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004972 // Potential implicit null checks, in the case of reference or
4973 // long fields, are handled in the previous switch statement.
4974 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004975 codegen_->MaybeRecordImplicitNullCheck(instruction);
4976 }
4977
Calin Juravle52c48962014-12-16 17:02:57 +00004978 if (is_volatile) {
Vladimir Marko61b92282017-10-11 13:23:17 +01004979 if (load_type == DataType::Type::kReference) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004980 // Memory barriers, in the case of references, are also handled
4981 // in the previous switch statement.
4982 } else {
4983 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4984 }
Roland Levillain4d027112015-07-01 15:41:14 +01004985 }
Calin Juravle52c48962014-12-16 17:02:57 +00004986}
4987
4988void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4989 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4990
4991 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004992 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00004993 locations->SetInAt(0, Location::RequiresRegister());
4994 bool is_volatile = field_info.IsVolatile();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004995 DataType::Type field_type = field_info.GetFieldType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004996 bool is_byte_type = DataType::Size(field_type) == 1u;
Calin Juravle52c48962014-12-16 17:02:57 +00004997
4998 // The register allocator does not support multiple
4999 // inputs that die at entry with one in a specific register.
5000 if (is_byte_type) {
5001 // Ensure the value is in a byte register.
5002 locations->SetInAt(1, Location::RegisterLocation(EAX));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005003 } else if (DataType::IsFloatingPointType(field_type)) {
5004 if (is_volatile && field_type == DataType::Type::kFloat64) {
Mark Mendell81489372015-11-04 11:30:41 -05005005 // In order to satisfy the semantics of volatile, this must be a single instruction store.
5006 locations->SetInAt(1, Location::RequiresFpuRegister());
5007 } else {
5008 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
5009 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005010 } else if (is_volatile && field_type == DataType::Type::kInt64) {
Mark Mendell81489372015-11-04 11:30:41 -05005011 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00005012 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05005013
Calin Juravle52c48962014-12-16 17:02:57 +00005014 // 64bits value can be atomically written to an address with movsd and an XMM register.
5015 // We need two XMM registers because there's no easier way to (bit) copy a register pair
5016 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
5017 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
5018 // isolated cases when we need this it isn't worth adding the extra complexity.
5019 locations->AddTemp(Location::RequiresFpuRegister());
5020 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05005021 } else {
5022 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5023
5024 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
5025 // Temporary registers for the write barrier.
5026 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
5027 // Ensure the card is in a byte register.
5028 locations->AddTemp(Location::RegisterLocation(ECX));
5029 }
Calin Juravle52c48962014-12-16 17:02:57 +00005030 }
5031}
5032
5033void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005034 const FieldInfo& field_info,
5035 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00005036 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5037
5038 LocationSummary* locations = instruction->GetLocations();
5039 Register base = locations->InAt(0).AsRegister<Register>();
5040 Location value = locations->InAt(1);
5041 bool is_volatile = field_info.IsVolatile();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005042 DataType::Type field_type = field_info.GetFieldType();
Calin Juravle52c48962014-12-16 17:02:57 +00005043 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01005044 bool needs_write_barrier =
5045 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00005046
5047 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005048 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00005049 }
5050
Mark Mendell81489372015-11-04 11:30:41 -05005051 bool maybe_record_implicit_null_check_done = false;
5052
Calin Juravle52c48962014-12-16 17:02:57 +00005053 switch (field_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005054 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005055 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005056 case DataType::Type::kInt8: {
Calin Juravle52c48962014-12-16 17:02:57 +00005057 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
5058 break;
5059 }
5060
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005061 case DataType::Type::kUint16:
5062 case DataType::Type::kInt16: {
Mark Mendell81489372015-11-04 11:30:41 -05005063 if (value.IsConstant()) {
Nicolas Geoffray78612082017-07-24 14:18:53 +01005064 __ movw(Address(base, offset),
5065 Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Mark Mendell81489372015-11-04 11:30:41 -05005066 } else {
5067 __ movw(Address(base, offset), value.AsRegister<Register>());
5068 }
Calin Juravle52c48962014-12-16 17:02:57 +00005069 break;
5070 }
5071
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005072 case DataType::Type::kInt32:
5073 case DataType::Type::kReference: {
Roland Levillain4d027112015-07-01 15:41:14 +01005074 if (kPoisonHeapReferences && needs_write_barrier) {
5075 // Note that in the case where `value` is a null reference,
5076 // we do not enter this block, as the reference does not
5077 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005078 DCHECK_EQ(field_type, DataType::Type::kReference);
Roland Levillain4d027112015-07-01 15:41:14 +01005079 Register temp = locations->GetTemp(0).AsRegister<Register>();
5080 __ movl(temp, value.AsRegister<Register>());
5081 __ PoisonHeapReference(temp);
5082 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05005083 } else if (value.IsConstant()) {
5084 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5085 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01005086 } else {
Nicolas Geoffray03971632016-03-17 10:44:24 +00005087 DCHECK(value.IsRegister()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01005088 __ movl(Address(base, offset), value.AsRegister<Register>());
5089 }
Calin Juravle52c48962014-12-16 17:02:57 +00005090 break;
5091 }
5092
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005093 case DataType::Type::kInt64: {
Calin Juravle52c48962014-12-16 17:02:57 +00005094 if (is_volatile) {
5095 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
5096 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
5097 __ movd(temp1, value.AsRegisterPairLow<Register>());
5098 __ movd(temp2, value.AsRegisterPairHigh<Register>());
5099 __ punpckldq(temp1, temp2);
5100 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00005101 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05005102 } else if (value.IsConstant()) {
5103 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
5104 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
5105 codegen_->MaybeRecordImplicitNullCheck(instruction);
5106 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00005107 } else {
5108 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005109 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005110 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
5111 }
Mark Mendell81489372015-11-04 11:30:41 -05005112 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00005113 break;
5114 }
5115
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005116 case DataType::Type::kFloat32: {
Mark Mendell81489372015-11-04 11:30:41 -05005117 if (value.IsConstant()) {
5118 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5119 __ movl(Address(base, offset), Immediate(v));
5120 } else {
5121 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
5122 }
Calin Juravle52c48962014-12-16 17:02:57 +00005123 break;
5124 }
5125
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005126 case DataType::Type::kFloat64: {
Mark Mendell81489372015-11-04 11:30:41 -05005127 if (value.IsConstant()) {
5128 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
5129 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
5130 codegen_->MaybeRecordImplicitNullCheck(instruction);
5131 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
5132 maybe_record_implicit_null_check_done = true;
5133 } else {
5134 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
5135 }
Calin Juravle52c48962014-12-16 17:02:57 +00005136 break;
5137 }
5138
Aart Bik66c158e2018-01-31 12:55:04 -08005139 case DataType::Type::kUint32:
5140 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005141 case DataType::Type::kVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00005142 LOG(FATAL) << "Unreachable type " << field_type;
5143 UNREACHABLE();
5144 }
5145
Mark Mendell81489372015-11-04 11:30:41 -05005146 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005147 codegen_->MaybeRecordImplicitNullCheck(instruction);
5148 }
5149
Roland Levillain4d027112015-07-01 15:41:14 +01005150 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005151 Register temp = locations->GetTemp(0).AsRegister<Register>();
5152 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005153 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00005154 }
5155
Calin Juravle52c48962014-12-16 17:02:57 +00005156 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005157 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00005158 }
5159}
5160
5161void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5162 HandleFieldGet(instruction, instruction->GetFieldInfo());
5163}
5164
5165void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5166 HandleFieldGet(instruction, instruction->GetFieldInfo());
5167}
5168
5169void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5170 HandleFieldSet(instruction, instruction->GetFieldInfo());
5171}
5172
5173void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005174 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005175}
5176
5177void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5178 HandleFieldSet(instruction, instruction->GetFieldInfo());
5179}
5180
5181void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005182 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005183}
5184
5185void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5186 HandleFieldGet(instruction, instruction->GetFieldInfo());
5187}
5188
5189void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5190 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005191}
5192
Calin Juravlee460d1d2015-09-29 04:52:17 +01005193void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
5194 HUnresolvedInstanceFieldGet* instruction) {
5195 FieldAccessCallingConventionX86 calling_convention;
5196 codegen_->CreateUnresolvedFieldLocationSummary(
5197 instruction, instruction->GetFieldType(), calling_convention);
5198}
5199
5200void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
5201 HUnresolvedInstanceFieldGet* instruction) {
5202 FieldAccessCallingConventionX86 calling_convention;
5203 codegen_->GenerateUnresolvedFieldAccess(instruction,
5204 instruction->GetFieldType(),
5205 instruction->GetFieldIndex(),
5206 instruction->GetDexPc(),
5207 calling_convention);
5208}
5209
5210void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
5211 HUnresolvedInstanceFieldSet* instruction) {
5212 FieldAccessCallingConventionX86 calling_convention;
5213 codegen_->CreateUnresolvedFieldLocationSummary(
5214 instruction, instruction->GetFieldType(), calling_convention);
5215}
5216
5217void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
5218 HUnresolvedInstanceFieldSet* instruction) {
5219 FieldAccessCallingConventionX86 calling_convention;
5220 codegen_->GenerateUnresolvedFieldAccess(instruction,
5221 instruction->GetFieldType(),
5222 instruction->GetFieldIndex(),
5223 instruction->GetDexPc(),
5224 calling_convention);
5225}
5226
5227void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
5228 HUnresolvedStaticFieldGet* instruction) {
5229 FieldAccessCallingConventionX86 calling_convention;
5230 codegen_->CreateUnresolvedFieldLocationSummary(
5231 instruction, instruction->GetFieldType(), calling_convention);
5232}
5233
5234void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
5235 HUnresolvedStaticFieldGet* instruction) {
5236 FieldAccessCallingConventionX86 calling_convention;
5237 codegen_->GenerateUnresolvedFieldAccess(instruction,
5238 instruction->GetFieldType(),
5239 instruction->GetFieldIndex(),
5240 instruction->GetDexPc(),
5241 calling_convention);
5242}
5243
5244void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
5245 HUnresolvedStaticFieldSet* instruction) {
5246 FieldAccessCallingConventionX86 calling_convention;
5247 codegen_->CreateUnresolvedFieldLocationSummary(
5248 instruction, instruction->GetFieldType(), calling_convention);
5249}
5250
5251void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
5252 HUnresolvedStaticFieldSet* instruction) {
5253 FieldAccessCallingConventionX86 calling_convention;
5254 codegen_->GenerateUnresolvedFieldAccess(instruction,
5255 instruction->GetFieldType(),
5256 instruction->GetFieldIndex(),
5257 instruction->GetDexPc(),
5258 calling_convention);
5259}
5260
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005261void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005262 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5263 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
5264 ? Location::RequiresRegister()
5265 : Location::Any();
5266 locations->SetInAt(0, loc);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005267}
5268
Calin Juravle2ae48182016-03-16 14:05:09 +00005269void CodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
5270 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005271 return;
5272 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005273 LocationSummary* locations = instruction->GetLocations();
5274 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005275
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005276 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00005277 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005278}
5279
Calin Juravle2ae48182016-03-16 14:05:09 +00005280void CodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005281 SlowPathCode* slow_path = new (GetScopedAllocator()) NullCheckSlowPathX86(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005282 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005283
5284 LocationSummary* locations = instruction->GetLocations();
5285 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005286
5287 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04005288 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005289 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005290 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005291 } else {
5292 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00005293 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005294 __ jmp(slow_path->GetEntryLabel());
5295 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005296 }
5297 __ j(kEqual, slow_path->GetEntryLabel());
5298}
5299
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005300void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005301 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005302}
5303
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005304void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005305 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005306 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005307 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005308 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
5309 object_array_get_with_read_barrier
5310 ? LocationSummary::kCallOnSlowPath
5311 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005312 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005313 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005314 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005315 locations->SetInAt(0, Location::RequiresRegister());
5316 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005317 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005318 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5319 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005320 // The output overlaps in case of long: we don't want the low move
5321 // to overwrite the array's location. Likewise, in the case of an
5322 // object array get with read barriers enabled, we do not want the
5323 // move to overwrite the array's location, as we need it to emit
5324 // the read barrier.
5325 locations->SetOut(
5326 Location::RequiresRegister(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005327 (instruction->GetType() == DataType::Type::kInt64 || object_array_get_with_read_barrier)
5328 ? Location::kOutputOverlap
5329 : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005330 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005331}
5332
5333void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
5334 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005335 Location obj_loc = locations->InAt(0);
5336 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005337 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005338 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005339 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005340
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005341 DataType::Type type = instruction->GetType();
Calin Juravle77520bc2015-01-12 18:45:46 +00005342 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005343 case DataType::Type::kBool:
5344 case DataType::Type::kUint8: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005345 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005346 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005347 break;
5348 }
5349
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005350 case DataType::Type::kInt8: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005351 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005352 __ movsxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005353 break;
5354 }
5355
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005356 case DataType::Type::kUint16: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005357 Register out = out_loc.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07005358 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5359 // Branch cases into compressed and uncompressed for each index's type.
5360 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
5361 NearLabel done, not_compressed;
Vladimir Marko3c89d422017-02-17 11:30:23 +00005362 __ testb(Address(obj, count_offset), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005363 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005364 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
5365 "Expecting 0=compressed, 1=uncompressed");
5366 __ j(kNotZero, &not_compressed);
jessicahandojo4877b792016-09-08 19:49:13 -07005367 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
5368 __ jmp(&done);
5369 __ Bind(&not_compressed);
5370 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5371 __ Bind(&done);
5372 } else {
5373 // Common case for charAt of array of char or when string compression's
5374 // feature is turned off.
5375 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5376 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005377 break;
5378 }
5379
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005380 case DataType::Type::kInt16: {
5381 Register out = out_loc.AsRegister<Register>();
5382 __ movsxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5383 break;
5384 }
5385
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005386 case DataType::Type::kInt32: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005387 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005388 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005389 break;
5390 }
5391
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005392 case DataType::Type::kReference: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005393 static_assert(
5394 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5395 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00005396 // /* HeapReference<Object> */ out =
5397 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5398 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005399 // Note that a potential implicit null check is handled in this
5400 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
5401 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00005402 instruction, out_loc, obj, data_offset, index, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005403 } else {
5404 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005405 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
5406 codegen_->MaybeRecordImplicitNullCheck(instruction);
5407 // If read barriers are enabled, emit read barriers other than
5408 // Baker's using a slow path (and also unpoison the loaded
5409 // reference, if heap poisoning is enabled).
Roland Levillain7c1559a2015-12-15 10:55:36 +00005410 if (index.IsConstant()) {
5411 uint32_t offset =
5412 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005413 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5414 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005415 codegen_->MaybeGenerateReadBarrierSlow(
5416 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5417 }
5418 }
5419 break;
5420 }
5421
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005422 case DataType::Type::kInt64: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005423 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005424 __ movl(out_loc.AsRegisterPairLow<Register>(),
5425 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
5426 codegen_->MaybeRecordImplicitNullCheck(instruction);
5427 __ movl(out_loc.AsRegisterPairHigh<Register>(),
5428 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005429 break;
5430 }
5431
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005432 case DataType::Type::kFloat32: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005433 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005434 __ movss(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005435 break;
5436 }
5437
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005438 case DataType::Type::kFloat64: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005439 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005440 __ movsd(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005441 break;
5442 }
5443
Aart Bik66c158e2018-01-31 12:55:04 -08005444 case DataType::Type::kUint32:
5445 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005446 case DataType::Type::kVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00005447 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005448 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005449 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005450
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005451 if (type == DataType::Type::kReference || type == DataType::Type::kInt64) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005452 // Potential implicit null checks, in the case of reference or
5453 // long arrays, are handled in the previous switch statement.
5454 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005455 codegen_->MaybeRecordImplicitNullCheck(instruction);
5456 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005457}
5458
5459void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005460 DataType::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005461
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005462 bool needs_write_barrier =
5463 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005464 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005465
Vladimir Markoca6fff82017-10-03 14:49:14 +01005466 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Nicolas Geoffray39468442014-09-02 15:17:15 +01005467 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01005468 may_need_runtime_call_for_type_check ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00005469 LocationSummary::kCallOnSlowPath :
5470 LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005471
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005472 bool is_byte_type = DataType::Size(value_type) == 1u;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005473 // We need the inputs to be different than the output in case of long operation.
5474 // In case of a byte operation, the register allocator does not support multiple
5475 // inputs that die at entry with one in a specific register.
5476 locations->SetInAt(0, Location::RequiresRegister());
5477 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5478 if (is_byte_type) {
5479 // Ensure the value is in a byte register.
5480 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005481 } else if (DataType::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05005482 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005483 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005484 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
5485 }
5486 if (needs_write_barrier) {
5487 // Temporary registers for the write barrier.
5488 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5489 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005490 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005491 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005492}
5493
5494void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
5495 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005496 Location array_loc = locations->InAt(0);
5497 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005498 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005499 Location value = locations->InAt(2);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005500 DataType::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005501 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5502 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5503 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005504 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005505 bool needs_write_barrier =
5506 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005507
5508 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005509 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005510 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005511 case DataType::Type::kInt8: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005512 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005513 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_1, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005514 if (value.IsRegister()) {
5515 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005516 } else {
Nicolas Geoffray78612082017-07-24 14:18:53 +01005517 __ movb(address, Immediate(CodeGenerator::GetInt8ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005518 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005519 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005520 break;
5521 }
5522
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005523 case DataType::Type::kUint16:
5524 case DataType::Type::kInt16: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005525 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005526 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_2, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005527 if (value.IsRegister()) {
5528 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005529 } else {
Nicolas Geoffray78612082017-07-24 14:18:53 +01005530 __ movw(address, Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005531 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005532 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005533 break;
5534 }
5535
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005536 case DataType::Type::kReference: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005537 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005538 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005539
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005540 if (!value.IsRegister()) {
5541 // Just setting null.
5542 DCHECK(instruction->InputAt(2)->IsNullConstant());
5543 DCHECK(value.IsConstant()) << value;
5544 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00005545 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005546 DCHECK(!needs_write_barrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005547 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005548 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005549 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005550
5551 DCHECK(needs_write_barrier);
5552 Register register_value = value.AsRegister<Register>();
Roland Levillain16d9f942016-08-25 17:27:56 +01005553 // We cannot use a NearLabel for `done`, as its range may be too
5554 // short when Baker read barriers are enabled.
5555 Label done;
5556 NearLabel not_null, do_put;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005557 SlowPathCode* slow_path = nullptr;
Roland Levillain16d9f942016-08-25 17:27:56 +01005558 Location temp_loc = locations->GetTemp(0);
5559 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005560 if (may_need_runtime_call_for_type_check) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005561 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathX86(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005562 codegen_->AddSlowPath(slow_path);
5563 if (instruction->GetValueCanBeNull()) {
5564 __ testl(register_value, register_value);
5565 __ j(kNotEqual, &not_null);
5566 __ movl(address, Immediate(0));
5567 codegen_->MaybeRecordImplicitNullCheck(instruction);
5568 __ jmp(&done);
5569 __ Bind(&not_null);
5570 }
5571
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005572 // Note that when Baker read barriers are enabled, the type
5573 // checks are performed without read barriers. This is fine,
5574 // even in the case where a class object is in the from-space
5575 // after the flip, as a comparison involving such a type would
5576 // not produce a false positive; it may of course produce a
5577 // false negative, in which case we would take the ArraySet
5578 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01005579
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005580 // /* HeapReference<Class> */ temp = array->klass_
5581 __ movl(temp, Address(array, class_offset));
5582 codegen_->MaybeRecordImplicitNullCheck(instruction);
5583 __ MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01005584
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005585 // /* HeapReference<Class> */ temp = temp->component_type_
5586 __ movl(temp, Address(temp, component_offset));
5587 // If heap poisoning is enabled, no need to unpoison `temp`
5588 // nor the object reference in `register_value->klass`, as
5589 // we are comparing two poisoned references.
5590 __ cmpl(temp, Address(register_value, class_offset));
Roland Levillain16d9f942016-08-25 17:27:56 +01005591
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005592 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5593 __ j(kEqual, &do_put);
5594 // If heap poisoning is enabled, the `temp` reference has
5595 // not been unpoisoned yet; unpoison it now.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005596 __ MaybeUnpoisonHeapReference(temp);
5597
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005598 // If heap poisoning is enabled, no need to unpoison the
5599 // heap reference loaded below, as it is only used for a
5600 // comparison with null.
5601 __ cmpl(Address(temp, super_offset), Immediate(0));
5602 __ j(kNotEqual, slow_path->GetEntryLabel());
5603 __ Bind(&do_put);
5604 } else {
5605 __ j(kNotEqual, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005606 }
5607 }
5608
5609 if (kPoisonHeapReferences) {
5610 __ movl(temp, register_value);
5611 __ PoisonHeapReference(temp);
5612 __ movl(address, temp);
5613 } else {
5614 __ movl(address, register_value);
5615 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005616 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005617 codegen_->MaybeRecordImplicitNullCheck(instruction);
5618 }
5619
5620 Register card = locations->GetTemp(1).AsRegister<Register>();
5621 codegen_->MarkGCCard(
5622 temp, card, array, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
5623 __ Bind(&done);
5624
5625 if (slow_path != nullptr) {
5626 __ Bind(slow_path->GetExitLabel());
5627 }
5628
5629 break;
5630 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005631
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005632 case DataType::Type::kInt32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005633 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005634 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005635 if (value.IsRegister()) {
5636 __ movl(address, value.AsRegister<Register>());
5637 } else {
5638 DCHECK(value.IsConstant()) << value;
5639 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5640 __ movl(address, Immediate(v));
5641 }
5642 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005643 break;
5644 }
5645
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005646 case DataType::Type::kInt64: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005647 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005648 if (value.IsRegisterPair()) {
5649 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
5650 value.AsRegisterPairLow<Register>());
5651 codegen_->MaybeRecordImplicitNullCheck(instruction);
5652 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
5653 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005654 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005655 DCHECK(value.IsConstant());
5656 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
5657 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
5658 Immediate(Low32Bits(val)));
5659 codegen_->MaybeRecordImplicitNullCheck(instruction);
5660 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
5661 Immediate(High32Bits(val)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005662 }
5663 break;
5664 }
5665
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005666 case DataType::Type::kFloat32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005667 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005668 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005669 if (value.IsFpuRegister()) {
5670 __ movss(address, value.AsFpuRegister<XmmRegister>());
5671 } else {
5672 DCHECK(value.IsConstant());
5673 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
5674 __ movl(address, Immediate(v));
5675 }
5676 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005677 break;
5678 }
5679
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005680 case DataType::Type::kFloat64: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005681 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005682 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005683 if (value.IsFpuRegister()) {
5684 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5685 } else {
5686 DCHECK(value.IsConstant());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005687 Address address_hi =
5688 CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset + kX86WordSize);
Mark Mendell81489372015-11-04 11:30:41 -05005689 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5690 __ movl(address, Immediate(Low32Bits(v)));
5691 codegen_->MaybeRecordImplicitNullCheck(instruction);
5692 __ movl(address_hi, Immediate(High32Bits(v)));
5693 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005694 break;
5695 }
5696
Aart Bik66c158e2018-01-31 12:55:04 -08005697 case DataType::Type::kUint32:
5698 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005699 case DataType::Type::kVoid:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005700 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005701 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005702 }
5703}
5704
5705void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005706 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005707 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04005708 if (!instruction->IsEmittedAtUseSite()) {
5709 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5710 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005711}
5712
5713void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04005714 if (instruction->IsEmittedAtUseSite()) {
5715 return;
5716 }
5717
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005718 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005719 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005720 Register obj = locations->InAt(0).AsRegister<Register>();
5721 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005722 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005723 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo4877b792016-09-08 19:49:13 -07005724 // Mask out most significant bit in case the array is String's array of char.
5725 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005726 __ shrl(out, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005727 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005728}
5729
5730void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005731 RegisterSet caller_saves = RegisterSet::Empty();
5732 InvokeRuntimeCallingConvention calling_convention;
5733 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5734 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5735 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005736 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04005737 HInstruction* length = instruction->InputAt(1);
5738 if (!length->IsEmittedAtUseSite()) {
5739 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5740 }
jessicahandojo4877b792016-09-08 19:49:13 -07005741 // Need register to see array's length.
5742 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5743 locations->AddTemp(Location::RequiresRegister());
5744 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005745}
5746
5747void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
jessicahandojo4877b792016-09-08 19:49:13 -07005748 const bool is_string_compressed_char_at =
5749 mirror::kUseStringCompression && instruction->IsStringCharAt();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005750 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005751 Location index_loc = locations->InAt(0);
5752 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07005753 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01005754 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005755
Mark Mendell99dbd682015-04-22 16:18:52 -04005756 if (length_loc.IsConstant()) {
5757 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5758 if (index_loc.IsConstant()) {
5759 // BCE will remove the bounds check if we are guarenteed to pass.
5760 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5761 if (index < 0 || index >= length) {
5762 codegen_->AddSlowPath(slow_path);
5763 __ jmp(slow_path->GetEntryLabel());
5764 } else {
5765 // Some optimization after BCE may have generated this, and we should not
5766 // generate a bounds check if it is a valid range.
5767 }
5768 return;
5769 }
5770
5771 // We have to reverse the jump condition because the length is the constant.
5772 Register index_reg = index_loc.AsRegister<Register>();
5773 __ cmpl(index_reg, Immediate(length));
5774 codegen_->AddSlowPath(slow_path);
5775 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005776 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04005777 HInstruction* array_length = instruction->InputAt(1);
5778 if (array_length->IsEmittedAtUseSite()) {
5779 // Address the length field in the array.
5780 DCHECK(array_length->IsArrayLength());
5781 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
5782 Location array_loc = array_length->GetLocations()->InAt(0);
5783 Address array_len(array_loc.AsRegister<Register>(), len_offset);
jessicahandojo4877b792016-09-08 19:49:13 -07005784 if (is_string_compressed_char_at) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005785 // TODO: if index_loc.IsConstant(), compare twice the index (to compensate for
5786 // the string compression flag) with the in-memory length and avoid the temporary.
jessicahandojo4877b792016-09-08 19:49:13 -07005787 Register length_reg = locations->GetTemp(0).AsRegister<Register>();
5788 __ movl(length_reg, array_len);
5789 codegen_->MaybeRecordImplicitNullCheck(array_length);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005790 __ shrl(length_reg, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005791 codegen_->GenerateIntCompare(length_reg, index_loc);
Mark Mendellee8d9712016-07-12 11:13:15 -04005792 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07005793 // Checking bounds for general case:
5794 // Array of char or string's array with feature compression off.
5795 if (index_loc.IsConstant()) {
5796 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5797 __ cmpl(array_len, Immediate(value));
5798 } else {
5799 __ cmpl(array_len, index_loc.AsRegister<Register>());
5800 }
5801 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendellee8d9712016-07-12 11:13:15 -04005802 }
Mark Mendell99dbd682015-04-22 16:18:52 -04005803 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005804 codegen_->GenerateIntCompare(length_loc, index_loc);
Mark Mendell99dbd682015-04-22 16:18:52 -04005805 }
5806 codegen_->AddSlowPath(slow_path);
5807 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005808 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005809}
5810
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005811void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005812 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005813}
5814
5815void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01005816 if (instruction->GetNext()->IsSuspendCheck() &&
5817 instruction->GetBlock()->GetLoopInformation() != nullptr) {
5818 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
5819 // The back edge will generate the suspend check.
5820 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
5821 }
5822
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005823 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5824}
5825
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005826void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005827 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5828 instruction, LocationSummary::kCallOnSlowPath);
Aart Bikb13c65b2017-03-21 20:14:07 -07005829 // In suspend check slow path, usually there are no caller-save registers at all.
5830 // If SIMD instructions are present, however, we force spilling all live SIMD
5831 // registers in full width (since the runtime only saves/restores lower part).
Aart Bik5576f372017-03-23 16:17:37 -07005832 locations->SetCustomSlowPathCallerSaves(
5833 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005834}
5835
5836void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005837 HBasicBlock* block = instruction->GetBlock();
5838 if (block->GetLoopInformation() != nullptr) {
5839 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5840 // The back edge will generate the suspend check.
5841 return;
5842 }
5843 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5844 // The goto will generate the suspend check.
5845 return;
5846 }
5847 GenerateSuspendCheck(instruction, nullptr);
5848}
5849
5850void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
5851 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005852 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005853 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
5854 if (slow_path == nullptr) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005855 slow_path =
5856 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005857 instruction->SetSlowPath(slow_path);
5858 codegen_->AddSlowPath(slow_path);
5859 if (successor != nullptr) {
5860 DCHECK(successor->IsLoopHeader());
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005861 }
5862 } else {
5863 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5864 }
5865
Andreas Gampe542451c2016-07-26 09:02:02 -07005866 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86PointerSize>().Int32Value()),
Roland Levillain7c1559a2015-12-15 10:55:36 +00005867 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005868 if (successor == nullptr) {
5869 __ j(kNotEqual, slow_path->GetEntryLabel());
5870 __ Bind(slow_path->GetReturnLabel());
5871 } else {
5872 __ j(kEqual, codegen_->GetLabelOf(successor));
5873 __ jmp(slow_path->GetEntryLabel());
5874 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005875}
5876
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005877X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
5878 return codegen_->GetAssembler();
5879}
5880
Aart Bikcfe50bb2017-12-12 14:54:12 -08005881void ParallelMoveResolverX86::MoveMemoryToMemory(int dst, int src, int number_of_words) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005882 ScratchRegisterScope ensure_scratch(
5883 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5884 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5885 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
Mark Mendell7c8d0092015-01-26 11:21:33 -05005886
Aart Bikcfe50bb2017-12-12 14:54:12 -08005887 // Now that temp register is available (possibly spilled), move blocks of memory.
5888 for (int i = 0; i < number_of_words; i++) {
5889 __ movl(temp_reg, Address(ESP, src + stack_offset));
5890 __ movl(Address(ESP, dst + stack_offset), temp_reg);
5891 stack_offset += kX86WordSize;
5892 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005893}
5894
5895void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005896 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005897 Location source = move->GetSource();
5898 Location destination = move->GetDestination();
5899
5900 if (source.IsRegister()) {
5901 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005902 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005903 } else if (destination.IsFpuRegister()) {
5904 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005905 } else {
5906 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005907 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005908 }
David Brazdil74eb1b22015-12-14 11:44:01 +00005909 } else if (source.IsRegisterPair()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005910 size_t elem_size = DataType::Size(DataType::Type::kInt32);
David Brazdil74eb1b22015-12-14 11:44:01 +00005911 // Create stack space for 2 elements.
5912 __ subl(ESP, Immediate(2 * elem_size));
5913 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
5914 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
5915 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
5916 // And remove the temporary stack space we allocated.
5917 __ addl(ESP, Immediate(2 * elem_size));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005918 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005919 if (destination.IsRegister()) {
5920 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
5921 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005922 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005923 } else if (destination.IsRegisterPair()) {
5924 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
5925 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
5926 __ psrlq(src_reg, Immediate(32));
5927 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005928 } else if (destination.IsStackSlot()) {
5929 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07005930 } else if (destination.IsDoubleStackSlot()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005931 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07005932 } else {
5933 DCHECK(destination.IsSIMDStackSlot());
5934 __ movups(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005935 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005936 } else if (source.IsStackSlot()) {
5937 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005938 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005939 } else if (destination.IsFpuRegister()) {
5940 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005941 } else {
5942 DCHECK(destination.IsStackSlot());
Aart Bikcfe50bb2017-12-12 14:54:12 -08005943 MoveMemoryToMemory(destination.GetStackIndex(), source.GetStackIndex(), 1);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005944 }
5945 } else if (source.IsDoubleStackSlot()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005946 if (destination.IsRegisterPair()) {
5947 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
5948 __ movl(destination.AsRegisterPairHigh<Register>(),
5949 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
5950 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005951 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
5952 } else {
5953 DCHECK(destination.IsDoubleStackSlot()) << destination;
Aart Bikcfe50bb2017-12-12 14:54:12 -08005954 MoveMemoryToMemory(destination.GetStackIndex(), source.GetStackIndex(), 2);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005955 }
Aart Bik5576f372017-03-23 16:17:37 -07005956 } else if (source.IsSIMDStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08005957 if (destination.IsFpuRegister()) {
5958 __ movups(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
5959 } else {
5960 DCHECK(destination.IsSIMDStackSlot());
5961 MoveMemoryToMemory(destination.GetStackIndex(), source.GetStackIndex(), 4);
5962 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005963 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005964 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005965 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005966 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005967 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005968 if (value == 0) {
5969 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
5970 } else {
5971 __ movl(destination.AsRegister<Register>(), Immediate(value));
5972 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005973 } else {
5974 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05005975 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005976 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005977 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005978 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005979 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005980 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005981 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005982 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5983 if (value == 0) {
5984 // Easy handling of 0.0.
5985 __ xorps(dest, dest);
5986 } else {
5987 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005988 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5989 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
5990 __ movl(temp, Immediate(value));
5991 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005992 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005993 } else {
5994 DCHECK(destination.IsStackSlot()) << destination;
5995 __ movl(Address(ESP, destination.GetStackIndex()), imm);
5996 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005997 } else if (constant->IsLongConstant()) {
5998 int64_t value = constant->AsLongConstant()->GetValue();
5999 int32_t low_value = Low32Bits(value);
6000 int32_t high_value = High32Bits(value);
6001 Immediate low(low_value);
6002 Immediate high(high_value);
6003 if (destination.IsDoubleStackSlot()) {
6004 __ movl(Address(ESP, destination.GetStackIndex()), low);
6005 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
6006 } else {
6007 __ movl(destination.AsRegisterPairLow<Register>(), low);
6008 __ movl(destination.AsRegisterPairHigh<Register>(), high);
6009 }
6010 } else {
6011 DCHECK(constant->IsDoubleConstant());
6012 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00006013 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006014 int32_t low_value = Low32Bits(value);
6015 int32_t high_value = High32Bits(value);
6016 Immediate low(low_value);
6017 Immediate high(high_value);
6018 if (destination.IsFpuRegister()) {
6019 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
6020 if (value == 0) {
6021 // Easy handling of 0.0.
6022 __ xorpd(dest, dest);
6023 } else {
6024 __ pushl(high);
6025 __ pushl(low);
6026 __ movsd(dest, Address(ESP, 0));
6027 __ addl(ESP, Immediate(8));
6028 }
6029 } else {
6030 DCHECK(destination.IsDoubleStackSlot()) << destination;
6031 __ movl(Address(ESP, destination.GetStackIndex()), low);
6032 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
6033 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01006034 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006035 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006036 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006037 }
6038}
6039
Mark Mendella5c19ce2015-04-01 12:51:05 -04006040void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006041 Register suggested_scratch = reg == EAX ? EBX : EAX;
6042 ScratchRegisterScope ensure_scratch(
6043 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
6044
6045 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
6046 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
6047 __ movl(Address(ESP, mem + stack_offset), reg);
6048 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006049}
6050
Mark Mendell7c8d0092015-01-26 11:21:33 -05006051void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006052 ScratchRegisterScope ensure_scratch(
6053 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
6054
6055 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
6056 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
6057 __ movl(temp_reg, Address(ESP, mem + stack_offset));
6058 __ movss(Address(ESP, mem + stack_offset), reg);
6059 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006060}
6061
Aart Bikcfe50bb2017-12-12 14:54:12 -08006062void ParallelMoveResolverX86::Exchange128(XmmRegister reg, int mem) {
6063 size_t extra_slot = 4 * kX86WordSize;
6064 __ subl(ESP, Immediate(extra_slot));
6065 __ movups(Address(ESP, 0), XmmRegister(reg));
6066 ExchangeMemory(0, mem + extra_slot, 4);
6067 __ movups(XmmRegister(reg), Address(ESP, 0));
6068 __ addl(ESP, Immediate(extra_slot));
6069}
6070
6071void ParallelMoveResolverX86::ExchangeMemory(int mem1, int mem2, int number_of_words) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006072 ScratchRegisterScope ensure_scratch1(
6073 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006074
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006075 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
6076 ScratchRegisterScope ensure_scratch2(
6077 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006078
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006079 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
6080 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
Aart Bikcfe50bb2017-12-12 14:54:12 -08006081
6082 // Now that temp registers are available (possibly spilled), exchange blocks of memory.
6083 for (int i = 0; i < number_of_words; i++) {
6084 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
6085 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
6086 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
6087 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
6088 stack_offset += kX86WordSize;
6089 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006090}
6091
6092void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006093 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006094 Location source = move->GetSource();
6095 Location destination = move->GetDestination();
6096
6097 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04006098 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
6099 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
6100 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
6101 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
6102 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006103 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006104 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006105 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006106 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006107 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08006108 ExchangeMemory(destination.GetStackIndex(), source.GetStackIndex(), 1);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006109 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
6110 // Use XOR Swap algorithm to avoid a temporary.
6111 DCHECK_NE(source.reg(), destination.reg());
6112 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
6113 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
6114 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
6115 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
6116 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
6117 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
6118 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006119 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
6120 // Take advantage of the 16 bytes in the XMM register.
6121 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
6122 Address stack(ESP, destination.GetStackIndex());
6123 // Load the double into the high doubleword.
6124 __ movhpd(reg, stack);
6125
6126 // Store the low double into the destination.
6127 __ movsd(stack, reg);
6128
6129 // Move the high double to the low double.
6130 __ psrldq(reg, Immediate(8));
6131 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
6132 // Take advantage of the 16 bytes in the XMM register.
6133 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
6134 Address stack(ESP, source.GetStackIndex());
6135 // Load the double into the high doubleword.
6136 __ movhpd(reg, stack);
6137
6138 // Store the low double into the destination.
6139 __ movsd(stack, reg);
6140
6141 // Move the high double to the low double.
6142 __ psrldq(reg, Immediate(8));
6143 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08006144 ExchangeMemory(destination.GetStackIndex(), source.GetStackIndex(), 2);
6145 } else if (source.IsSIMDStackSlot() && destination.IsSIMDStackSlot()) {
6146 ExchangeMemory(destination.GetStackIndex(), source.GetStackIndex(), 4);
6147 } else if (source.IsFpuRegister() && destination.IsSIMDStackSlot()) {
6148 Exchange128(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
6149 } else if (destination.IsFpuRegister() && source.IsSIMDStackSlot()) {
6150 Exchange128(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006151 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006152 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006153 }
6154}
6155
6156void ParallelMoveResolverX86::SpillScratch(int reg) {
6157 __ pushl(static_cast<Register>(reg));
6158}
6159
6160void ParallelMoveResolverX86::RestoreScratch(int reg) {
6161 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006162}
6163
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006164HLoadClass::LoadKind CodeGeneratorX86::GetSupportedLoadClassKind(
6165 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006166 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006167 case HLoadClass::LoadKind::kInvalid:
6168 LOG(FATAL) << "UNREACHABLE";
6169 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006170 case HLoadClass::LoadKind::kReferrersClass:
6171 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006172 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006173 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006174 case HLoadClass::LoadKind::kBssEntry:
Vladimir Marko764d4542017-05-16 10:31:41 +01006175 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006176 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006177 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006178 DCHECK(Runtime::Current()->UseJitCompilation());
6179 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01006180 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006181 case HLoadClass::LoadKind::kRuntimeCall:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006182 break;
6183 }
6184 return desired_class_load_kind;
6185}
6186
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006187void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00006188 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006189 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006190 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00006191 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006192 cls,
6193 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00006194 Location::RegisterLocation(EAX));
Vladimir Markoea4c1262017-02-06 19:59:33 +00006195 DCHECK_EQ(calling_convention.GetRegisterAt(0), EAX);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006196 return;
6197 }
Vladimir Marko41559982017-01-06 14:04:23 +00006198 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006199
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006200 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
6201 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006202 ? LocationSummary::kCallOnSlowPath
6203 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01006204 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006205 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006206 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006207 }
6208
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006209 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006210 load_kind == HLoadClass::LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006211 load_kind == HLoadClass::LoadKind::kBootImageClassTable ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006212 load_kind == HLoadClass::LoadKind::kBssEntry) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006213 locations->SetInAt(0, Location::RequiresRegister());
6214 }
6215 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006216 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
6217 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6218 // Rely on the type resolution and/or initialization to save everything.
6219 RegisterSet caller_saves = RegisterSet::Empty();
6220 InvokeRuntimeCallingConvention calling_convention;
6221 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6222 locations->SetCustomSlowPathCallerSaves(caller_saves);
6223 } else {
6224 // For non-Baker read barrier we have a temp-clobbering call.
6225 }
6226 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006227}
6228
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006229Label* CodeGeneratorX86::NewJitRootClassPatch(const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01006230 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006231 Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006232 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006233 // Add a patch entry and return the label.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006234 jit_class_patches_.emplace_back(&dex_file, type_index.index_);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006235 PatchInfo<Label>* info = &jit_class_patches_.back();
6236 return &info->label;
6237}
6238
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006239// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6240// move.
6241void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00006242 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006243 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00006244 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01006245 return;
6246 }
Vladimir Marko41559982017-01-06 14:04:23 +00006247 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01006248
Vladimir Marko41559982017-01-06 14:04:23 +00006249 LocationSummary* locations = cls->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006250 Location out_loc = locations->Out();
6251 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006252
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006253 bool generate_null_check = false;
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006254 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
6255 ? kWithoutReadBarrier
6256 : kCompilerReadBarrierOption;
Vladimir Marko41559982017-01-06 14:04:23 +00006257 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006258 case HLoadClass::LoadKind::kReferrersClass: {
6259 DCHECK(!cls->CanCallRuntime());
6260 DCHECK(!cls->MustGenerateClinitCheck());
6261 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6262 Register current_method = locations->InAt(0).AsRegister<Register>();
6263 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006264 cls,
6265 out_loc,
6266 Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()),
Roland Levillain00468f32016-10-27 18:02:48 +01006267 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006268 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006269 break;
6270 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006271 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006272 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006273 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006274 Register method_address = locations->InAt(0).AsRegister<Register>();
6275 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006276 codegen_->RecordBootImageTypePatch(cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006277 break;
6278 }
6279 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006280 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006281 uint32_t address = dchecked_integral_cast<uint32_t>(
6282 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
6283 DCHECK_NE(address, 0u);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006284 __ movl(out, Immediate(address));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006285 break;
6286 }
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006287 case HLoadClass::LoadKind::kBootImageClassTable: {
6288 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6289 Register method_address = locations->InAt(0).AsRegister<Register>();
6290 __ movl(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006291 codegen_->RecordBootImageTypePatch(cls);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006292 // Extract the reference from the slot data, i.e. clear the hash bits.
6293 int32_t masked_hash = ClassTable::TableSlot::MaskHash(
6294 ComputeModifiedUtf8Hash(cls->GetDexFile().StringByTypeIdx(cls->GetTypeIndex())));
6295 if (masked_hash != 0) {
6296 __ subl(out, Immediate(masked_hash));
6297 }
6298 break;
6299 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006300 case HLoadClass::LoadKind::kBssEntry: {
6301 Register method_address = locations->InAt(0).AsRegister<Register>();
6302 Address address(method_address, CodeGeneratorX86::kDummy32BitOffset);
6303 Label* fixup_label = codegen_->NewTypeBssEntryPatch(cls);
6304 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
6305 generate_null_check = true;
6306 break;
6307 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006308 case HLoadClass::LoadKind::kJitTableAddress: {
6309 Address address = Address::Absolute(CodeGeneratorX86::kDummy32BitOffset);
6310 Label* fixup_label = codegen_->NewJitRootClassPatch(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006311 cls->GetDexFile(), cls->GetTypeIndex(), cls->GetClass());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006312 // /* GcRoot<mirror::Class> */ out = *address
Vladimir Markoea4c1262017-02-06 19:59:33 +00006313 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006314 break;
6315 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006316 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006317 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00006318 LOG(FATAL) << "UNREACHABLE";
6319 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006320 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006321
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006322 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6323 DCHECK(cls->CanCallRuntime());
Vladimir Marko174b2e22017-10-12 13:34:49 +01006324 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathX86(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006325 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
6326 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006327
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006328 if (generate_null_check) {
6329 __ testl(out, out);
6330 __ j(kEqual, slow_path->GetEntryLabel());
6331 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006332
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006333 if (cls->MustGenerateClinitCheck()) {
6334 GenerateClassInitializationCheck(slow_path, out);
6335 } else {
6336 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006337 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006338 }
6339}
6340
6341void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
6342 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006343 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006344 locations->SetInAt(0, Location::RequiresRegister());
6345 if (check->HasUses()) {
6346 locations->SetOut(Location::SameAsFirstInput());
6347 }
6348}
6349
6350void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006351 // We assume the class to not be null.
Vladimir Marko174b2e22017-10-12 13:34:49 +01006352 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006353 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006354 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00006355 GenerateClassInitializationCheck(slow_path,
6356 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006357}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006358
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006359void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07006360 SlowPathCode* slow_path, Register class_reg) {
Vladimir Markodc682aa2018-01-04 18:42:57 +00006361 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
6362 const size_t status_byte_offset =
6363 mirror::Class::StatusOffset().SizeValue() + (status_lsb_position / kBitsPerByte);
6364 constexpr uint32_t shifted_initialized_value =
6365 enum_cast<uint32_t>(ClassStatus::kInitialized) << (status_lsb_position % kBitsPerByte);
6366
6367 __ cmpb(Address(class_reg, status_byte_offset), Immediate(shifted_initialized_value));
Vladimir Marko2c64a832018-01-04 11:31:56 +00006368 __ j(kBelow, slow_path->GetEntryLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006369 __ Bind(slow_path->GetExitLabel());
6370 // No need for memory fence, thanks to the X86 memory model.
6371}
6372
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006373HLoadString::LoadKind CodeGeneratorX86::GetSupportedLoadStringKind(
6374 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006375 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006376 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006377 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00006378 case HLoadString::LoadKind::kBssEntry:
Vladimir Marko764d4542017-05-16 10:31:41 +01006379 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006380 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006381 case HLoadString::LoadKind::kJitTableAddress:
6382 DCHECK(Runtime::Current()->UseJitCompilation());
6383 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01006384 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006385 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006386 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006387 }
6388 return desired_string_load_kind;
6389}
6390
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006391void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006392 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01006393 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006394 HLoadString::LoadKind load_kind = load->GetLoadKind();
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006395 if (load_kind == HLoadString::LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006396 load_kind == HLoadString::LoadKind::kBootImageInternTable ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00006397 load_kind == HLoadString::LoadKind::kBssEntry) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006398 locations->SetInAt(0, Location::RequiresRegister());
6399 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006400 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006401 locations->SetOut(Location::RegisterLocation(EAX));
6402 } else {
6403 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006404 if (load_kind == HLoadString::LoadKind::kBssEntry) {
6405 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00006406 // Rely on the pResolveString to save everything.
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006407 RegisterSet caller_saves = RegisterSet::Empty();
6408 InvokeRuntimeCallingConvention calling_convention;
6409 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6410 locations->SetCustomSlowPathCallerSaves(caller_saves);
6411 } else {
6412 // For non-Baker read barrier we have a temp-clobbering call.
6413 }
6414 }
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006415 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006416}
6417
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006418Label* CodeGeneratorX86::NewJitRootStringPatch(const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01006419 dex::StringIndex string_index,
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006420 Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006421 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006422 // Add a patch entry and return the label.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006423 jit_string_patches_.emplace_back(&dex_file, string_index.index_);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006424 PatchInfo<Label>* info = &jit_string_patches_.back();
6425 return &info->label;
6426}
6427
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006428// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6429// move.
6430void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006431 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006432 Location out_loc = locations->Out();
6433 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006434
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006435 switch (load->GetLoadKind()) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006436 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006437 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006438 Register method_address = locations->InAt(0).AsRegister<Register>();
6439 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006440 codegen_->RecordBootImageStringPatch(load);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006441 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006442 }
6443 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006444 uint32_t address = dchecked_integral_cast<uint32_t>(
6445 reinterpret_cast<uintptr_t>(load->GetString().Get()));
6446 DCHECK_NE(address, 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006447 __ movl(out, Immediate(address));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006448 return;
6449 }
6450 case HLoadString::LoadKind::kBootImageInternTable: {
6451 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6452 Register method_address = locations->InAt(0).AsRegister<Register>();
6453 __ movl(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006454 codegen_->RecordBootImageStringPatch(load);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006455 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006456 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00006457 case HLoadString::LoadKind::kBssEntry: {
6458 Register method_address = locations->InAt(0).AsRegister<Register>();
6459 Address address = Address(method_address, CodeGeneratorX86::kDummy32BitOffset);
6460 Label* fixup_label = codegen_->NewStringBssEntryPatch(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006461 // /* GcRoot<mirror::String> */ out = *address /* PC-relative */
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006462 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
Vladimir Marko174b2e22017-10-12 13:34:49 +01006463 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadStringSlowPathX86(load);
Vladimir Markoaad75c62016-10-03 08:46:48 +00006464 codegen_->AddSlowPath(slow_path);
6465 __ testl(out, out);
6466 __ j(kEqual, slow_path->GetEntryLabel());
6467 __ Bind(slow_path->GetExitLabel());
6468 return;
6469 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006470 case HLoadString::LoadKind::kJitTableAddress: {
6471 Address address = Address::Absolute(CodeGeneratorX86::kDummy32BitOffset);
6472 Label* fixup_label = codegen_->NewJitRootStringPatch(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006473 load->GetDexFile(), load->GetStringIndex(), load->GetString());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006474 // /* GcRoot<mirror::String> */ out = *address
6475 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
6476 return;
6477 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006478 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006479 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006480 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006481
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006482 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006483 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006484 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006485 __ movl(calling_convention.GetRegisterAt(0), Immediate(load->GetStringIndex().index_));
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006486 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6487 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006488}
6489
David Brazdilcb1c0552015-08-04 16:22:25 +01006490static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006491 return Address::Absolute(Thread::ExceptionOffset<kX86PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01006492}
6493
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006494void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
6495 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006496 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006497 locations->SetOut(Location::RequiresRegister());
6498}
6499
6500void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01006501 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
6502}
6503
6504void LocationsBuilderX86::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006505 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
David Brazdilcb1c0552015-08-04 16:22:25 +01006506}
6507
6508void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6509 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006510}
6511
6512void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006513 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6514 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006515 InvokeRuntimeCallingConvention calling_convention;
6516 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6517}
6518
6519void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006520 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006521 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006522}
6523
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006524// Temp is used for read barrier.
6525static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
6526 if (kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00006527 !kUseBakerReadBarrier &&
6528 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain7c1559a2015-12-15 10:55:36 +00006529 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006530 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
6531 return 1;
6532 }
6533 return 0;
6534}
6535
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006536// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006537// interface pointer, one for loading the current interface.
6538// The other checks have one temp for loading the object's class.
6539static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
Vladimir Markoe619f6c2017-12-12 16:00:01 +00006540 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006541 return 2;
6542 }
6543 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006544}
6545
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006546void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006547 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006548 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01006549 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006550 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006551 case TypeCheckKind::kExactCheck:
6552 case TypeCheckKind::kAbstractClassCheck:
6553 case TypeCheckKind::kClassHierarchyCheck:
Vladimir Marko87584542017-12-12 17:47:52 +00006554 case TypeCheckKind::kArrayObjectCheck: {
6555 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
6556 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
6557 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006558 break;
Vladimir Marko87584542017-12-12 17:47:52 +00006559 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006560 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006561 case TypeCheckKind::kUnresolvedCheck:
6562 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006563 call_kind = LocationSummary::kCallOnSlowPath;
6564 break;
6565 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006566
Vladimir Markoca6fff82017-10-03 14:49:14 +01006567 LocationSummary* locations =
6568 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01006569 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006570 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006571 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006572 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006573 locations->SetInAt(1, Location::Any());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006574 // Note that TypeCheckSlowPathX86 uses this "out" register too.
6575 locations->SetOut(Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006576 // When read barriers are enabled, we need a temporary register for some cases.
6577 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006578}
6579
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006580void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006581 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006582 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006583 Location obj_loc = locations->InAt(0);
6584 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006585 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006586 Location out_loc = locations->Out();
6587 Register out = out_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006588 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
6589 DCHECK_LE(num_temps, 1u);
6590 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006591 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006592 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6593 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6594 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07006595 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006596 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006597
6598 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006599 // Avoid null check if we know obj is not null.
6600 if (instruction->MustDoNullCheck()) {
6601 __ testl(obj, obj);
6602 __ j(kEqual, &zero);
6603 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006604
Roland Levillain7c1559a2015-12-15 10:55:36 +00006605 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006606 case TypeCheckKind::kExactCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00006607 ReadBarrierOption read_barrier_option =
6608 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006609 // /* HeapReference<Class> */ out = obj->klass_
6610 GenerateReferenceLoadTwoRegisters(instruction,
6611 out_loc,
6612 obj_loc,
6613 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00006614 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006615 if (cls.IsRegister()) {
6616 __ cmpl(out, cls.AsRegister<Register>());
6617 } else {
6618 DCHECK(cls.IsStackSlot()) << cls;
6619 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6620 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006621
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006622 // Classes must be equal for the instanceof to succeed.
6623 __ j(kNotEqual, &zero);
6624 __ movl(out, Immediate(1));
6625 __ jmp(&done);
6626 break;
6627 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006628
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006629 case TypeCheckKind::kAbstractClassCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00006630 ReadBarrierOption read_barrier_option =
6631 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006632 // /* HeapReference<Class> */ out = obj->klass_
6633 GenerateReferenceLoadTwoRegisters(instruction,
6634 out_loc,
6635 obj_loc,
6636 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00006637 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006638 // If the class is abstract, we eagerly fetch the super class of the
6639 // object to avoid doing a comparison we know will fail.
6640 NearLabel loop;
6641 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006642 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006643 GenerateReferenceLoadOneRegister(instruction,
6644 out_loc,
6645 super_offset,
6646 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00006647 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006648 __ testl(out, out);
6649 // If `out` is null, we use it for the result, and jump to `done`.
6650 __ j(kEqual, &done);
6651 if (cls.IsRegister()) {
6652 __ cmpl(out, cls.AsRegister<Register>());
6653 } else {
6654 DCHECK(cls.IsStackSlot()) << cls;
6655 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6656 }
6657 __ j(kNotEqual, &loop);
6658 __ movl(out, Immediate(1));
6659 if (zero.IsLinked()) {
6660 __ jmp(&done);
6661 }
6662 break;
6663 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006664
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006665 case TypeCheckKind::kClassHierarchyCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00006666 ReadBarrierOption read_barrier_option =
6667 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006668 // /* HeapReference<Class> */ out = obj->klass_
6669 GenerateReferenceLoadTwoRegisters(instruction,
6670 out_loc,
6671 obj_loc,
6672 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00006673 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006674 // Walk over the class hierarchy to find a match.
6675 NearLabel loop, success;
6676 __ Bind(&loop);
6677 if (cls.IsRegister()) {
6678 __ cmpl(out, cls.AsRegister<Register>());
6679 } else {
6680 DCHECK(cls.IsStackSlot()) << cls;
6681 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6682 }
6683 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006684 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006685 GenerateReferenceLoadOneRegister(instruction,
6686 out_loc,
6687 super_offset,
6688 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00006689 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006690 __ testl(out, out);
6691 __ j(kNotEqual, &loop);
6692 // If `out` is null, we use it for the result, and jump to `done`.
6693 __ jmp(&done);
6694 __ Bind(&success);
6695 __ movl(out, Immediate(1));
6696 if (zero.IsLinked()) {
6697 __ jmp(&done);
6698 }
6699 break;
6700 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006701
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006702 case TypeCheckKind::kArrayObjectCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00006703 ReadBarrierOption read_barrier_option =
6704 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006705 // /* HeapReference<Class> */ out = obj->klass_
6706 GenerateReferenceLoadTwoRegisters(instruction,
6707 out_loc,
6708 obj_loc,
6709 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00006710 read_barrier_option);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006711 // Do an exact check.
6712 NearLabel exact_check;
6713 if (cls.IsRegister()) {
6714 __ cmpl(out, cls.AsRegister<Register>());
6715 } else {
6716 DCHECK(cls.IsStackSlot()) << cls;
6717 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6718 }
6719 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006720 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006721 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006722 GenerateReferenceLoadOneRegister(instruction,
6723 out_loc,
6724 component_offset,
6725 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00006726 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006727 __ testl(out, out);
6728 // If `out` is null, we use it for the result, and jump to `done`.
6729 __ j(kEqual, &done);
6730 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
6731 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006732 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006733 __ movl(out, Immediate(1));
6734 __ jmp(&done);
6735 break;
6736 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006737
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006738 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006739 // No read barrier since the slow path will retry upon failure.
6740 // /* HeapReference<Class> */ out = obj->klass_
6741 GenerateReferenceLoadTwoRegisters(instruction,
6742 out_loc,
6743 obj_loc,
6744 class_offset,
6745 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006746 if (cls.IsRegister()) {
6747 __ cmpl(out, cls.AsRegister<Register>());
6748 } else {
6749 DCHECK(cls.IsStackSlot()) << cls;
6750 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6751 }
6752 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01006753 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
6754 instruction, /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006755 codegen_->AddSlowPath(slow_path);
6756 __ j(kNotEqual, slow_path->GetEntryLabel());
6757 __ movl(out, Immediate(1));
6758 if (zero.IsLinked()) {
6759 __ jmp(&done);
6760 }
6761 break;
6762 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006763
Calin Juravle98893e12015-10-02 21:05:03 +01006764 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006765 case TypeCheckKind::kInterfaceCheck: {
6766 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006767 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00006768 // cases.
6769 //
6770 // We cannot directly call the InstanceofNonTrivial runtime
6771 // entry point without resorting to a type checking slow path
6772 // here (i.e. by calling InvokeRuntime directly), as it would
6773 // require to assign fixed registers for the inputs of this
6774 // HInstanceOf instruction (following the runtime calling
6775 // convention), which might be cluttered by the potential first
6776 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006777 //
6778 // TODO: Introduce a new runtime entry point taking the object
6779 // to test (instead of its class) as argument, and let it deal
6780 // with the read barrier issues. This will let us refactor this
6781 // case of the `switch` code as it was previously (with a direct
6782 // call to the runtime not using a type checking slow path).
6783 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006784 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01006785 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
6786 instruction, /* is_fatal */ false);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006787 codegen_->AddSlowPath(slow_path);
6788 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006789 if (zero.IsLinked()) {
6790 __ jmp(&done);
6791 }
6792 break;
6793 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006794 }
6795
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006796 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006797 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006798 __ xorl(out, out);
6799 }
6800
6801 if (done.IsLinked()) {
6802 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006803 }
6804
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006805 if (slow_path != nullptr) {
6806 __ Bind(slow_path->GetExitLabel());
6807 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006808}
6809
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006810void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006811 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko87584542017-12-12 17:47:52 +00006812 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01006813 LocationSummary* locations =
6814 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006815 locations->SetInAt(0, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006816 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
6817 // Require a register for the interface check since there is a loop that compares the class to
6818 // a memory address.
6819 locations->SetInAt(1, Location::RequiresRegister());
6820 } else {
6821 locations->SetInAt(1, Location::Any());
6822 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006823 // Note that TypeCheckSlowPathX86 uses this "temp" register too.
6824 locations->AddTemp(Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006825 // When read barriers are enabled, we need an additional temporary register for some cases.
6826 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
6827}
6828
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006829void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006830 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006831 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006832 Location obj_loc = locations->InAt(0);
6833 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006834 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006835 Location temp_loc = locations->GetTemp(0);
6836 Register temp = temp_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006837 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
6838 DCHECK_GE(num_temps, 1u);
6839 DCHECK_LE(num_temps, 2u);
6840 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
6841 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6842 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6843 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6844 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
6845 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
6846 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
6847 const uint32_t object_array_data_offset =
6848 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006849
Vladimir Marko87584542017-12-12 17:47:52 +00006850 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006851 SlowPathCode* type_check_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006852 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
6853 instruction, is_type_check_slow_path_fatal);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006854 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006855
Roland Levillain0d5a2812015-11-13 10:07:31 +00006856 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006857 // Avoid null check if we know obj is not null.
6858 if (instruction->MustDoNullCheck()) {
6859 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006860 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006861 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006862
Roland Levillain0d5a2812015-11-13 10:07:31 +00006863 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006864 case TypeCheckKind::kExactCheck:
6865 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006866 // /* HeapReference<Class> */ temp = obj->klass_
6867 GenerateReferenceLoadTwoRegisters(instruction,
6868 temp_loc,
6869 obj_loc,
6870 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006871 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006872
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006873 if (cls.IsRegister()) {
6874 __ cmpl(temp, cls.AsRegister<Register>());
6875 } else {
6876 DCHECK(cls.IsStackSlot()) << cls;
6877 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6878 }
6879 // Jump to slow path for throwing the exception or doing a
6880 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006881 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006882 break;
6883 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006884
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006885 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006886 // /* HeapReference<Class> */ temp = obj->klass_
6887 GenerateReferenceLoadTwoRegisters(instruction,
6888 temp_loc,
6889 obj_loc,
6890 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006891 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006892
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006893 // If the class is abstract, we eagerly fetch the super class of the
6894 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006895 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006896 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006897 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006898 GenerateReferenceLoadOneRegister(instruction,
6899 temp_loc,
6900 super_offset,
6901 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006902 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006903
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006904 // If the class reference currently in `temp` is null, jump to the slow path to throw the
6905 // exception.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006906 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006907 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006908
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006909 // Otherwise, compare the classes
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006910 if (cls.IsRegister()) {
6911 __ cmpl(temp, cls.AsRegister<Register>());
6912 } else {
6913 DCHECK(cls.IsStackSlot()) << cls;
6914 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6915 }
6916 __ j(kNotEqual, &loop);
6917 break;
6918 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006919
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006920 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006921 // /* HeapReference<Class> */ temp = obj->klass_
6922 GenerateReferenceLoadTwoRegisters(instruction,
6923 temp_loc,
6924 obj_loc,
6925 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006926 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006927
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006928 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006929 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006930 __ Bind(&loop);
6931 if (cls.IsRegister()) {
6932 __ cmpl(temp, cls.AsRegister<Register>());
6933 } else {
6934 DCHECK(cls.IsStackSlot()) << cls;
6935 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6936 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006937 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006938
Roland Levillain0d5a2812015-11-13 10:07:31 +00006939 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006940 GenerateReferenceLoadOneRegister(instruction,
6941 temp_loc,
6942 super_offset,
6943 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006944 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006945
6946 // If the class reference currently in `temp` is not null, jump
6947 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006948 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006949 __ j(kNotZero, &loop);
6950 // Otherwise, jump to the slow path to throw the exception.;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006951 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006952 break;
6953 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006954
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006955 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006956 // /* HeapReference<Class> */ temp = obj->klass_
6957 GenerateReferenceLoadTwoRegisters(instruction,
6958 temp_loc,
6959 obj_loc,
6960 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006961 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006962
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006963 // Do an exact check.
6964 if (cls.IsRegister()) {
6965 __ cmpl(temp, cls.AsRegister<Register>());
6966 } else {
6967 DCHECK(cls.IsStackSlot()) << cls;
6968 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6969 }
6970 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006971
6972 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006973 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006974 GenerateReferenceLoadOneRegister(instruction,
6975 temp_loc,
6976 component_offset,
6977 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006978 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006979
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006980 // If the component type is null (i.e. the object not an array), jump to the slow path to
6981 // throw the exception. Otherwise proceed with the check.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006982 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006983 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006984
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006985 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006986 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006987 break;
6988 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006989
Calin Juravle98893e12015-10-02 21:05:03 +01006990 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006991 // We always go into the type check slow path for the unresolved check case.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006992 // We cannot directly call the CheckCast runtime entry point
6993 // without resorting to a type checking slow path here (i.e. by
6994 // calling InvokeRuntime directly), as it would require to
6995 // assign fixed registers for the inputs of this HInstanceOf
6996 // instruction (following the runtime calling convention), which
6997 // might be cluttered by the potential first read barrier
6998 // emission at the beginning of this method.
6999 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007000 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007001
7002 case TypeCheckKind::kInterfaceCheck: {
Vladimir Markoe619f6c2017-12-12 16:00:01 +00007003 // Fast path for the interface check. Try to avoid read barriers to improve the fast path.
7004 // We can not get false positives by doing this.
7005 // /* HeapReference<Class> */ temp = obj->klass_
7006 GenerateReferenceLoadTwoRegisters(instruction,
7007 temp_loc,
7008 obj_loc,
7009 class_offset,
7010 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007011
Vladimir Markoe619f6c2017-12-12 16:00:01 +00007012 // /* HeapReference<Class> */ temp = temp->iftable_
7013 GenerateReferenceLoadTwoRegisters(instruction,
7014 temp_loc,
7015 temp_loc,
7016 iftable_offset,
7017 kWithoutReadBarrier);
7018 // Iftable is never null.
7019 __ movl(maybe_temp2_loc.AsRegister<Register>(), Address(temp, array_length_offset));
7020 // Maybe poison the `cls` for direct comparison with memory.
7021 __ MaybePoisonHeapReference(cls.AsRegister<Register>());
7022 // Loop through the iftable and check if any class matches.
7023 NearLabel start_loop;
7024 __ Bind(&start_loop);
7025 // Need to subtract first to handle the empty array case.
7026 __ subl(maybe_temp2_loc.AsRegister<Register>(), Immediate(2));
7027 __ j(kNegative, type_check_slow_path->GetEntryLabel());
7028 // Go to next interface if the classes do not match.
7029 __ cmpl(cls.AsRegister<Register>(),
7030 CodeGeneratorX86::ArrayAddress(temp,
7031 maybe_temp2_loc,
7032 TIMES_4,
7033 object_array_data_offset));
7034 __ j(kNotEqual, &start_loop);
7035 // If `cls` was poisoned above, unpoison it.
7036 __ MaybeUnpoisonHeapReference(cls.AsRegister<Register>());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007037 break;
7038 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007039 }
7040 __ Bind(&done);
7041
Roland Levillain0d5a2812015-11-13 10:07:31 +00007042 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007043}
7044
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007045void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007046 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
7047 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007048 InvokeRuntimeCallingConvention calling_convention;
7049 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7050}
7051
7052void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01007053 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject
7054 : kQuickUnlockObject,
Alexandre Rames8158f282015-08-07 10:26:17 +01007055 instruction,
Serban Constantinescuba45db02016-07-12 22:53:02 +01007056 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00007057 if (instruction->IsEnter()) {
7058 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
7059 } else {
7060 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
7061 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007062}
7063
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007064void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
7065void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
7066void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
7067
7068void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
7069 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007070 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007071 DCHECK(instruction->GetResultType() == DataType::Type::kInt32
7072 || instruction->GetResultType() == DataType::Type::kInt64);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007073 locations->SetInAt(0, Location::RequiresRegister());
7074 locations->SetInAt(1, Location::Any());
7075 locations->SetOut(Location::SameAsFirstInput());
7076}
7077
7078void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
7079 HandleBitwiseOperation(instruction);
7080}
7081
7082void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
7083 HandleBitwiseOperation(instruction);
7084}
7085
7086void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
7087 HandleBitwiseOperation(instruction);
7088}
7089
7090void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
7091 LocationSummary* locations = instruction->GetLocations();
7092 Location first = locations->InAt(0);
7093 Location second = locations->InAt(1);
7094 DCHECK(first.Equals(locations->Out()));
7095
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007096 if (instruction->GetResultType() == DataType::Type::kInt32) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007097 if (second.IsRegister()) {
7098 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007099 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007100 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007101 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007102 } else {
7103 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00007104 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007105 }
7106 } else if (second.IsConstant()) {
7107 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00007108 __ andl(first.AsRegister<Register>(),
7109 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007110 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00007111 __ orl(first.AsRegister<Register>(),
7112 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007113 } else {
7114 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00007115 __ xorl(first.AsRegister<Register>(),
7116 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007117 }
7118 } else {
7119 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007120 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007121 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007122 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007123 } else {
7124 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00007125 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007126 }
7127 }
7128 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007129 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007130 if (second.IsRegisterPair()) {
7131 if (instruction->IsAnd()) {
7132 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7133 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7134 } else if (instruction->IsOr()) {
7135 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7136 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7137 } else {
7138 DCHECK(instruction->IsXor());
7139 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7140 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7141 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007142 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007143 if (instruction->IsAnd()) {
7144 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7145 __ andl(first.AsRegisterPairHigh<Register>(),
7146 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7147 } else if (instruction->IsOr()) {
7148 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7149 __ orl(first.AsRegisterPairHigh<Register>(),
7150 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7151 } else {
7152 DCHECK(instruction->IsXor());
7153 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7154 __ xorl(first.AsRegisterPairHigh<Register>(),
7155 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7156 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007157 } else {
7158 DCHECK(second.IsConstant()) << second;
7159 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007160 int32_t low_value = Low32Bits(value);
7161 int32_t high_value = High32Bits(value);
7162 Immediate low(low_value);
7163 Immediate high(high_value);
7164 Register first_low = first.AsRegisterPairLow<Register>();
7165 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007166 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007167 if (low_value == 0) {
7168 __ xorl(first_low, first_low);
7169 } else if (low_value != -1) {
7170 __ andl(first_low, low);
7171 }
7172 if (high_value == 0) {
7173 __ xorl(first_high, first_high);
7174 } else if (high_value != -1) {
7175 __ andl(first_high, high);
7176 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007177 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007178 if (low_value != 0) {
7179 __ orl(first_low, low);
7180 }
7181 if (high_value != 0) {
7182 __ orl(first_high, high);
7183 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007184 } else {
7185 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007186 if (low_value != 0) {
7187 __ xorl(first_low, low);
7188 }
7189 if (high_value != 0) {
7190 __ xorl(first_high, high);
7191 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007192 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007193 }
7194 }
7195}
7196
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007197void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(
7198 HInstruction* instruction,
7199 Location out,
7200 uint32_t offset,
7201 Location maybe_temp,
7202 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007203 Register out_reg = out.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007204 if (read_barrier_option == kWithReadBarrier) {
7205 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007206 if (kUseBakerReadBarrier) {
7207 // Load with fast path based Baker's read barrier.
7208 // /* HeapReference<Object> */ out = *(out + offset)
7209 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00007210 instruction, out, out_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007211 } else {
7212 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007213 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain7c1559a2015-12-15 10:55:36 +00007214 // in the following move operation, as we will need it for the
7215 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00007216 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007217 __ movl(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007218 // /* HeapReference<Object> */ out = *(out + offset)
7219 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007220 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007221 }
7222 } else {
7223 // Plain load with no read barrier.
7224 // /* HeapReference<Object> */ out = *(out + offset)
7225 __ movl(out_reg, Address(out_reg, offset));
7226 __ MaybeUnpoisonHeapReference(out_reg);
7227 }
7228}
7229
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007230void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(
7231 HInstruction* instruction,
7232 Location out,
7233 Location obj,
7234 uint32_t offset,
7235 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007236 Register out_reg = out.AsRegister<Register>();
7237 Register obj_reg = obj.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007238 if (read_barrier_option == kWithReadBarrier) {
7239 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007240 if (kUseBakerReadBarrier) {
7241 // Load with fast path based Baker's read barrier.
7242 // /* HeapReference<Object> */ out = *(obj + offset)
7243 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00007244 instruction, out, obj_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007245 } else {
7246 // Load with slow path based read barrier.
7247 // /* HeapReference<Object> */ out = *(obj + offset)
7248 __ movl(out_reg, Address(obj_reg, offset));
7249 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
7250 }
7251 } else {
7252 // Plain load with no read barrier.
7253 // /* HeapReference<Object> */ out = *(obj + offset)
7254 __ movl(out_reg, Address(obj_reg, offset));
7255 __ MaybeUnpoisonHeapReference(out_reg);
7256 }
7257}
7258
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007259void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(
7260 HInstruction* instruction,
7261 Location root,
7262 const Address& address,
7263 Label* fixup_label,
7264 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007265 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007266 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07007267 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007268 if (kUseBakerReadBarrier) {
7269 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
7270 // Baker's read barrier are used:
7271 //
Roland Levillaind966ce72017-02-09 16:20:14 +00007272 // root = obj.field;
7273 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
7274 // if (temp != null) {
7275 // root = temp(root)
Roland Levillain7c1559a2015-12-15 10:55:36 +00007276 // }
7277
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007278 // /* GcRoot<mirror::Object> */ root = *address
7279 __ movl(root_reg, address);
7280 if (fixup_label != nullptr) {
7281 __ Bind(fixup_label);
7282 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007283 static_assert(
7284 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
7285 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
7286 "have different sizes.");
7287 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
7288 "art::mirror::CompressedReference<mirror::Object> and int32_t "
7289 "have different sizes.");
7290
Vladimir Marko953437b2016-08-24 08:30:46 +00007291 // Slow path marking the GC root `root`.
Vladimir Marko174b2e22017-10-12 13:34:49 +01007292 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathX86(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007293 instruction, root, /* unpoison_ref_before_marking */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007294 codegen_->AddSlowPath(slow_path);
7295
Roland Levillaind966ce72017-02-09 16:20:14 +00007296 // Test the entrypoint (`Thread::Current()->pReadBarrierMarkReg ## root.reg()`).
7297 const int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +01007298 Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(root.reg());
Roland Levillaind966ce72017-02-09 16:20:14 +00007299 __ fs()->cmpl(Address::Absolute(entry_point_offset), Immediate(0));
7300 // The entrypoint is null when the GC is not marking.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007301 __ j(kNotEqual, slow_path->GetEntryLabel());
7302 __ Bind(slow_path->GetExitLabel());
7303 } else {
7304 // GC root loaded through a slow path for read barriers other
7305 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007306 // /* GcRoot<mirror::Object>* */ root = address
7307 __ leal(root_reg, address);
7308 if (fixup_label != nullptr) {
7309 __ Bind(fixup_label);
7310 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007311 // /* mirror::Object* */ root = root->Read()
7312 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
7313 }
7314 } else {
7315 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007316 // /* GcRoot<mirror::Object> */ root = *address
7317 __ movl(root_reg, address);
7318 if (fixup_label != nullptr) {
7319 __ Bind(fixup_label);
7320 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007321 // Note that GC roots are not affected by heap poisoning, thus we
7322 // do not have to unpoison `root_reg` here.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007323 }
7324}
7325
7326void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
7327 Location ref,
7328 Register obj,
7329 uint32_t offset,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007330 bool needs_null_check) {
7331 DCHECK(kEmitCompilerReadBarrier);
7332 DCHECK(kUseBakerReadBarrier);
7333
7334 // /* HeapReference<Object> */ ref = *(obj + offset)
7335 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007336 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007337}
7338
7339void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
7340 Location ref,
7341 Register obj,
7342 uint32_t data_offset,
7343 Location index,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007344 bool needs_null_check) {
7345 DCHECK(kEmitCompilerReadBarrier);
7346 DCHECK(kUseBakerReadBarrier);
7347
Roland Levillain3d312422016-06-23 13:53:42 +01007348 static_assert(
7349 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
7350 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00007351 // /* HeapReference<Object> */ ref =
7352 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007353 Address src = CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007354 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007355}
7356
7357void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
7358 Location ref,
7359 Register obj,
7360 const Address& src,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007361 bool needs_null_check,
7362 bool always_update_field,
7363 Register* temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007364 DCHECK(kEmitCompilerReadBarrier);
7365 DCHECK(kUseBakerReadBarrier);
7366
7367 // In slow path based read barriers, the read barrier call is
7368 // inserted after the original load. However, in fast path based
7369 // Baker's read barriers, we need to perform the load of
7370 // mirror::Object::monitor_ *before* the original reference load.
7371 // This load-load ordering is required by the read barrier.
7372 // The fast path/slow path (for Baker's algorithm) should look like:
7373 //
7374 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7375 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7376 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007377 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain7c1559a2015-12-15 10:55:36 +00007378 // if (is_gray) {
7379 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7380 // }
7381 //
7382 // Note: the original implementation in ReadBarrier::Barrier is
7383 // slightly more complex as:
7384 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007385 // the high-bits of rb_state, which are expected to be all zeroes
7386 // (we use CodeGeneratorX86::GenerateMemoryBarrier instead here,
7387 // which is a no-op thanks to the x86 memory model);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007388 // - it performs additional checks that we do not do here for
7389 // performance reasons.
7390
7391 Register ref_reg = ref.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +00007392 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7393
Vladimir Marko953437b2016-08-24 08:30:46 +00007394 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007395 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
7396 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko953437b2016-08-24 08:30:46 +00007397 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
7398 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
7399 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
7400
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007401 // if (rb_state == ReadBarrier::GrayState())
Vladimir Marko953437b2016-08-24 08:30:46 +00007402 // ref = ReadBarrier::Mark(ref);
7403 // At this point, just do the "if" and make sure that flags are preserved until the branch.
7404 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain7c1559a2015-12-15 10:55:36 +00007405 if (needs_null_check) {
7406 MaybeRecordImplicitNullCheck(instruction);
7407 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007408
7409 // Load fence to prevent load-load reordering.
7410 // Note that this is a no-op, thanks to the x86 memory model.
7411 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
7412
7413 // The actual reference load.
7414 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00007415 __ movl(ref_reg, src); // Flags are unaffected.
7416
7417 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
7418 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007419 SlowPathCode* slow_path;
7420 if (always_update_field) {
7421 DCHECK(temp != nullptr);
Vladimir Marko174b2e22017-10-12 13:34:49 +01007422 slow_path = new (GetScopedAllocator()) ReadBarrierMarkAndUpdateFieldSlowPathX86(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007423 instruction, ref, obj, src, /* unpoison_ref_before_marking */ true, *temp);
7424 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01007425 slow_path = new (GetScopedAllocator()) ReadBarrierMarkSlowPathX86(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007426 instruction, ref, /* unpoison_ref_before_marking */ true);
7427 }
Vladimir Marko953437b2016-08-24 08:30:46 +00007428 AddSlowPath(slow_path);
7429
7430 // We have done the "if" of the gray bit check above, now branch based on the flags.
7431 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain7c1559a2015-12-15 10:55:36 +00007432
7433 // Object* ref = ref_addr->AsMirrorPtr()
7434 __ MaybeUnpoisonHeapReference(ref_reg);
7435
Roland Levillain7c1559a2015-12-15 10:55:36 +00007436 __ Bind(slow_path->GetExitLabel());
7437}
7438
7439void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
7440 Location out,
7441 Location ref,
7442 Location obj,
7443 uint32_t offset,
7444 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007445 DCHECK(kEmitCompilerReadBarrier);
7446
Roland Levillain7c1559a2015-12-15 10:55:36 +00007447 // Insert a slow path based read barrier *after* the reference load.
7448 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007449 // If heap poisoning is enabled, the unpoisoning of the loaded
7450 // reference will be carried out by the runtime within the slow
7451 // path.
7452 //
7453 // Note that `ref` currently does not get unpoisoned (when heap
7454 // poisoning is enabled), which is alright as the `ref` argument is
7455 // not used by the artReadBarrierSlow entry point.
7456 //
7457 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01007458 SlowPathCode* slow_path = new (GetScopedAllocator())
Roland Levillain0d5a2812015-11-13 10:07:31 +00007459 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
7460 AddSlowPath(slow_path);
7461
Roland Levillain0d5a2812015-11-13 10:07:31 +00007462 __ jmp(slow_path->GetEntryLabel());
7463 __ Bind(slow_path->GetExitLabel());
7464}
7465
Roland Levillain7c1559a2015-12-15 10:55:36 +00007466void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7467 Location out,
7468 Location ref,
7469 Location obj,
7470 uint32_t offset,
7471 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007472 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007473 // Baker's read barriers shall be handled by the fast path
7474 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
7475 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007476 // If heap poisoning is enabled, unpoisoning will be taken care of
7477 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007478 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007479 } else if (kPoisonHeapReferences) {
7480 __ UnpoisonHeapReference(out.AsRegister<Register>());
7481 }
7482}
7483
Roland Levillain7c1559a2015-12-15 10:55:36 +00007484void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7485 Location out,
7486 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007487 DCHECK(kEmitCompilerReadBarrier);
7488
Roland Levillain7c1559a2015-12-15 10:55:36 +00007489 // Insert a slow path based read barrier *after* the GC root load.
7490 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007491 // Note that GC roots are not affected by heap poisoning, so we do
7492 // not need to do anything special for this here.
7493 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007494 new (GetScopedAllocator()) ReadBarrierForRootSlowPathX86(instruction, out, root);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007495 AddSlowPath(slow_path);
7496
Roland Levillain0d5a2812015-11-13 10:07:31 +00007497 __ jmp(slow_path->GetEntryLabel());
7498 __ Bind(slow_path->GetExitLabel());
7499}
7500
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007501void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007502 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007503 LOG(FATAL) << "Unreachable";
7504}
7505
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007506void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007507 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007508 LOG(FATAL) << "Unreachable";
7509}
7510
Mark Mendellfe57faa2015-09-18 09:26:15 -04007511// Simple implementation of packed switch - generate cascaded compare/jumps.
7512void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7513 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007514 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendellfe57faa2015-09-18 09:26:15 -04007515 locations->SetInAt(0, Location::RequiresRegister());
7516}
7517
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007518void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
7519 int32_t lower_bound,
7520 uint32_t num_entries,
7521 HBasicBlock* switch_block,
7522 HBasicBlock* default_block) {
7523 // Figure out the correct compare values and jump conditions.
7524 // Handle the first compare/branch as a special case because it might
7525 // jump to the default case.
7526 DCHECK_GT(num_entries, 2u);
7527 Condition first_condition;
7528 uint32_t index;
7529 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
7530 if (lower_bound != 0) {
7531 first_condition = kLess;
7532 __ cmpl(value_reg, Immediate(lower_bound));
7533 __ j(first_condition, codegen_->GetLabelOf(default_block));
7534 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007535
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007536 index = 1;
7537 } else {
7538 // Handle all the compare/jumps below.
7539 first_condition = kBelow;
7540 index = 0;
7541 }
7542
7543 // Handle the rest of the compare/jumps.
7544 for (; index + 1 < num_entries; index += 2) {
7545 int32_t compare_to_value = lower_bound + index + 1;
7546 __ cmpl(value_reg, Immediate(compare_to_value));
7547 // Jump to successors[index] if value < case_value[index].
7548 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
7549 // Jump to successors[index + 1] if value == case_value[index + 1].
7550 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
7551 }
7552
7553 if (index != num_entries) {
7554 // There are an odd number of entries. Handle the last one.
7555 DCHECK_EQ(index + 1, num_entries);
7556 __ cmpl(value_reg, Immediate(lower_bound + index));
7557 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007558 }
7559
7560 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007561 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
7562 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007563 }
7564}
7565
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007566void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7567 int32_t lower_bound = switch_instr->GetStartValue();
7568 uint32_t num_entries = switch_instr->GetNumEntries();
7569 LocationSummary* locations = switch_instr->GetLocations();
7570 Register value_reg = locations->InAt(0).AsRegister<Register>();
7571
7572 GenPackedSwitchWithCompares(value_reg,
7573 lower_bound,
7574 num_entries,
7575 switch_instr->GetBlock(),
7576 switch_instr->GetDefaultBlock());
7577}
7578
Mark Mendell805b3b52015-09-18 14:10:29 -04007579void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7580 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007581 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendell805b3b52015-09-18 14:10:29 -04007582 locations->SetInAt(0, Location::RequiresRegister());
7583
7584 // Constant area pointer.
7585 locations->SetInAt(1, Location::RequiresRegister());
7586
7587 // And the temporary we need.
7588 locations->AddTemp(Location::RequiresRegister());
7589}
7590
7591void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7592 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007593 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04007594 LocationSummary* locations = switch_instr->GetLocations();
7595 Register value_reg = locations->InAt(0).AsRegister<Register>();
7596 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7597
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007598 if (num_entries <= kPackedSwitchJumpTableThreshold) {
7599 GenPackedSwitchWithCompares(value_reg,
7600 lower_bound,
7601 num_entries,
7602 switch_instr->GetBlock(),
7603 default_block);
7604 return;
7605 }
7606
Mark Mendell805b3b52015-09-18 14:10:29 -04007607 // Optimizing has a jump area.
7608 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7609 Register constant_area = locations->InAt(1).AsRegister<Register>();
7610
7611 // Remove the bias, if needed.
7612 if (lower_bound != 0) {
7613 __ leal(temp_reg, Address(value_reg, -lower_bound));
7614 value_reg = temp_reg;
7615 }
7616
7617 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007618 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04007619 __ cmpl(value_reg, Immediate(num_entries - 1));
7620 __ j(kAbove, codegen_->GetLabelOf(default_block));
7621
7622 // We are in the range of the table.
7623 // Load (target-constant_area) from the jump table, indexing by the value.
7624 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
7625
7626 // Compute the actual target address by adding in constant_area.
7627 __ addl(temp_reg, constant_area);
7628
7629 // And jump.
7630 __ jmp(temp_reg);
7631}
7632
Mark Mendell0616ae02015-04-17 12:49:27 -04007633void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
7634 HX86ComputeBaseMethodAddress* insn) {
7635 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007636 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Mark Mendell0616ae02015-04-17 12:49:27 -04007637 locations->SetOut(Location::RequiresRegister());
7638}
7639
7640void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
7641 HX86ComputeBaseMethodAddress* insn) {
7642 LocationSummary* locations = insn->GetLocations();
7643 Register reg = locations->Out().AsRegister<Register>();
7644
7645 // Generate call to next instruction.
7646 Label next_instruction;
7647 __ call(&next_instruction);
7648 __ Bind(&next_instruction);
7649
7650 // Remember this offset for later use with constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007651 codegen_->AddMethodAddressOffset(insn, GetAssembler()->CodeSize());
Mark Mendell0616ae02015-04-17 12:49:27 -04007652
7653 // Grab the return address off the stack.
7654 __ popl(reg);
7655}
7656
7657void LocationsBuilderX86::VisitX86LoadFromConstantTable(
7658 HX86LoadFromConstantTable* insn) {
7659 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007660 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Mark Mendell0616ae02015-04-17 12:49:27 -04007661
7662 locations->SetInAt(0, Location::RequiresRegister());
7663 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
7664
7665 // If we don't need to be materialized, we only need the inputs to be set.
David Brazdilb3e773e2016-01-26 11:28:37 +00007666 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007667 return;
7668 }
7669
7670 switch (insn->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007671 case DataType::Type::kFloat32:
7672 case DataType::Type::kFloat64:
Mark Mendell0616ae02015-04-17 12:49:27 -04007673 locations->SetOut(Location::RequiresFpuRegister());
7674 break;
7675
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007676 case DataType::Type::kInt32:
Mark Mendell0616ae02015-04-17 12:49:27 -04007677 locations->SetOut(Location::RequiresRegister());
7678 break;
7679
7680 default:
7681 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7682 }
7683}
7684
7685void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
David Brazdilb3e773e2016-01-26 11:28:37 +00007686 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007687 return;
7688 }
7689
7690 LocationSummary* locations = insn->GetLocations();
7691 Location out = locations->Out();
7692 Register const_area = locations->InAt(0).AsRegister<Register>();
7693 HConstant *value = insn->GetConstant();
7694
7695 switch (insn->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007696 case DataType::Type::kFloat32:
Mark Mendell0616ae02015-04-17 12:49:27 -04007697 __ movss(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007698 codegen_->LiteralFloatAddress(
7699 value->AsFloatConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04007700 break;
7701
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007702 case DataType::Type::kFloat64:
Mark Mendell0616ae02015-04-17 12:49:27 -04007703 __ movsd(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007704 codegen_->LiteralDoubleAddress(
7705 value->AsDoubleConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04007706 break;
7707
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007708 case DataType::Type::kInt32:
Mark Mendell0616ae02015-04-17 12:49:27 -04007709 __ movl(out.AsRegister<Register>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007710 codegen_->LiteralInt32Address(
7711 value->AsIntConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04007712 break;
7713
7714 default:
7715 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7716 }
7717}
7718
Mark Mendell0616ae02015-04-17 12:49:27 -04007719/**
7720 * Class to handle late fixup of offsets into constant area.
7721 */
Vladimir Marko5233f932015-09-29 19:01:15 +01007722class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04007723 public:
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007724 RIPFixup(CodeGeneratorX86& codegen,
7725 HX86ComputeBaseMethodAddress* base_method_address,
7726 size_t offset)
7727 : codegen_(&codegen),
7728 base_method_address_(base_method_address),
7729 offset_into_constant_area_(offset) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04007730
7731 protected:
7732 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
7733
7734 CodeGeneratorX86* codegen_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007735 HX86ComputeBaseMethodAddress* base_method_address_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007736
7737 private:
7738 void Process(const MemoryRegion& region, int pos) OVERRIDE {
7739 // Patch the correct offset for the instruction. The place to patch is the
7740 // last 4 bytes of the instruction.
7741 // The value to patch is the distance from the offset in the constant area
7742 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04007743 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007744 int32_t relative_position =
7745 constant_offset - codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell0616ae02015-04-17 12:49:27 -04007746
7747 // Patch in the right value.
7748 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
7749 }
7750
Mark Mendell0616ae02015-04-17 12:49:27 -04007751 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04007752 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007753};
7754
Mark Mendell805b3b52015-09-18 14:10:29 -04007755/**
7756 * Class to handle late fixup of offsets to a jump table that will be created in the
7757 * constant area.
7758 */
7759class JumpTableRIPFixup : public RIPFixup {
7760 public:
7761 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007762 : RIPFixup(codegen, switch_instr->GetBaseMethodAddress(), static_cast<size_t>(-1)),
7763 switch_instr_(switch_instr) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04007764
7765 void CreateJumpTable() {
7766 X86Assembler* assembler = codegen_->GetAssembler();
7767
7768 // Ensure that the reference to the jump table has the correct offset.
7769 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
7770 SetOffset(offset_in_constant_table);
7771
7772 // The label values in the jump table are computed relative to the
7773 // instruction addressing the constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007774 const int32_t relative_offset = codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell805b3b52015-09-18 14:10:29 -04007775
7776 // Populate the jump table with the correct values for the jump table.
7777 int32_t num_entries = switch_instr_->GetNumEntries();
7778 HBasicBlock* block = switch_instr_->GetBlock();
7779 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
7780 // The value that we want is the target offset - the position of the table.
7781 for (int32_t i = 0; i < num_entries; i++) {
7782 HBasicBlock* b = successors[i];
7783 Label* l = codegen_->GetLabelOf(b);
7784 DCHECK(l->IsBound());
7785 int32_t offset_to_block = l->Position() - relative_offset;
7786 assembler->AppendInt32(offset_to_block);
7787 }
7788 }
7789
7790 private:
7791 const HX86PackedSwitch* switch_instr_;
7792};
7793
7794void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
7795 // Generate the constant area if needed.
7796 X86Assembler* assembler = GetAssembler();
7797 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
7798 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
7799 // byte values.
7800 assembler->Align(4, 0);
7801 constant_area_start_ = assembler->CodeSize();
7802
7803 // Populate any jump tables.
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007804 for (JumpTableRIPFixup* jump_table : fixups_to_jump_tables_) {
Mark Mendell805b3b52015-09-18 14:10:29 -04007805 jump_table->CreateJumpTable();
7806 }
7807
7808 // And now add the constant area to the generated code.
7809 assembler->AddConstantArea();
7810 }
7811
7812 // And finish up.
7813 CodeGenerator::Finalize(allocator);
7814}
7815
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007816Address CodeGeneratorX86::LiteralDoubleAddress(double v,
7817 HX86ComputeBaseMethodAddress* method_base,
7818 Register reg) {
7819 AssemblerFixup* fixup =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007820 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddDouble(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04007821 return Address(reg, kDummy32BitOffset, fixup);
7822}
7823
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007824Address CodeGeneratorX86::LiteralFloatAddress(float v,
7825 HX86ComputeBaseMethodAddress* method_base,
7826 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007827 AssemblerFixup* fixup =
7828 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddFloat(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04007829 return Address(reg, kDummy32BitOffset, fixup);
7830}
7831
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007832Address CodeGeneratorX86::LiteralInt32Address(int32_t v,
7833 HX86ComputeBaseMethodAddress* method_base,
7834 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007835 AssemblerFixup* fixup =
7836 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddInt32(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04007837 return Address(reg, kDummy32BitOffset, fixup);
7838}
7839
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007840Address CodeGeneratorX86::LiteralInt64Address(int64_t v,
7841 HX86ComputeBaseMethodAddress* method_base,
7842 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007843 AssemblerFixup* fixup =
7844 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddInt64(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04007845 return Address(reg, kDummy32BitOffset, fixup);
7846}
7847
Aart Bika19616e2016-02-01 18:57:58 -08007848void CodeGeneratorX86::Load32BitValue(Register dest, int32_t value) {
7849 if (value == 0) {
7850 __ xorl(dest, dest);
7851 } else {
7852 __ movl(dest, Immediate(value));
7853 }
7854}
7855
7856void CodeGeneratorX86::Compare32BitValue(Register dest, int32_t value) {
7857 if (value == 0) {
7858 __ testl(dest, dest);
7859 } else {
7860 __ cmpl(dest, Immediate(value));
7861 }
7862}
7863
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007864void CodeGeneratorX86::GenerateIntCompare(Location lhs, Location rhs) {
7865 Register lhs_reg = lhs.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07007866 GenerateIntCompare(lhs_reg, rhs);
7867}
7868
7869void CodeGeneratorX86::GenerateIntCompare(Register lhs, Location rhs) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007870 if (rhs.IsConstant()) {
7871 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
jessicahandojo4877b792016-09-08 19:49:13 -07007872 Compare32BitValue(lhs, value);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007873 } else if (rhs.IsStackSlot()) {
jessicahandojo4877b792016-09-08 19:49:13 -07007874 __ cmpl(lhs, Address(ESP, rhs.GetStackIndex()));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007875 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07007876 __ cmpl(lhs, rhs.AsRegister<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007877 }
7878}
7879
7880Address CodeGeneratorX86::ArrayAddress(Register obj,
7881 Location index,
7882 ScaleFactor scale,
7883 uint32_t data_offset) {
7884 return index.IsConstant() ?
7885 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << scale) + data_offset) :
7886 Address(obj, index.AsRegister<Register>(), scale, data_offset);
7887}
7888
Mark Mendell805b3b52015-09-18 14:10:29 -04007889Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
7890 Register reg,
7891 Register value) {
7892 // Create a fixup to be used to create and address the jump table.
7893 JumpTableRIPFixup* table_fixup =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007894 new (GetGraph()->GetAllocator()) JumpTableRIPFixup(*this, switch_instr);
Mark Mendell805b3b52015-09-18 14:10:29 -04007895
7896 // We have to populate the jump tables.
7897 fixups_to_jump_tables_.push_back(table_fixup);
7898
7899 // We want a scaled address, as we are extracting the correct offset from the table.
7900 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
7901}
7902
Andreas Gampe85b62f22015-09-09 13:15:38 -07007903// TODO: target as memory.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007904void CodeGeneratorX86::MoveFromReturnRegister(Location target, DataType::Type type) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07007905 if (!target.IsValid()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007906 DCHECK_EQ(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007907 return;
7908 }
7909
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007910 DCHECK_NE(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007911
7912 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
7913 if (target.Equals(return_loc)) {
7914 return;
7915 }
7916
7917 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7918 // with the else branch.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007919 if (type == DataType::Type::kInt64) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007920 HParallelMove parallel_move(GetGraph()->GetAllocator());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007921 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), DataType::Type::kInt32, nullptr);
7922 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), DataType::Type::kInt32, nullptr);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007923 GetMoveResolver()->EmitNativeCode(&parallel_move);
7924 } else {
7925 // Let the parallel move resolver take care of all of this.
Vladimir Markoca6fff82017-10-03 14:49:14 +01007926 HParallelMove parallel_move(GetGraph()->GetAllocator());
Andreas Gampe85b62f22015-09-09 13:15:38 -07007927 parallel_move.AddMove(return_loc, target, type, nullptr);
7928 GetMoveResolver()->EmitNativeCode(&parallel_move);
7929 }
7930}
7931
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007932void CodeGeneratorX86::PatchJitRootUse(uint8_t* code,
7933 const uint8_t* roots_data,
7934 const PatchInfo<Label>& info,
7935 uint64_t index_in_table) const {
7936 uint32_t code_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
7937 uintptr_t address =
7938 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
7939 typedef __attribute__((__aligned__(1))) uint32_t unaligned_uint32_t;
7940 reinterpret_cast<unaligned_uint32_t*>(code + code_offset)[0] =
7941 dchecked_integral_cast<uint32_t>(address);
7942}
7943
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007944void CodeGeneratorX86::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
7945 for (const PatchInfo<Label>& info : jit_string_patches_) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007946 StringReference string_reference(info.target_dex_file, dex::StringIndex(info.offset_or_index));
Vladimir Marko174b2e22017-10-12 13:34:49 +01007947 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007948 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007949 }
7950
7951 for (const PatchInfo<Label>& info : jit_class_patches_) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007952 TypeReference type_reference(info.target_dex_file, dex::TypeIndex(info.offset_or_index));
Vladimir Marko174b2e22017-10-12 13:34:49 +01007953 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007954 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007955 }
7956}
7957
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007958void LocationsBuilderX86::VisitIntermediateAddress(HIntermediateAddress* instruction
7959 ATTRIBUTE_UNUSED) {
7960 LOG(FATAL) << "Unreachable";
7961}
7962
7963void InstructionCodeGeneratorX86::VisitIntermediateAddress(HIntermediateAddress* instruction
7964 ATTRIBUTE_UNUSED) {
7965 LOG(FATAL) << "Unreachable";
7966}
7967
Roland Levillain4d027112015-07-01 15:41:14 +01007968#undef __
7969
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007970} // namespace x86
7971} // namespace art