blob: cbe9e0a35c246bd55a36e1fdd2f497914daa7613 [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)),
1031 string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1032 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 Geoffrayd97dc402015-01-22 13:50:01 +00001064 if (!skip_overflow_check) {
Vladimir Marko33bff252017-11-01 14:35:42 +00001065 size_t reserved_bytes = GetStackOverflowReservedBytes(InstructionSet::kX86);
1066 __ testl(EAX, Address(ESP, -static_cast<int32_t>(reserved_bytes)));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001067 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +01001068 }
1069
Mark Mendell5f874182015-03-04 15:42:45 -05001070 if (HasEmptyFrame()) {
1071 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001072 }
Mark Mendell5f874182015-03-04 15:42:45 -05001073
1074 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
1075 Register reg = kCoreCalleeSaves[i];
1076 if (allocated_registers_.ContainsCoreRegister(reg)) {
1077 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001078 __ cfi().AdjustCFAOffset(kX86WordSize);
1079 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -05001080 }
1081 }
1082
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001083 int adjust = GetFrameSize() - FrameEntrySpillSize();
1084 __ subl(ESP, Immediate(adjust));
1085 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001086 // Save the current method if we need it. Note that we do not
1087 // do this in HCurrentMethod, as the instruction might have been removed
1088 // in the SSA graph.
1089 if (RequiresCurrentMethod()) {
1090 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
1091 }
Nicolas Geoffrayf7893532017-06-15 12:34:36 +01001092
1093 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1094 // Initialize should_deoptimize flag to 0.
1095 __ movl(Address(ESP, GetStackOffsetOfShouldDeoptimizeFlag()), Immediate(0));
1096 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001097}
1098
1099void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001100 __ cfi().RememberState();
1101 if (!HasEmptyFrame()) {
1102 int adjust = GetFrameSize() - FrameEntrySpillSize();
1103 __ addl(ESP, Immediate(adjust));
1104 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -05001105
David Srbeckyc34dc932015-04-12 09:27:43 +01001106 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1107 Register reg = kCoreCalleeSaves[i];
1108 if (allocated_registers_.ContainsCoreRegister(reg)) {
1109 __ popl(reg);
1110 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
1111 __ cfi().Restore(DWARFReg(reg));
1112 }
Mark Mendell5f874182015-03-04 15:42:45 -05001113 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001114 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001115 __ ret();
1116 __ cfi().RestoreState();
1117 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001118}
1119
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001120void CodeGeneratorX86::Bind(HBasicBlock* block) {
1121 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001122}
1123
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001124Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(DataType::Type type) const {
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001125 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001126 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001127 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001128 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001129 case DataType::Type::kInt8:
1130 case DataType::Type::kUint16:
1131 case DataType::Type::kInt16:
1132 case DataType::Type::kInt32:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001133 return Location::RegisterLocation(EAX);
1134
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001135 case DataType::Type::kInt64:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001136 return Location::RegisterPairLocation(EAX, EDX);
1137
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001138 case DataType::Type::kVoid:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001139 return Location::NoLocation();
1140
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001141 case DataType::Type::kFloat64:
1142 case DataType::Type::kFloat32:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001143 return Location::FpuRegisterLocation(XMM0);
1144 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001145
1146 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001147}
1148
1149Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
1150 return Location::RegisterLocation(kMethodRegisterArgument);
1151}
1152
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001153Location InvokeDexCallingConventionVisitorX86::GetNextLocation(DataType::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001154 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001155 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001156 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001157 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001158 case DataType::Type::kInt8:
1159 case DataType::Type::kUint16:
1160 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001161 case DataType::Type::kInt32: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001162 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001163 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001164 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001165 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001166 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001167 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001168 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001169 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001170
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001171 case DataType::Type::kInt64: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001172 uint32_t index = gp_index_;
1173 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001174 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001175 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001176 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
1177 calling_convention.GetRegisterPairAt(index));
1178 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001179 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001180 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1181 }
1182 }
1183
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001184 case DataType::Type::kFloat32: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001185 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001186 stack_index_++;
1187 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1188 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1189 } else {
1190 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1191 }
1192 }
1193
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001194 case DataType::Type::kFloat64: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001195 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001196 stack_index_ += 2;
1197 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1198 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1199 } else {
1200 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001201 }
1202 }
1203
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001204 case DataType::Type::kVoid:
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001205 LOG(FATAL) << "Unexpected parameter type " << type;
1206 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001207 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00001208 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001209}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001210
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001211void CodeGeneratorX86::Move32(Location destination, Location source) {
1212 if (source.Equals(destination)) {
1213 return;
1214 }
1215 if (destination.IsRegister()) {
1216 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001217 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001218 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001219 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001220 } else {
1221 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001222 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001223 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001224 } else if (destination.IsFpuRegister()) {
1225 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001226 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001227 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001228 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001229 } else {
1230 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001231 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001232 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001233 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001234 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001235 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001236 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001237 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001238 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05001239 } else if (source.IsConstant()) {
1240 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001241 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05001242 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001243 } else {
1244 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001245 __ pushl(Address(ESP, source.GetStackIndex()));
1246 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001247 }
1248 }
1249}
1250
1251void CodeGeneratorX86::Move64(Location destination, Location source) {
1252 if (source.Equals(destination)) {
1253 return;
1254 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001255 if (destination.IsRegisterPair()) {
1256 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001257 EmitParallelMoves(
1258 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1259 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001260 DataType::Type::kInt32,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001261 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001262 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001263 DataType::Type::kInt32);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001264 } else if (source.IsFpuRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001265 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
1266 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
1267 __ psrlq(src_reg, Immediate(32));
1268 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001269 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001270 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001271 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001272 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
1273 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001274 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
1275 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001276 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05001277 if (source.IsFpuRegister()) {
1278 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
1279 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001280 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001281 } else if (source.IsRegisterPair()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001282 size_t elem_size = DataType::Size(DataType::Type::kInt32);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001283 // Create stack space for 2 elements.
1284 __ subl(ESP, Immediate(2 * elem_size));
1285 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
1286 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
1287 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
1288 // And remove the temporary stack space we allocated.
1289 __ addl(ESP, Immediate(2 * elem_size));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001290 } else {
1291 LOG(FATAL) << "Unimplemented";
1292 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001293 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001294 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001295 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001296 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001297 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001298 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001299 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001300 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001301 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001302 } else if (source.IsConstant()) {
1303 HConstant* constant = source.GetConstant();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001304 DCHECK(constant->IsLongConstant() || constant->IsDoubleConstant());
1305 int64_t value = GetInt64ValueOf(constant);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001306 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001307 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
1308 Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001309 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001310 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001311 EmitParallelMoves(
1312 Location::StackSlot(source.GetStackIndex()),
1313 Location::StackSlot(destination.GetStackIndex()),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001314 DataType::Type::kInt32,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001315 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001316 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001317 DataType::Type::kInt32);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001318 }
1319 }
1320}
1321
Calin Juravle175dc732015-08-25 15:42:32 +01001322void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
1323 DCHECK(location.IsRegister());
1324 __ movl(location.AsRegister<Register>(), Immediate(value));
1325}
1326
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001327void CodeGeneratorX86::MoveLocation(Location dst, Location src, DataType::Type dst_type) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001328 HParallelMove move(GetGraph()->GetAllocator());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001329 if (dst_type == DataType::Type::kInt64 && !src.IsConstant() && !src.IsFpuRegister()) {
1330 move.AddMove(src.ToLow(), dst.ToLow(), DataType::Type::kInt32, nullptr);
1331 move.AddMove(src.ToHigh(), dst.ToHigh(), DataType::Type::kInt32, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001332 } else {
David Brazdil74eb1b22015-12-14 11:44:01 +00001333 move.AddMove(src, dst, dst_type, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001334 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001335 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001336}
1337
1338void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1339 if (location.IsRegister()) {
1340 locations->AddTemp(location);
1341 } else if (location.IsRegisterPair()) {
1342 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1343 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1344 } else {
1345 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1346 }
1347}
1348
David Brazdilfc6a86a2015-06-26 10:33:45 +00001349void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08001350 if (successor->IsExitBlock()) {
1351 DCHECK(got->GetPrevious()->AlwaysThrows());
1352 return; // no code needed
1353 }
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001354
1355 HBasicBlock* block = got->GetBlock();
1356 HInstruction* previous = got->GetPrevious();
1357
1358 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001359 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001360 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1361 return;
1362 }
1363
1364 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1365 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1366 }
1367 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001368 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001369 }
1370}
1371
David Brazdilfc6a86a2015-06-26 10:33:45 +00001372void LocationsBuilderX86::VisitGoto(HGoto* got) {
1373 got->SetLocations(nullptr);
1374}
1375
1376void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1377 HandleGoto(got, got->GetSuccessor());
1378}
1379
1380void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1381 try_boundary->SetLocations(nullptr);
1382}
1383
1384void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1385 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1386 if (!successor->IsExitBlock()) {
1387 HandleGoto(try_boundary, successor);
1388 }
1389}
1390
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001391void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001392 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001393}
1394
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001395void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001396}
1397
Mark Mendell152408f2015-12-31 12:28:50 -05001398template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001399void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001400 LabelType* true_label,
1401 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001402 if (cond->IsFPConditionTrueIfNaN()) {
1403 __ j(kUnordered, true_label);
1404 } else if (cond->IsFPConditionFalseIfNaN()) {
1405 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001406 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001407 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001408}
1409
Mark Mendell152408f2015-12-31 12:28:50 -05001410template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001411void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001412 LabelType* true_label,
1413 LabelType* false_label) {
Mark Mendellc4701932015-04-10 13:18:51 -04001414 LocationSummary* locations = cond->GetLocations();
1415 Location left = locations->InAt(0);
1416 Location right = locations->InAt(1);
1417 IfCondition if_cond = cond->GetCondition();
1418
Mark Mendellc4701932015-04-10 13:18:51 -04001419 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001420 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001421 IfCondition true_high_cond = if_cond;
1422 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001423 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001424
1425 // Set the conditions for the test, remembering that == needs to be
1426 // decided using the low words.
1427 switch (if_cond) {
1428 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001429 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001430 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001431 break;
1432 case kCondLT:
1433 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001434 break;
1435 case kCondLE:
1436 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001437 break;
1438 case kCondGT:
1439 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001440 break;
1441 case kCondGE:
1442 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001443 break;
Aart Bike9f37602015-10-09 11:15:55 -07001444 case kCondB:
1445 false_high_cond = kCondA;
1446 break;
1447 case kCondBE:
1448 true_high_cond = kCondB;
1449 break;
1450 case kCondA:
1451 false_high_cond = kCondB;
1452 break;
1453 case kCondAE:
1454 true_high_cond = kCondA;
1455 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001456 }
1457
1458 if (right.IsConstant()) {
1459 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001460 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001461 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001462
Aart Bika19616e2016-02-01 18:57:58 -08001463 codegen_->Compare32BitValue(left_high, val_high);
Mark Mendellc4701932015-04-10 13:18:51 -04001464 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001465 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001466 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001467 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001468 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001469 __ j(X86Condition(true_high_cond), true_label);
1470 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001471 }
1472 // Must be equal high, so compare the lows.
Aart Bika19616e2016-02-01 18:57:58 -08001473 codegen_->Compare32BitValue(left_low, val_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001474 } else if (right.IsRegisterPair()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001475 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001476 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001477
1478 __ cmpl(left_high, right_high);
1479 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.
1488 __ cmpl(left_low, right_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001489 } else {
1490 DCHECK(right.IsDoubleStackSlot());
1491 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
1492 if (if_cond == kCondNE) {
1493 __ j(X86Condition(true_high_cond), true_label);
1494 } else if (if_cond == kCondEQ) {
1495 __ j(X86Condition(false_high_cond), false_label);
1496 } else {
1497 __ j(X86Condition(true_high_cond), true_label);
1498 __ j(X86Condition(false_high_cond), false_label);
1499 }
1500 // Must be equal high, so compare the lows.
1501 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Mark Mendellc4701932015-04-10 13:18:51 -04001502 }
1503 // The last comparison might be unsigned.
1504 __ j(final_condition, true_label);
1505}
1506
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001507void InstructionCodeGeneratorX86::GenerateFPCompare(Location lhs,
1508 Location rhs,
1509 HInstruction* insn,
1510 bool is_double) {
1511 HX86LoadFromConstantTable* const_area = insn->InputAt(1)->AsX86LoadFromConstantTable();
1512 if (is_double) {
1513 if (rhs.IsFpuRegister()) {
1514 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1515 } else if (const_area != nullptr) {
1516 DCHECK(const_area->IsEmittedAtUseSite());
1517 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(),
1518 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001519 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
1520 const_area->GetBaseMethodAddress(),
1521 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001522 } else {
1523 DCHECK(rhs.IsDoubleStackSlot());
1524 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1525 }
1526 } else {
1527 if (rhs.IsFpuRegister()) {
1528 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1529 } else if (const_area != nullptr) {
1530 DCHECK(const_area->IsEmittedAtUseSite());
1531 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(),
1532 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001533 const_area->GetConstant()->AsFloatConstant()->GetValue(),
1534 const_area->GetBaseMethodAddress(),
1535 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001536 } else {
1537 DCHECK(rhs.IsStackSlot());
1538 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1539 }
1540 }
1541}
1542
Mark Mendell152408f2015-12-31 12:28:50 -05001543template<class LabelType>
David Brazdil0debae72015-11-12 18:37:00 +00001544void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendell152408f2015-12-31 12:28:50 -05001545 LabelType* true_target_in,
1546 LabelType* false_target_in) {
David Brazdil0debae72015-11-12 18:37:00 +00001547 // Generated branching requires both targets to be explicit. If either of the
1548 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Mark Mendell152408f2015-12-31 12:28:50 -05001549 LabelType fallthrough_target;
1550 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1551 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
David Brazdil0debae72015-11-12 18:37:00 +00001552
Mark Mendellc4701932015-04-10 13:18:51 -04001553 LocationSummary* locations = condition->GetLocations();
1554 Location left = locations->InAt(0);
1555 Location right = locations->InAt(1);
1556
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001557 DataType::Type type = condition->InputAt(0)->GetType();
Mark Mendellc4701932015-04-10 13:18:51 -04001558 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001559 case DataType::Type::kInt64:
Mark Mendellc4701932015-04-10 13:18:51 -04001560 GenerateLongComparesAndJumps(condition, true_target, false_target);
1561 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001562 case DataType::Type::kFloat32:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001563 GenerateFPCompare(left, right, condition, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001564 GenerateFPJumps(condition, true_target, false_target);
1565 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001566 case DataType::Type::kFloat64:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001567 GenerateFPCompare(left, right, condition, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001568 GenerateFPJumps(condition, true_target, false_target);
1569 break;
1570 default:
1571 LOG(FATAL) << "Unexpected compare type " << type;
1572 }
1573
David Brazdil0debae72015-11-12 18:37:00 +00001574 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001575 __ jmp(false_target);
1576 }
David Brazdil0debae72015-11-12 18:37:00 +00001577
1578 if (fallthrough_target.IsLinked()) {
1579 __ Bind(&fallthrough_target);
1580 }
Mark Mendellc4701932015-04-10 13:18:51 -04001581}
1582
David Brazdil0debae72015-11-12 18:37:00 +00001583static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1584 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1585 // are set only strictly before `branch`. We can't use the eflags on long/FP
1586 // conditions if they are materialized due to the complex branching.
1587 return cond->IsCondition() &&
1588 cond->GetNext() == branch &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001589 cond->InputAt(0)->GetType() != DataType::Type::kInt64 &&
1590 !DataType::IsFloatingPointType(cond->InputAt(0)->GetType());
David Brazdil0debae72015-11-12 18:37:00 +00001591}
1592
Mark Mendell152408f2015-12-31 12:28:50 -05001593template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001594void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001595 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001596 LabelType* true_target,
1597 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001598 HInstruction* cond = instruction->InputAt(condition_input_index);
1599
1600 if (true_target == nullptr && false_target == nullptr) {
1601 // Nothing to do. The code always falls through.
1602 return;
1603 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001604 // Constant condition, statically compared against "true" (integer value 1).
1605 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001606 if (true_target != nullptr) {
1607 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001608 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001609 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001610 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001611 if (false_target != nullptr) {
1612 __ jmp(false_target);
1613 }
1614 }
1615 return;
1616 }
1617
1618 // The following code generates these patterns:
1619 // (1) true_target == nullptr && false_target != nullptr
1620 // - opposite condition true => branch to false_target
1621 // (2) true_target != nullptr && false_target == nullptr
1622 // - condition true => branch to true_target
1623 // (3) true_target != nullptr && false_target != nullptr
1624 // - condition true => branch to true_target
1625 // - branch to false_target
1626 if (IsBooleanValueOrMaterializedCondition(cond)) {
1627 if (AreEflagsSetFrom(cond, instruction)) {
1628 if (true_target == nullptr) {
1629 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
1630 } else {
1631 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
1632 }
1633 } else {
1634 // Materialized condition, compare against 0.
1635 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1636 if (lhs.IsRegister()) {
1637 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1638 } else {
1639 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1640 }
1641 if (true_target == nullptr) {
1642 __ j(kEqual, false_target);
1643 } else {
1644 __ j(kNotEqual, true_target);
1645 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001646 }
1647 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001648 // Condition has not been materialized, use its inputs as the comparison and
1649 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001650 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001651
1652 // If this is a long or FP comparison that has been folded into
1653 // the HCondition, generate the comparison directly.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001654 DataType::Type type = condition->InputAt(0)->GetType();
1655 if (type == DataType::Type::kInt64 || DataType::IsFloatingPointType(type)) {
David Brazdil0debae72015-11-12 18:37:00 +00001656 GenerateCompareTestAndBranch(condition, true_target, false_target);
1657 return;
1658 }
1659
1660 Location lhs = condition->GetLocations()->InAt(0);
1661 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001662 // LHS is guaranteed to be in a register (see LocationsBuilderX86::HandleCondition).
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001663 codegen_->GenerateIntCompare(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00001664 if (true_target == nullptr) {
1665 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1666 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001667 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001668 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001669 }
David Brazdil0debae72015-11-12 18:37:00 +00001670
1671 // If neither branch falls through (case 3), the conditional branch to `true_target`
1672 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1673 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001674 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001675 }
1676}
1677
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001678void LocationsBuilderX86::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001679 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00001680 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001681 locations->SetInAt(0, Location::Any());
1682 }
1683}
1684
1685void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001686 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1687 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1688 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1689 nullptr : codegen_->GetLabelOf(true_successor);
1690 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1691 nullptr : codegen_->GetLabelOf(false_successor);
1692 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001693}
1694
1695void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001696 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001697 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01001698 InvokeRuntimeCallingConvention calling_convention;
1699 RegisterSet caller_saves = RegisterSet::Empty();
1700 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1701 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00001702 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001703 locations->SetInAt(0, Location::Any());
1704 }
1705}
1706
1707void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001708 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001709 GenerateTestAndBranch<Label>(deoptimize,
1710 /* condition_input_index */ 0,
1711 slow_path->GetEntryLabel(),
1712 /* false_target */ nullptr);
1713}
1714
Mingyao Yang063fc772016-08-02 11:02:54 -07001715void LocationsBuilderX86::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001716 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yang063fc772016-08-02 11:02:54 -07001717 LocationSummary(flag, LocationSummary::kNoCall);
1718 locations->SetOut(Location::RequiresRegister());
1719}
1720
1721void InstructionCodeGeneratorX86::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
1722 __ movl(flag->GetLocations()->Out().AsRegister<Register>(),
1723 Address(ESP, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
1724}
1725
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001726static bool SelectCanUseCMOV(HSelect* select) {
1727 // There are no conditional move instructions for XMMs.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001728 if (DataType::IsFloatingPointType(select->GetType())) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001729 return false;
1730 }
1731
1732 // A FP condition doesn't generate the single CC that we need.
1733 // In 32 bit mode, a long condition doesn't generate a single CC either.
1734 HInstruction* condition = select->GetCondition();
1735 if (condition->IsCondition()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001736 DataType::Type compare_type = condition->InputAt(0)->GetType();
1737 if (compare_type == DataType::Type::kInt64 ||
1738 DataType::IsFloatingPointType(compare_type)) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001739 return false;
1740 }
1741 }
1742
1743 // We can generate a CMOV for this Select.
1744 return true;
1745}
1746
David Brazdil74eb1b22015-12-14 11:44:01 +00001747void LocationsBuilderX86::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001748 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001749 if (DataType::IsFloatingPointType(select->GetType())) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001750 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001751 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001752 } else {
1753 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001754 if (SelectCanUseCMOV(select)) {
1755 if (select->InputAt(1)->IsConstant()) {
1756 // Cmov can't handle a constant value.
1757 locations->SetInAt(1, Location::RequiresRegister());
1758 } else {
1759 locations->SetInAt(1, Location::Any());
1760 }
1761 } else {
1762 locations->SetInAt(1, Location::Any());
1763 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001764 }
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001765 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1766 locations->SetInAt(2, Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00001767 }
1768 locations->SetOut(Location::SameAsFirstInput());
1769}
1770
1771void InstructionCodeGeneratorX86::VisitSelect(HSelect* select) {
1772 LocationSummary* locations = select->GetLocations();
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001773 DCHECK(locations->InAt(0).Equals(locations->Out()));
1774 if (SelectCanUseCMOV(select)) {
1775 // If both the condition and the source types are integer, we can generate
1776 // a CMOV to implement Select.
1777
1778 HInstruction* select_condition = select->GetCondition();
1779 Condition cond = kNotEqual;
1780
1781 // Figure out how to test the 'condition'.
1782 if (select_condition->IsCondition()) {
1783 HCondition* condition = select_condition->AsCondition();
1784 if (!condition->IsEmittedAtUseSite()) {
1785 // This was a previously materialized condition.
1786 // Can we use the existing condition code?
1787 if (AreEflagsSetFrom(condition, select)) {
1788 // Materialization was the previous instruction. Condition codes are right.
1789 cond = X86Condition(condition->GetCondition());
1790 } else {
1791 // No, we have to recreate the condition code.
1792 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1793 __ testl(cond_reg, cond_reg);
1794 }
1795 } else {
1796 // We can't handle FP or long here.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001797 DCHECK_NE(condition->InputAt(0)->GetType(), DataType::Type::kInt64);
1798 DCHECK(!DataType::IsFloatingPointType(condition->InputAt(0)->GetType()));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001799 LocationSummary* cond_locations = condition->GetLocations();
Roland Levillain0b671c02016-08-19 12:02:34 +01001800 codegen_->GenerateIntCompare(cond_locations->InAt(0), cond_locations->InAt(1));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001801 cond = X86Condition(condition->GetCondition());
1802 }
1803 } else {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01001804 // Must be a Boolean condition, which needs to be compared to 0.
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001805 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1806 __ testl(cond_reg, cond_reg);
1807 }
1808
1809 // If the condition is true, overwrite the output, which already contains false.
1810 Location false_loc = locations->InAt(0);
1811 Location true_loc = locations->InAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001812 if (select->GetType() == DataType::Type::kInt64) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001813 // 64 bit conditional move.
1814 Register false_high = false_loc.AsRegisterPairHigh<Register>();
1815 Register false_low = false_loc.AsRegisterPairLow<Register>();
1816 if (true_loc.IsRegisterPair()) {
1817 __ cmovl(cond, false_high, true_loc.AsRegisterPairHigh<Register>());
1818 __ cmovl(cond, false_low, true_loc.AsRegisterPairLow<Register>());
1819 } else {
1820 __ cmovl(cond, false_high, Address(ESP, true_loc.GetHighStackIndex(kX86WordSize)));
1821 __ cmovl(cond, false_low, Address(ESP, true_loc.GetStackIndex()));
1822 }
1823 } else {
1824 // 32 bit conditional move.
1825 Register false_reg = false_loc.AsRegister<Register>();
1826 if (true_loc.IsRegister()) {
1827 __ cmovl(cond, false_reg, true_loc.AsRegister<Register>());
1828 } else {
1829 __ cmovl(cond, false_reg, Address(ESP, true_loc.GetStackIndex()));
1830 }
1831 }
1832 } else {
1833 NearLabel false_target;
1834 GenerateTestAndBranch<NearLabel>(
1835 select, /* condition_input_index */ 2, /* true_target */ nullptr, &false_target);
1836 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1837 __ Bind(&false_target);
1838 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001839}
1840
David Srbecky0cf44932015-12-09 14:09:59 +00001841void LocationsBuilderX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001842 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00001843}
1844
David Srbeckyd28f4a02016-03-14 17:14:24 +00001845void InstructionCodeGeneratorX86::VisitNativeDebugInfo(HNativeDebugInfo*) {
1846 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001847}
1848
1849void CodeGeneratorX86::GenerateNop() {
1850 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001851}
1852
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001853void LocationsBuilderX86::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001854 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01001855 new (GetGraph()->GetAllocator()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001856 // Handle the long/FP comparisons made in instruction simplification.
1857 switch (cond->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001858 case DataType::Type::kInt64: {
Mark Mendellc4701932015-04-10 13:18:51 -04001859 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell8659e842016-02-16 10:41:46 -05001860 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001861 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001862 locations->SetOut(Location::RequiresRegister());
1863 }
1864 break;
1865 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001866 case DataType::Type::kFloat32:
1867 case DataType::Type::kFloat64: {
Mark Mendellc4701932015-04-10 13:18:51 -04001868 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001869 if (cond->InputAt(1)->IsX86LoadFromConstantTable()) {
1870 DCHECK(cond->InputAt(1)->IsEmittedAtUseSite());
1871 } else if (cond->InputAt(1)->IsConstant()) {
1872 locations->SetInAt(1, Location::RequiresFpuRegister());
1873 } else {
1874 locations->SetInAt(1, Location::Any());
1875 }
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 }
1881 default:
1882 locations->SetInAt(0, Location::RequiresRegister());
1883 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001884 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001885 // We need a byte register.
1886 locations->SetOut(Location::RegisterLocation(ECX));
1887 }
1888 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001889 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001890}
1891
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001892void InstructionCodeGeneratorX86::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001893 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001894 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001895 }
Mark Mendellc4701932015-04-10 13:18:51 -04001896
1897 LocationSummary* locations = cond->GetLocations();
1898 Location lhs = locations->InAt(0);
1899 Location rhs = locations->InAt(1);
1900 Register reg = locations->Out().AsRegister<Register>();
Mark Mendell152408f2015-12-31 12:28:50 -05001901 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001902
1903 switch (cond->InputAt(0)->GetType()) {
1904 default: {
1905 // Integer case.
1906
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01001907 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04001908 __ xorl(reg, reg);
Roland Levillain0b671c02016-08-19 12:02:34 +01001909 codegen_->GenerateIntCompare(lhs, rhs);
Aart Bike9f37602015-10-09 11:15:55 -07001910 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001911 return;
1912 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001913 case DataType::Type::kInt64:
Mark Mendellc4701932015-04-10 13:18:51 -04001914 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1915 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001916 case DataType::Type::kFloat32:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001917 GenerateFPCompare(lhs, rhs, cond, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001918 GenerateFPJumps(cond, &true_label, &false_label);
1919 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001920 case DataType::Type::kFloat64:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001921 GenerateFPCompare(lhs, rhs, cond, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001922 GenerateFPJumps(cond, &true_label, &false_label);
1923 break;
1924 }
1925
1926 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001927 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001928
Roland Levillain4fa13f62015-07-06 18:11:54 +01001929 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001930 __ Bind(&false_label);
1931 __ xorl(reg, reg);
1932 __ jmp(&done_label);
1933
Roland Levillain4fa13f62015-07-06 18:11:54 +01001934 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001935 __ Bind(&true_label);
1936 __ movl(reg, Immediate(1));
1937 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001938}
1939
1940void LocationsBuilderX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001941 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001942}
1943
1944void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001945 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001946}
1947
1948void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001949 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001950}
1951
1952void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001953 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001954}
1955
1956void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001957 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001958}
1959
1960void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001961 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001962}
1963
1964void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001965 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001966}
1967
1968void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001969 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001970}
1971
1972void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001973 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001974}
1975
1976void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001977 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001978}
1979
1980void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001981 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001982}
1983
1984void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001985 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001986}
1987
Aart Bike9f37602015-10-09 11:15:55 -07001988void LocationsBuilderX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001989 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001990}
1991
1992void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001993 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001994}
1995
1996void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001997 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001998}
1999
2000void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002001 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002002}
2003
2004void LocationsBuilderX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002005 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002006}
2007
2008void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002009 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002010}
2011
2012void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002013 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002014}
2015
2016void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002017 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002018}
2019
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002020void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002021 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002022 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002023 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002024}
2025
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002026void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002027 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002028}
2029
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002030void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
2031 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002032 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002033 locations->SetOut(Location::ConstantLocation(constant));
2034}
2035
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002036void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002037 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002038}
2039
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002040void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002041 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002042 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002043 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002044}
2045
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002046void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002047 // Will be generated at use site.
2048}
2049
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002050void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
2051 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002052 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002053 locations->SetOut(Location::ConstantLocation(constant));
2054}
2055
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002056void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002057 // Will be generated at use site.
2058}
2059
2060void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
2061 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002062 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002063 locations->SetOut(Location::ConstantLocation(constant));
2064}
2065
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002066void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002067 // Will be generated at use site.
2068}
2069
Igor Murashkind01745e2017-04-05 16:40:31 -07002070void LocationsBuilderX86::VisitConstructorFence(HConstructorFence* constructor_fence) {
2071 constructor_fence->SetLocations(nullptr);
2072}
2073
2074void InstructionCodeGeneratorX86::VisitConstructorFence(
2075 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
2076 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
2077}
2078
Calin Juravle27df7582015-04-17 19:12:31 +01002079void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2080 memory_barrier->SetLocations(nullptr);
2081}
2082
2083void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00002084 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002085}
2086
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002087void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002088 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002089}
2090
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002091void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002092 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002093}
2094
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002095void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002096 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002097 new (GetGraph()->GetAllocator()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002098 switch (ret->InputAt(0)->GetType()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002099 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002100 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002101 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002102 case DataType::Type::kInt8:
2103 case DataType::Type::kUint16:
2104 case DataType::Type::kInt16:
2105 case DataType::Type::kInt32:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002106 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002107 break;
2108
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002109 case DataType::Type::kInt64:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002110 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002111 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002112 break;
2113
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002114 case DataType::Type::kFloat32:
2115 case DataType::Type::kFloat64:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002116 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002117 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002118 break;
2119
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002120 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002121 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002122 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002123}
2124
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002125void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002126 if (kIsDebugBuild) {
2127 switch (ret->InputAt(0)->GetType()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002128 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002129 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002130 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002131 case DataType::Type::kInt8:
2132 case DataType::Type::kUint16:
2133 case DataType::Type::kInt16:
2134 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002135 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002136 break;
2137
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002138 case DataType::Type::kInt64:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002139 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
2140 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002141 break;
2142
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002143 case DataType::Type::kFloat32:
2144 case DataType::Type::kFloat64:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002145 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002146 break;
2147
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002148 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002149 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002150 }
2151 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002152 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002153}
2154
Calin Juravle175dc732015-08-25 15:42:32 +01002155void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2156 // The trampoline uses the same calling convention as dex calling conventions,
2157 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2158 // the method_idx.
2159 HandleInvoke(invoke);
2160}
2161
2162void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2163 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2164}
2165
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002166void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002167 // Explicit clinit checks triggered by static invokes must have been pruned by
2168 // art::PrepareForRegisterAllocation.
2169 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002170
Mark Mendellfb8d2792015-03-31 22:16:59 -04002171 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002172 if (intrinsic.TryDispatch(invoke)) {
Vladimir Marko65979462017-05-19 17:25:12 +01002173 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeMethodLoadKind()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00002174 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002175 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04002176 return;
2177 }
2178
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002179 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002180
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002181 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
Vladimir Marko65979462017-05-19 17:25:12 +01002182 if (invoke->HasPcRelativeMethodLoadKind()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00002183 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002184 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002185}
2186
Mark Mendell09ed1a32015-03-25 08:30:06 -04002187static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
2188 if (invoke->GetLocations()->Intrinsified()) {
2189 IntrinsicCodeGeneratorX86 intrinsic(codegen);
2190 intrinsic.Dispatch(invoke);
2191 return true;
2192 }
2193 return false;
2194}
2195
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002196void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002197 // Explicit clinit checks triggered by static invokes must have been pruned by
2198 // art::PrepareForRegisterAllocation.
2199 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002200
Mark Mendell09ed1a32015-03-25 08:30:06 -04002201 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2202 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002203 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002204
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002205 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04002206 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002207 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002208}
2209
2210void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00002211 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
2212 if (intrinsic.TryDispatch(invoke)) {
2213 return;
2214 }
2215
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002216 HandleInvoke(invoke);
2217}
2218
2219void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002220 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002221 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002222}
2223
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002224void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002225 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2226 return;
2227 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002228
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002229 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002230 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002231}
2232
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002233void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002234 // This call to HandleInvoke allocates a temporary (core) register
2235 // which is also used to transfer the hidden argument from FP to
2236 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002237 HandleInvoke(invoke);
2238 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002239 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002240}
2241
2242void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
2243 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002244 LocationSummary* locations = invoke->GetLocations();
2245 Register temp = locations->GetTemp(0).AsRegister<Register>();
2246 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002247 Location receiver = locations->InAt(0);
2248 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2249
Roland Levillain0d5a2812015-11-13 10:07:31 +00002250 // Set the hidden argument. This is safe to do this here, as XMM7
2251 // won't be modified thereafter, before the `call` instruction.
2252 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002253 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002254 __ movd(hidden_reg, temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002255
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002256 if (receiver.IsStackSlot()) {
2257 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002258 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002259 __ movl(temp, Address(temp, class_offset));
2260 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002261 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002262 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002263 }
Roland Levillain4d027112015-07-01 15:41:14 +01002264 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002265 // Instead of simply (possibly) unpoisoning `temp` here, we should
2266 // emit a read barrier for the previous class reference load.
2267 // However this is not required in practice, as this is an
2268 // intermediate/temporary reference and because the current
2269 // concurrent copying collector keeps the from-space memory
2270 // intact/accessible until the end of the marking phase (the
2271 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002272 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002273 // temp = temp->GetAddressOfIMT()
2274 __ movl(temp,
2275 Address(temp, mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002276 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002277 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002278 invoke->GetImtIndex(), kX86PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002279 __ movl(temp, Address(temp, method_offset));
2280 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002281 __ call(Address(temp,
Andreas Gampe542451c2016-07-26 09:02:02 -07002282 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002283
2284 DCHECK(!codegen_->IsLeafMethod());
2285 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2286}
2287
Orion Hodsonac141392017-01-13 11:53:47 +00002288void LocationsBuilderX86::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2289 HandleInvoke(invoke);
2290}
2291
2292void InstructionCodeGeneratorX86::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2293 codegen_->GenerateInvokePolymorphicCall(invoke);
2294}
2295
Roland Levillain88cb1752014-10-20 16:36:47 +01002296void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2297 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002298 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Roland Levillain88cb1752014-10-20 16:36:47 +01002299 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002300 case DataType::Type::kInt32:
2301 case DataType::Type::kInt64:
Roland Levillain88cb1752014-10-20 16:36:47 +01002302 locations->SetInAt(0, Location::RequiresRegister());
2303 locations->SetOut(Location::SameAsFirstInput());
2304 break;
2305
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002306 case DataType::Type::kFloat32:
Roland Levillain5368c212014-11-27 15:03:41 +00002307 locations->SetInAt(0, Location::RequiresFpuRegister());
2308 locations->SetOut(Location::SameAsFirstInput());
2309 locations->AddTemp(Location::RequiresRegister());
2310 locations->AddTemp(Location::RequiresFpuRegister());
2311 break;
2312
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002313 case DataType::Type::kFloat64:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002314 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002315 locations->SetOut(Location::SameAsFirstInput());
2316 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002317 break;
2318
2319 default:
2320 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2321 }
2322}
2323
2324void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2325 LocationSummary* locations = neg->GetLocations();
2326 Location out = locations->Out();
2327 Location in = locations->InAt(0);
2328 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002329 case DataType::Type::kInt32:
Roland Levillain88cb1752014-10-20 16:36:47 +01002330 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002331 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002332 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002333 break;
2334
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002335 case DataType::Type::kInt64:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002336 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002337 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002338 __ negl(out.AsRegisterPairLow<Register>());
2339 // Negation is similar to subtraction from zero. The least
2340 // significant byte triggers a borrow when it is different from
2341 // zero; to take it into account, add 1 to the most significant
2342 // byte if the carry flag (CF) is set to 1 after the first NEGL
2343 // operation.
2344 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2345 __ negl(out.AsRegisterPairHigh<Register>());
2346 break;
2347
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002348 case DataType::Type::kFloat32: {
Roland Levillain5368c212014-11-27 15:03:41 +00002349 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002350 Register constant = locations->GetTemp(0).AsRegister<Register>();
2351 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002352 // Implement float negation with an exclusive or with value
2353 // 0x80000000 (mask for bit 31, representing the sign of a
2354 // single-precision floating-point number).
2355 __ movl(constant, Immediate(INT32_C(0x80000000)));
2356 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002357 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002358 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002359 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002360
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002361 case DataType::Type::kFloat64: {
Roland Levillain5368c212014-11-27 15:03:41 +00002362 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002363 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002364 // Implement double negation with an exclusive or with value
2365 // 0x8000000000000000 (mask for bit 63, representing the sign of
2366 // a double-precision floating-point number).
2367 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002368 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002369 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002370 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002371
2372 default:
2373 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2374 }
2375}
2376
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002377void LocationsBuilderX86::VisitX86FPNeg(HX86FPNeg* neg) {
2378 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002379 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002380 DCHECK(DataType::IsFloatingPointType(neg->GetType()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002381 locations->SetInAt(0, Location::RequiresFpuRegister());
2382 locations->SetInAt(1, Location::RequiresRegister());
2383 locations->SetOut(Location::SameAsFirstInput());
2384 locations->AddTemp(Location::RequiresFpuRegister());
2385}
2386
2387void InstructionCodeGeneratorX86::VisitX86FPNeg(HX86FPNeg* neg) {
2388 LocationSummary* locations = neg->GetLocations();
2389 Location out = locations->Out();
2390 DCHECK(locations->InAt(0).Equals(out));
2391
2392 Register constant_area = locations->InAt(1).AsRegister<Register>();
2393 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002394 if (neg->GetType() == DataType::Type::kFloat32) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00002395 __ movss(mask, codegen_->LiteralInt32Address(INT32_C(0x80000000),
2396 neg->GetBaseMethodAddress(),
2397 constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002398 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
2399 } else {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00002400 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000),
2401 neg->GetBaseMethodAddress(),
2402 constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002403 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
2404 }
2405}
2406
Roland Levillaindff1f282014-11-05 14:15:05 +00002407void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002408 DataType::Type result_type = conversion->GetResultType();
2409 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002410 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
2411 << input_type << " -> " << result_type;
Roland Levillain624279f2014-12-04 11:54:28 +00002412
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002413 // The float-to-long and double-to-long type conversions rely on a
2414 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002415 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002416 ((input_type == DataType::Type::kFloat32 || input_type == DataType::Type::kFloat64)
2417 && result_type == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002418 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002419 : LocationSummary::kNoCall;
2420 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002421 new (GetGraph()->GetAllocator()) LocationSummary(conversion, call_kind);
Roland Levillain624279f2014-12-04 11:54:28 +00002422
Roland Levillaindff1f282014-11-05 14:15:05 +00002423 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002424 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002425 case DataType::Type::kInt8:
Roland Levillain51d3fc42014-11-13 14:11:42 +00002426 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002427 case DataType::Type::kUint8:
2428 case DataType::Type::kInt8:
2429 case DataType::Type::kUint16:
2430 case DataType::Type::kInt16:
2431 case DataType::Type::kInt32:
2432 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2433 // Make the output overlap to please the register allocator. This greatly simplifies
2434 // the validation of the linear scan implementation
2435 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2436 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002437 case DataType::Type::kInt64: {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002438 HInstruction* input = conversion->InputAt(0);
2439 Location input_location = input->IsConstant()
2440 ? Location::ConstantLocation(input->AsConstant())
2441 : Location::RegisterPairLocation(EAX, EDX);
2442 locations->SetInAt(0, input_location);
2443 // Make the output overlap to please the register allocator. This greatly simplifies
2444 // the validation of the linear scan implementation
2445 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2446 break;
2447 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002448
2449 default:
2450 LOG(FATAL) << "Unexpected type conversion from " << input_type
2451 << " to " << result_type;
2452 }
2453 break;
2454
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002455 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002456 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002457 DCHECK(DataType::IsIntegralType(input_type)) << input_type;
2458 locations->SetInAt(0, Location::Any());
2459 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Roland Levillain01a8d712014-11-14 16:27:39 +00002460 break;
2461
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002462 case DataType::Type::kInt32:
Roland Levillain946e1432014-11-11 17:35:19 +00002463 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002464 case DataType::Type::kInt64:
Roland Levillain946e1432014-11-11 17:35:19 +00002465 locations->SetInAt(0, Location::Any());
2466 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2467 break;
2468
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002469 case DataType::Type::kFloat32:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002470 locations->SetInAt(0, Location::RequiresFpuRegister());
2471 locations->SetOut(Location::RequiresRegister());
2472 locations->AddTemp(Location::RequiresFpuRegister());
2473 break;
2474
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002475 case DataType::Type::kFloat64:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002476 locations->SetInAt(0, Location::RequiresFpuRegister());
2477 locations->SetOut(Location::RequiresRegister());
2478 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002479 break;
2480
2481 default:
2482 LOG(FATAL) << "Unexpected type conversion from " << input_type
2483 << " to " << result_type;
2484 }
2485 break;
2486
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002487 case DataType::Type::kInt64:
Roland Levillaindff1f282014-11-05 14:15:05 +00002488 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002489 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002490 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002491 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002492 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002493 case DataType::Type::kInt16:
2494 case DataType::Type::kInt32:
Roland Levillaindff1f282014-11-05 14:15:05 +00002495 locations->SetInAt(0, Location::RegisterLocation(EAX));
2496 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2497 break;
2498
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002499 case DataType::Type::kFloat32:
2500 case DataType::Type::kFloat64: {
Vladimir Marko949c91f2015-01-27 10:48:44 +00002501 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002502 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2503 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2504
Vladimir Marko949c91f2015-01-27 10:48:44 +00002505 // The runtime helper puts the result in EAX, EDX.
2506 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002507 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002508 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002509
2510 default:
2511 LOG(FATAL) << "Unexpected type conversion from " << input_type
2512 << " to " << result_type;
2513 }
2514 break;
2515
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002516 case DataType::Type::kFloat32:
Roland Levillaincff13742014-11-17 14:32:17 +00002517 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002518 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002519 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002520 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002521 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002522 case DataType::Type::kInt16:
2523 case DataType::Type::kInt32:
Roland Levillaincff13742014-11-17 14:32:17 +00002524 locations->SetInAt(0, Location::RequiresRegister());
2525 locations->SetOut(Location::RequiresFpuRegister());
2526 break;
2527
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002528 case DataType::Type::kInt64:
Roland Levillain232ade02015-04-20 15:14:36 +01002529 locations->SetInAt(0, Location::Any());
2530 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002531 break;
2532
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002533 case DataType::Type::kFloat64:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002534 locations->SetInAt(0, Location::RequiresFpuRegister());
2535 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002536 break;
2537
2538 default:
2539 LOG(FATAL) << "Unexpected type conversion from " << input_type
2540 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08002541 }
Roland Levillaincff13742014-11-17 14:32:17 +00002542 break;
2543
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002544 case DataType::Type::kFloat64:
Roland Levillaincff13742014-11-17 14:32:17 +00002545 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002546 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002547 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002548 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002549 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002550 case DataType::Type::kInt16:
2551 case DataType::Type::kInt32:
Roland Levillaincff13742014-11-17 14:32:17 +00002552 locations->SetInAt(0, Location::RequiresRegister());
2553 locations->SetOut(Location::RequiresFpuRegister());
2554 break;
2555
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002556 case DataType::Type::kInt64:
Roland Levillain232ade02015-04-20 15:14:36 +01002557 locations->SetInAt(0, Location::Any());
2558 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002559 break;
2560
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002561 case DataType::Type::kFloat32:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002562 locations->SetInAt(0, Location::RequiresFpuRegister());
2563 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002564 break;
2565
2566 default:
2567 LOG(FATAL) << "Unexpected type conversion from " << input_type
2568 << " to " << result_type;
2569 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002570 break;
2571
2572 default:
2573 LOG(FATAL) << "Unexpected type conversion from " << input_type
2574 << " to " << result_type;
2575 }
2576}
2577
2578void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2579 LocationSummary* locations = conversion->GetLocations();
2580 Location out = locations->Out();
2581 Location in = locations->InAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002582 DataType::Type result_type = conversion->GetResultType();
2583 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002584 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
2585 << input_type << " -> " << result_type;
Roland Levillaindff1f282014-11-05 14:15:05 +00002586 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002587 case DataType::Type::kUint8:
2588 switch (input_type) {
2589 case DataType::Type::kInt8:
2590 case DataType::Type::kUint16:
2591 case DataType::Type::kInt16:
2592 case DataType::Type::kInt32:
2593 if (in.IsRegister()) {
2594 __ movzxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
2595 } else {
2596 DCHECK(in.GetConstant()->IsIntConstant());
2597 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2598 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint8_t>(value)));
2599 }
2600 break;
2601 case DataType::Type::kInt64:
2602 if (in.IsRegisterPair()) {
2603 __ movzxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2604 } else {
2605 DCHECK(in.GetConstant()->IsLongConstant());
2606 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2607 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint8_t>(value)));
2608 }
2609 break;
2610
2611 default:
2612 LOG(FATAL) << "Unexpected type conversion from " << input_type
2613 << " to " << result_type;
2614 }
2615 break;
2616
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002617 case DataType::Type::kInt8:
Roland Levillain51d3fc42014-11-13 14:11:42 +00002618 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002619 case DataType::Type::kUint8:
2620 case DataType::Type::kUint16:
2621 case DataType::Type::kInt16:
2622 case DataType::Type::kInt32:
2623 if (in.IsRegister()) {
2624 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
2625 } else {
2626 DCHECK(in.GetConstant()->IsIntConstant());
2627 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2628 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2629 }
2630 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002631 case DataType::Type::kInt64:
Vladimir Markob52bbde2016-02-12 12:06:05 +00002632 if (in.IsRegisterPair()) {
2633 __ movsxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2634 } else {
2635 DCHECK(in.GetConstant()->IsLongConstant());
2636 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2637 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2638 }
2639 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002640
2641 default:
2642 LOG(FATAL) << "Unexpected type conversion from " << input_type
2643 << " to " << result_type;
2644 }
2645 break;
2646
2647 case DataType::Type::kUint16:
2648 switch (input_type) {
2649 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002650 case DataType::Type::kInt16:
2651 case DataType::Type::kInt32:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002652 if (in.IsRegister()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002653 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
2654 } else if (in.IsStackSlot()) {
2655 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002656 } else {
2657 DCHECK(in.GetConstant()->IsIntConstant());
2658 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002659 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
2660 }
2661 break;
2662 case DataType::Type::kInt64:
2663 if (in.IsRegisterPair()) {
2664 __ movzxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2665 } else if (in.IsDoubleStackSlot()) {
2666 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2667 } else {
2668 DCHECK(in.GetConstant()->IsLongConstant());
2669 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2670 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002671 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002672 break;
2673
2674 default:
2675 LOG(FATAL) << "Unexpected type conversion from " << input_type
2676 << " to " << result_type;
2677 }
2678 break;
2679
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002680 case DataType::Type::kInt16:
Roland Levillain01a8d712014-11-14 16:27:39 +00002681 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002682 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002683 case DataType::Type::kInt32:
Roland Levillain01a8d712014-11-14 16:27:39 +00002684 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002685 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002686 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002687 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002688 } else {
2689 DCHECK(in.GetConstant()->IsIntConstant());
2690 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002691 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002692 }
2693 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002694 case DataType::Type::kInt64:
2695 if (in.IsRegisterPair()) {
2696 __ movsxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2697 } else if (in.IsDoubleStackSlot()) {
2698 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2699 } else {
2700 DCHECK(in.GetConstant()->IsLongConstant());
2701 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2702 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
2703 }
2704 break;
Roland Levillain01a8d712014-11-14 16:27:39 +00002705
2706 default:
2707 LOG(FATAL) << "Unexpected type conversion from " << input_type
2708 << " to " << result_type;
2709 }
2710 break;
2711
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002712 case DataType::Type::kInt32:
Roland Levillain946e1432014-11-11 17:35:19 +00002713 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002714 case DataType::Type::kInt64:
Roland Levillain946e1432014-11-11 17:35:19 +00002715 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002716 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002717 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002718 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002719 } else {
2720 DCHECK(in.IsConstant());
2721 DCHECK(in.GetConstant()->IsLongConstant());
2722 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002723 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002724 }
2725 break;
2726
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002727 case DataType::Type::kFloat32: {
Roland Levillain3f8f9362014-12-02 17:45:01 +00002728 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2729 Register output = out.AsRegister<Register>();
2730 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002731 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002732
2733 __ movl(output, Immediate(kPrimIntMax));
2734 // temp = int-to-float(output)
2735 __ cvtsi2ss(temp, output);
2736 // if input >= temp goto done
2737 __ comiss(input, temp);
2738 __ j(kAboveEqual, &done);
2739 // if input == NaN goto nan
2740 __ j(kUnordered, &nan);
2741 // output = float-to-int-truncate(input)
2742 __ cvttss2si(output, input);
2743 __ jmp(&done);
2744 __ Bind(&nan);
2745 // output = 0
2746 __ xorl(output, output);
2747 __ Bind(&done);
2748 break;
2749 }
2750
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002751 case DataType::Type::kFloat64: {
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002752 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2753 Register output = out.AsRegister<Register>();
2754 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002755 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002756
2757 __ movl(output, Immediate(kPrimIntMax));
2758 // temp = int-to-double(output)
2759 __ cvtsi2sd(temp, output);
2760 // if input >= temp goto done
2761 __ comisd(input, temp);
2762 __ j(kAboveEqual, &done);
2763 // if input == NaN goto nan
2764 __ j(kUnordered, &nan);
2765 // output = double-to-int-truncate(input)
2766 __ cvttsd2si(output, input);
2767 __ jmp(&done);
2768 __ Bind(&nan);
2769 // output = 0
2770 __ xorl(output, output);
2771 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002772 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002773 }
Roland Levillain946e1432014-11-11 17:35:19 +00002774
2775 default:
2776 LOG(FATAL) << "Unexpected type conversion from " << input_type
2777 << " to " << result_type;
2778 }
2779 break;
2780
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002781 case DataType::Type::kInt64:
Roland Levillaindff1f282014-11-05 14:15:05 +00002782 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002783 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002784 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002785 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002786 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002787 case DataType::Type::kInt16:
2788 case DataType::Type::kInt32:
Roland Levillaindff1f282014-11-05 14:15:05 +00002789 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2790 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002791 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002792 __ cdq();
2793 break;
2794
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002795 case DataType::Type::kFloat32:
Serban Constantinescuba45db02016-07-12 22:53:02 +01002796 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002797 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002798 break;
2799
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002800 case DataType::Type::kFloat64:
Serban Constantinescuba45db02016-07-12 22:53:02 +01002801 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002802 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002803 break;
2804
2805 default:
2806 LOG(FATAL) << "Unexpected type conversion from " << input_type
2807 << " to " << result_type;
2808 }
2809 break;
2810
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002811 case DataType::Type::kFloat32:
Roland Levillaincff13742014-11-17 14:32:17 +00002812 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002813 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002814 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002815 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002816 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002817 case DataType::Type::kInt16:
2818 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002819 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002820 break;
2821
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002822 case DataType::Type::kInt64: {
Roland Levillain232ade02015-04-20 15:14:36 +01002823 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002824
Roland Levillain232ade02015-04-20 15:14:36 +01002825 // Create stack space for the call to
2826 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2827 // TODO: enhance register allocator to ask for stack temporaries.
2828 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002829 adjustment = DataType::Size(DataType::Type::kInt64);
Roland Levillain232ade02015-04-20 15:14:36 +01002830 __ subl(ESP, Immediate(adjustment));
2831 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002832
Roland Levillain232ade02015-04-20 15:14:36 +01002833 // Load the value to the FP stack, using temporaries if needed.
2834 PushOntoFPStack(in, 0, adjustment, false, true);
2835
2836 if (out.IsStackSlot()) {
2837 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2838 } else {
2839 __ fstps(Address(ESP, 0));
2840 Location stack_temp = Location::StackSlot(0);
2841 codegen_->Move32(out, stack_temp);
2842 }
2843
2844 // Remove the temporary stack space we allocated.
2845 if (adjustment != 0) {
2846 __ addl(ESP, Immediate(adjustment));
2847 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002848 break;
2849 }
2850
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002851 case DataType::Type::kFloat64:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002852 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002853 break;
2854
2855 default:
2856 LOG(FATAL) << "Unexpected type conversion from " << input_type
2857 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08002858 }
Roland Levillaincff13742014-11-17 14:32:17 +00002859 break;
2860
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002861 case DataType::Type::kFloat64:
Roland Levillaincff13742014-11-17 14:32:17 +00002862 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002863 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002864 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002865 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002866 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002867 case DataType::Type::kInt16:
2868 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002869 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002870 break;
2871
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002872 case DataType::Type::kInt64: {
Roland Levillain232ade02015-04-20 15:14:36 +01002873 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002874
Roland Levillain232ade02015-04-20 15:14:36 +01002875 // Create stack space for the call to
2876 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2877 // TODO: enhance register allocator to ask for stack temporaries.
2878 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002879 adjustment = DataType::Size(DataType::Type::kInt64);
Roland Levillain232ade02015-04-20 15:14:36 +01002880 __ subl(ESP, Immediate(adjustment));
2881 }
2882
2883 // Load the value to the FP stack, using temporaries if needed.
2884 PushOntoFPStack(in, 0, adjustment, false, true);
2885
2886 if (out.IsDoubleStackSlot()) {
2887 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2888 } else {
2889 __ fstpl(Address(ESP, 0));
2890 Location stack_temp = Location::DoubleStackSlot(0);
2891 codegen_->Move64(out, stack_temp);
2892 }
2893
2894 // Remove the temporary stack space we allocated.
2895 if (adjustment != 0) {
2896 __ addl(ESP, Immediate(adjustment));
2897 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002898 break;
2899 }
2900
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002901 case DataType::Type::kFloat32:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002902 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002903 break;
2904
2905 default:
2906 LOG(FATAL) << "Unexpected type conversion from " << input_type
2907 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08002908 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002909 break;
2910
2911 default:
2912 LOG(FATAL) << "Unexpected type conversion from " << input_type
2913 << " to " << result_type;
2914 }
2915}
2916
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002917void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002918 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002919 new (GetGraph()->GetAllocator()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002920 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002921 case DataType::Type::kInt32: {
Mark Mendell09b84632015-02-13 17:48:38 -05002922 locations->SetInAt(0, Location::RequiresRegister());
2923 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2924 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2925 break;
2926 }
2927
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002928 case DataType::Type::kInt64: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002929 locations->SetInAt(0, Location::RequiresRegister());
2930 locations->SetInAt(1, Location::Any());
2931 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002932 break;
2933 }
2934
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002935 case DataType::Type::kFloat32:
2936 case DataType::Type::kFloat64: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002937 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002938 if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2939 DCHECK(add->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002940 } else if (add->InputAt(1)->IsConstant()) {
2941 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002942 } else {
2943 locations->SetInAt(1, Location::Any());
2944 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002945 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002946 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002947 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002948
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002949 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002950 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2951 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002952 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002953}
2954
2955void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2956 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002957 Location first = locations->InAt(0);
2958 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002959 Location out = locations->Out();
2960
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002961 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002962 case DataType::Type::kInt32: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002963 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002964 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2965 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002966 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2967 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05002968 } else {
2969 __ leal(out.AsRegister<Register>(), Address(
2970 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
2971 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002972 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002973 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
2974 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2975 __ addl(out.AsRegister<Register>(), Immediate(value));
2976 } else {
2977 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
2978 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002979 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05002980 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002981 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002982 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002983 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002984 }
2985
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002986 case DataType::Type::kInt64: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002987 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002988 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2989 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002990 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002991 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
2992 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002993 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002994 } else {
2995 DCHECK(second.IsConstant()) << second;
2996 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2997 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2998 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002999 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003000 break;
3001 }
3002
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003003 case DataType::Type::kFloat32: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003004 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003005 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04003006 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3007 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003008 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003009 __ addss(first.AsFpuRegister<XmmRegister>(),
3010 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003011 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3012 const_area->GetBaseMethodAddress(),
3013 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003014 } else {
3015 DCHECK(second.IsStackSlot());
3016 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003017 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003018 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003019 }
3020
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003021 case DataType::Type::kFloat64: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003022 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003023 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04003024 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3025 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003026 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003027 __ addsd(first.AsFpuRegister<XmmRegister>(),
3028 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003029 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3030 const_area->GetBaseMethodAddress(),
3031 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003032 } else {
3033 DCHECK(second.IsDoubleStackSlot());
3034 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003035 }
3036 break;
3037 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003038
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003039 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003040 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003041 }
3042}
3043
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003044void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003045 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003046 new (GetGraph()->GetAllocator()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003047 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003048 case DataType::Type::kInt32:
3049 case DataType::Type::kInt64: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003050 locations->SetInAt(0, Location::RequiresRegister());
3051 locations->SetInAt(1, Location::Any());
3052 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003053 break;
3054 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003055 case DataType::Type::kFloat32:
3056 case DataType::Type::kFloat64: {
Calin Juravle11351682014-10-23 15:38:15 +01003057 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003058 if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3059 DCHECK(sub->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003060 } else if (sub->InputAt(1)->IsConstant()) {
3061 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003062 } else {
3063 locations->SetInAt(1, Location::Any());
3064 }
Calin Juravle11351682014-10-23 15:38:15 +01003065 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003066 break;
Calin Juravle11351682014-10-23 15:38:15 +01003067 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003068
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003069 default:
Calin Juravle11351682014-10-23 15:38:15 +01003070 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003071 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003072}
3073
3074void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
3075 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003076 Location first = locations->InAt(0);
3077 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01003078 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003079 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003080 case DataType::Type::kInt32: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003081 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003082 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003083 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003084 __ subl(first.AsRegister<Register>(),
3085 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003086 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003087 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003088 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003089 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003090 }
3091
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003092 case DataType::Type::kInt64: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003093 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01003094 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3095 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003096 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01003097 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003098 __ sbbl(first.AsRegisterPairHigh<Register>(),
3099 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003100 } else {
3101 DCHECK(second.IsConstant()) << second;
3102 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3103 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3104 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003105 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003106 break;
3107 }
3108
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003109 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003110 if (second.IsFpuRegister()) {
3111 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3112 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3113 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003114 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003115 __ subss(first.AsFpuRegister<XmmRegister>(),
3116 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003117 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3118 const_area->GetBaseMethodAddress(),
3119 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003120 } else {
3121 DCHECK(second.IsStackSlot());
3122 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3123 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003124 break;
Calin Juravle11351682014-10-23 15:38:15 +01003125 }
3126
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003127 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003128 if (second.IsFpuRegister()) {
3129 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3130 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3131 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003132 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003133 __ subsd(first.AsFpuRegister<XmmRegister>(),
3134 codegen_->LiteralDoubleAddress(
3135 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003136 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003137 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3138 } else {
3139 DCHECK(second.IsDoubleStackSlot());
3140 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3141 }
Calin Juravle11351682014-10-23 15:38:15 +01003142 break;
3143 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003144
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003145 default:
Calin Juravle11351682014-10-23 15:38:15 +01003146 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003147 }
3148}
3149
Calin Juravle34bacdf2014-10-07 20:23:36 +01003150void LocationsBuilderX86::VisitMul(HMul* mul) {
3151 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003152 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Calin Juravle34bacdf2014-10-07 20:23:36 +01003153 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003154 case DataType::Type::kInt32:
Calin Juravle34bacdf2014-10-07 20:23:36 +01003155 locations->SetInAt(0, Location::RequiresRegister());
3156 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003157 if (mul->InputAt(1)->IsIntConstant()) {
3158 // Can use 3 operand multiply.
3159 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3160 } else {
3161 locations->SetOut(Location::SameAsFirstInput());
3162 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003163 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003164 case DataType::Type::kInt64: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003165 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003166 locations->SetInAt(1, Location::Any());
3167 locations->SetOut(Location::SameAsFirstInput());
3168 // Needed for imul on 32bits with 64bits output.
3169 locations->AddTemp(Location::RegisterLocation(EAX));
3170 locations->AddTemp(Location::RegisterLocation(EDX));
3171 break;
3172 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003173 case DataType::Type::kFloat32:
3174 case DataType::Type::kFloat64: {
Calin Juravleb5bfa962014-10-21 18:02:24 +01003175 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003176 if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3177 DCHECK(mul->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003178 } else if (mul->InputAt(1)->IsConstant()) {
3179 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003180 } else {
3181 locations->SetInAt(1, Location::Any());
3182 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003183 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003184 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003185 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003186
3187 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003188 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003189 }
3190}
3191
3192void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
3193 LocationSummary* locations = mul->GetLocations();
3194 Location first = locations->InAt(0);
3195 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003196 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003197
3198 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003199 case DataType::Type::kInt32:
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003200 // The constant may have ended up in a register, so test explicitly to avoid
3201 // problems where the output may not be the same as the first operand.
3202 if (mul->InputAt(1)->IsIntConstant()) {
3203 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3204 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
3205 } else if (second.IsRegister()) {
3206 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003207 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003208 } else {
3209 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003210 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003211 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003212 }
3213 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003214
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003215 case DataType::Type::kInt64: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003216 Register in1_hi = first.AsRegisterPairHigh<Register>();
3217 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003218 Register eax = locations->GetTemp(0).AsRegister<Register>();
3219 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003220
3221 DCHECK_EQ(EAX, eax);
3222 DCHECK_EQ(EDX, edx);
3223
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003224 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01003225 // output: in1
3226 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3227 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3228 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003229 if (second.IsConstant()) {
3230 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003231
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003232 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3233 int32_t low_value = Low32Bits(value);
3234 int32_t high_value = High32Bits(value);
3235 Immediate low(low_value);
3236 Immediate high(high_value);
3237
3238 __ movl(eax, high);
3239 // eax <- in1.lo * in2.hi
3240 __ imull(eax, in1_lo);
3241 // in1.hi <- in1.hi * in2.lo
3242 __ imull(in1_hi, low);
3243 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3244 __ addl(in1_hi, eax);
3245 // move in2_lo to eax to prepare for double precision
3246 __ movl(eax, low);
3247 // edx:eax <- in1.lo * in2.lo
3248 __ mull(in1_lo);
3249 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3250 __ addl(in1_hi, edx);
3251 // in1.lo <- (in1.lo * in2.lo)[31:0];
3252 __ movl(in1_lo, eax);
3253 } else if (second.IsRegisterPair()) {
3254 Register in2_hi = second.AsRegisterPairHigh<Register>();
3255 Register in2_lo = second.AsRegisterPairLow<Register>();
3256
3257 __ movl(eax, in2_hi);
3258 // eax <- in1.lo * in2.hi
3259 __ imull(eax, in1_lo);
3260 // in1.hi <- in1.hi * in2.lo
3261 __ imull(in1_hi, in2_lo);
3262 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3263 __ addl(in1_hi, eax);
3264 // move in1_lo to eax to prepare for double precision
3265 __ movl(eax, in1_lo);
3266 // edx:eax <- in1.lo * in2.lo
3267 __ mull(in2_lo);
3268 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3269 __ addl(in1_hi, edx);
3270 // in1.lo <- (in1.lo * in2.lo)[31:0];
3271 __ movl(in1_lo, eax);
3272 } else {
3273 DCHECK(second.IsDoubleStackSlot()) << second;
3274 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
3275 Address in2_lo(ESP, second.GetStackIndex());
3276
3277 __ movl(eax, in2_hi);
3278 // eax <- in1.lo * in2.hi
3279 __ imull(eax, in1_lo);
3280 // in1.hi <- in1.hi * in2.lo
3281 __ imull(in1_hi, in2_lo);
3282 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3283 __ addl(in1_hi, eax);
3284 // move in1_lo to eax to prepare for double precision
3285 __ movl(eax, in1_lo);
3286 // edx:eax <- in1.lo * in2.lo
3287 __ mull(in2_lo);
3288 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3289 __ addl(in1_hi, edx);
3290 // in1.lo <- (in1.lo * in2.lo)[31:0];
3291 __ movl(in1_lo, eax);
3292 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003293
3294 break;
3295 }
3296
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003297 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003298 DCHECK(first.Equals(locations->Out()));
3299 if (second.IsFpuRegister()) {
3300 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3301 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3302 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003303 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003304 __ mulss(first.AsFpuRegister<XmmRegister>(),
3305 codegen_->LiteralFloatAddress(
3306 const_area->GetConstant()->AsFloatConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003307 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003308 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3309 } else {
3310 DCHECK(second.IsStackSlot());
3311 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3312 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003313 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003314 }
3315
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003316 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003317 DCHECK(first.Equals(locations->Out()));
3318 if (second.IsFpuRegister()) {
3319 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3320 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3321 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003322 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003323 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3324 codegen_->LiteralDoubleAddress(
3325 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003326 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003327 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3328 } else {
3329 DCHECK(second.IsDoubleStackSlot());
3330 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3331 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003332 break;
3333 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003334
3335 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003336 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003337 }
3338}
3339
Roland Levillain232ade02015-04-20 15:14:36 +01003340void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3341 uint32_t temp_offset,
3342 uint32_t stack_adjustment,
3343 bool is_fp,
3344 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003345 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003346 DCHECK(!is_wide);
3347 if (is_fp) {
3348 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3349 } else {
3350 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3351 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003352 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003353 DCHECK(is_wide);
3354 if (is_fp) {
3355 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3356 } else {
3357 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3358 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003359 } else {
3360 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003361 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003362 Location stack_temp = Location::StackSlot(temp_offset);
3363 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003364 if (is_fp) {
3365 __ flds(Address(ESP, temp_offset));
3366 } else {
3367 __ filds(Address(ESP, temp_offset));
3368 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003369 } else {
3370 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3371 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003372 if (is_fp) {
3373 __ fldl(Address(ESP, temp_offset));
3374 } else {
3375 __ fildl(Address(ESP, temp_offset));
3376 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003377 }
3378 }
3379}
3380
3381void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003382 DataType::Type type = rem->GetResultType();
3383 bool is_float = type == DataType::Type::kFloat32;
3384 size_t elem_size = DataType::Size(type);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003385 LocationSummary* locations = rem->GetLocations();
3386 Location first = locations->InAt(0);
3387 Location second = locations->InAt(1);
3388 Location out = locations->Out();
3389
3390 // Create stack space for 2 elements.
3391 // TODO: enhance register allocator to ask for stack temporaries.
3392 __ subl(ESP, Immediate(2 * elem_size));
3393
3394 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003395 const bool is_wide = !is_float;
3396 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
3397 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003398
3399 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003400 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003401 __ Bind(&retry);
3402 __ fprem();
3403
3404 // Move FP status to AX.
3405 __ fstsw();
3406
3407 // And see if the argument reduction is complete. This is signaled by the
3408 // C2 FPU flag bit set to 0.
3409 __ andl(EAX, Immediate(kC2ConditionMask));
3410 __ j(kNotEqual, &retry);
3411
3412 // We have settled on the final value. Retrieve it into an XMM register.
3413 // Store FP top of stack to real stack.
3414 if (is_float) {
3415 __ fsts(Address(ESP, 0));
3416 } else {
3417 __ fstl(Address(ESP, 0));
3418 }
3419
3420 // Pop the 2 items from the FP stack.
3421 __ fucompp();
3422
3423 // Load the value from the stack into an XMM register.
3424 DCHECK(out.IsFpuRegister()) << out;
3425 if (is_float) {
3426 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3427 } else {
3428 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3429 }
3430
3431 // And remove the temporary stack space we allocated.
3432 __ addl(ESP, Immediate(2 * elem_size));
3433}
3434
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003435
3436void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3437 DCHECK(instruction->IsDiv() || instruction->IsRem());
3438
3439 LocationSummary* locations = instruction->GetLocations();
3440 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003441 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003442
3443 Register out_register = locations->Out().AsRegister<Register>();
3444 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003445 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003446
3447 DCHECK(imm == 1 || imm == -1);
3448
3449 if (instruction->IsRem()) {
3450 __ xorl(out_register, out_register);
3451 } else {
3452 __ movl(out_register, input_register);
3453 if (imm == -1) {
3454 __ negl(out_register);
3455 }
3456 }
3457}
3458
3459
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003460void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003461 LocationSummary* locations = instruction->GetLocations();
3462
3463 Register out_register = locations->Out().AsRegister<Register>();
3464 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003465 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003466 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3467 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003468
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003469 Register num = locations->GetTemp(0).AsRegister<Register>();
3470
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003471 __ leal(num, Address(input_register, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003472 __ testl(input_register, input_register);
3473 __ cmovl(kGreaterEqual, num, input_register);
3474 int shift = CTZ(imm);
3475 __ sarl(num, Immediate(shift));
3476
3477 if (imm < 0) {
3478 __ negl(num);
3479 }
3480
3481 __ movl(out_register, num);
3482}
3483
3484void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3485 DCHECK(instruction->IsDiv() || instruction->IsRem());
3486
3487 LocationSummary* locations = instruction->GetLocations();
3488 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3489
3490 Register eax = locations->InAt(0).AsRegister<Register>();
3491 Register out = locations->Out().AsRegister<Register>();
3492 Register num;
3493 Register edx;
3494
3495 if (instruction->IsDiv()) {
3496 edx = locations->GetTemp(0).AsRegister<Register>();
3497 num = locations->GetTemp(1).AsRegister<Register>();
3498 } else {
3499 edx = locations->Out().AsRegister<Register>();
3500 num = locations->GetTemp(0).AsRegister<Register>();
3501 }
3502
3503 DCHECK_EQ(EAX, eax);
3504 DCHECK_EQ(EDX, edx);
3505 if (instruction->IsDiv()) {
3506 DCHECK_EQ(EAX, out);
3507 } else {
3508 DCHECK_EQ(EDX, out);
3509 }
3510
3511 int64_t magic;
3512 int shift;
3513 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3514
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003515 // Save the numerator.
3516 __ movl(num, eax);
3517
3518 // EAX = magic
3519 __ movl(eax, Immediate(magic));
3520
3521 // EDX:EAX = magic * numerator
3522 __ imull(num);
3523
3524 if (imm > 0 && magic < 0) {
3525 // EDX += num
3526 __ addl(edx, num);
3527 } else if (imm < 0 && magic > 0) {
3528 __ subl(edx, num);
3529 }
3530
3531 // Shift if needed.
3532 if (shift != 0) {
3533 __ sarl(edx, Immediate(shift));
3534 }
3535
3536 // EDX += 1 if EDX < 0
3537 __ movl(eax, edx);
3538 __ shrl(edx, Immediate(31));
3539 __ addl(edx, eax);
3540
3541 if (instruction->IsRem()) {
3542 __ movl(eax, num);
3543 __ imull(edx, Immediate(imm));
3544 __ subl(eax, edx);
3545 __ movl(edx, eax);
3546 } else {
3547 __ movl(eax, edx);
3548 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003549}
3550
Calin Juravlebacfec32014-11-14 15:54:36 +00003551void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3552 DCHECK(instruction->IsDiv() || instruction->IsRem());
3553
3554 LocationSummary* locations = instruction->GetLocations();
3555 Location out = locations->Out();
3556 Location first = locations->InAt(0);
3557 Location second = locations->InAt(1);
3558 bool is_div = instruction->IsDiv();
3559
3560 switch (instruction->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003561 case DataType::Type::kInt32: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003562 DCHECK_EQ(EAX, first.AsRegister<Register>());
3563 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003564
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003565 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003566 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003567
3568 if (imm == 0) {
3569 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3570 } else if (imm == 1 || imm == -1) {
3571 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003572 } else if (is_div && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003573 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003574 } else {
3575 DCHECK(imm <= -2 || imm >= 2);
3576 GenerateDivRemWithAnyConstant(instruction);
3577 }
3578 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01003579 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) DivRemMinusOneSlowPathX86(
David Srbecky9cd6d372016-02-09 15:24:47 +00003580 instruction, out.AsRegister<Register>(), is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003581 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003582
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003583 Register second_reg = second.AsRegister<Register>();
3584 // 0x80000000/-1 triggers an arithmetic exception!
3585 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3586 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003587
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003588 __ cmpl(second_reg, Immediate(-1));
3589 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003590
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003591 // edx:eax <- sign-extended of eax
3592 __ cdq();
3593 // eax = quotient, edx = remainder
3594 __ idivl(second_reg);
3595 __ Bind(slow_path->GetExitLabel());
3596 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003597 break;
3598 }
3599
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003600 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003601 InvokeRuntimeCallingConvention calling_convention;
3602 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3603 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3604 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3605 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3606 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3607 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3608
3609 if (is_div) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003610 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003611 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003612 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003613 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003614 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003615 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003616 break;
3617 }
3618
3619 default:
3620 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3621 }
3622}
3623
Calin Juravle7c4954d2014-10-28 16:57:40 +00003624void LocationsBuilderX86::VisitDiv(HDiv* div) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003625 LocationSummary::CallKind call_kind = (div->GetResultType() == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003626 ? LocationSummary::kCallOnMainOnly
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003627 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01003628 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(div, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003629
Calin Juravle7c4954d2014-10-28 16:57:40 +00003630 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003631 case DataType::Type::kInt32: {
Calin Juravled0d48522014-11-04 16:40:20 +00003632 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003633 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003634 locations->SetOut(Location::SameAsFirstInput());
3635 // Intel uses edx:eax as the dividend.
3636 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003637 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3638 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3639 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003640 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003641 locations->AddTemp(Location::RequiresRegister());
3642 }
Calin Juravled0d48522014-11-04 16:40:20 +00003643 break;
3644 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003645 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003646 InvokeRuntimeCallingConvention calling_convention;
3647 locations->SetInAt(0, Location::RegisterPairLocation(
3648 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3649 locations->SetInAt(1, Location::RegisterPairLocation(
3650 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3651 // Runtime helper puts the result in EAX, EDX.
3652 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003653 break;
3654 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003655 case DataType::Type::kFloat32:
3656 case DataType::Type::kFloat64: {
Calin Juravle7c4954d2014-10-28 16:57:40 +00003657 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003658 if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3659 DCHECK(div->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003660 } else if (div->InputAt(1)->IsConstant()) {
3661 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003662 } else {
3663 locations->SetInAt(1, Location::Any());
3664 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003665 locations->SetOut(Location::SameAsFirstInput());
3666 break;
3667 }
3668
3669 default:
3670 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3671 }
3672}
3673
3674void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3675 LocationSummary* locations = div->GetLocations();
3676 Location first = locations->InAt(0);
3677 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003678
3679 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003680 case DataType::Type::kInt32:
3681 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003682 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003683 break;
3684 }
3685
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003686 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003687 if (second.IsFpuRegister()) {
3688 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3689 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3690 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003691 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003692 __ divss(first.AsFpuRegister<XmmRegister>(),
3693 codegen_->LiteralFloatAddress(
3694 const_area->GetConstant()->AsFloatConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003695 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003696 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3697 } else {
3698 DCHECK(second.IsStackSlot());
3699 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3700 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003701 break;
3702 }
3703
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003704 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003705 if (second.IsFpuRegister()) {
3706 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3707 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3708 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003709 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003710 __ divsd(first.AsFpuRegister<XmmRegister>(),
3711 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003712 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3713 const_area->GetBaseMethodAddress(),
3714 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003715 } else {
3716 DCHECK(second.IsDoubleStackSlot());
3717 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3718 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003719 break;
3720 }
3721
3722 default:
3723 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3724 }
3725}
3726
Calin Juravlebacfec32014-11-14 15:54:36 +00003727void LocationsBuilderX86::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003728 DataType::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003729
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003730 LocationSummary::CallKind call_kind = (rem->GetResultType() == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003731 ? LocationSummary::kCallOnMainOnly
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003732 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01003733 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003734
Calin Juravled2ec87d2014-12-08 14:24:46 +00003735 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003736 case DataType::Type::kInt32: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003737 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003738 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003739 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003740 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3741 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3742 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003743 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003744 locations->AddTemp(Location::RequiresRegister());
3745 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003746 break;
3747 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003748 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003749 InvokeRuntimeCallingConvention calling_convention;
3750 locations->SetInAt(0, Location::RegisterPairLocation(
3751 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3752 locations->SetInAt(1, Location::RegisterPairLocation(
3753 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3754 // Runtime helper puts the result in EAX, EDX.
3755 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3756 break;
3757 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003758 case DataType::Type::kFloat64:
3759 case DataType::Type::kFloat32: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003760 locations->SetInAt(0, Location::Any());
3761 locations->SetInAt(1, Location::Any());
3762 locations->SetOut(Location::RequiresFpuRegister());
3763 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003764 break;
3765 }
3766
3767 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003768 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003769 }
3770}
3771
3772void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003773 DataType::Type type = rem->GetResultType();
Calin Juravlebacfec32014-11-14 15:54:36 +00003774 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003775 case DataType::Type::kInt32:
3776 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003777 GenerateDivRemIntegral(rem);
3778 break;
3779 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003780 case DataType::Type::kFloat32:
3781 case DataType::Type::kFloat64: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003782 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003783 break;
3784 }
3785 default:
3786 LOG(FATAL) << "Unexpected rem type " << type;
3787 }
3788}
3789
Calin Juravled0d48522014-11-04 16:40:20 +00003790void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003791 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003792 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003793 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003794 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003795 case DataType::Type::kInt8:
3796 case DataType::Type::kUint16:
3797 case DataType::Type::kInt16:
3798 case DataType::Type::kInt32: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003799 locations->SetInAt(0, Location::Any());
3800 break;
3801 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003802 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003803 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
3804 if (!instruction->IsConstant()) {
3805 locations->AddTemp(Location::RequiresRegister());
3806 }
3807 break;
3808 }
3809 default:
3810 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3811 }
Calin Juravled0d48522014-11-04 16:40:20 +00003812}
3813
3814void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01003815 SlowPathCode* slow_path =
3816 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003817 codegen_->AddSlowPath(slow_path);
3818
3819 LocationSummary* locations = instruction->GetLocations();
3820 Location value = locations->InAt(0);
3821
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003822 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003823 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003824 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003825 case DataType::Type::kInt8:
3826 case DataType::Type::kUint16:
3827 case DataType::Type::kInt16:
3828 case DataType::Type::kInt32: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003829 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003830 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003831 __ j(kEqual, slow_path->GetEntryLabel());
3832 } else if (value.IsStackSlot()) {
3833 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
3834 __ j(kEqual, slow_path->GetEntryLabel());
3835 } else {
3836 DCHECK(value.IsConstant()) << value;
3837 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01003838 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003839 }
3840 }
3841 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003842 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003843 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003844 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003845 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003846 __ movl(temp, value.AsRegisterPairLow<Register>());
3847 __ orl(temp, value.AsRegisterPairHigh<Register>());
3848 __ j(kEqual, slow_path->GetEntryLabel());
3849 } else {
3850 DCHECK(value.IsConstant()) << value;
3851 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3852 __ jmp(slow_path->GetEntryLabel());
3853 }
3854 }
3855 break;
3856 }
3857 default:
3858 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003859 }
Calin Juravled0d48522014-11-04 16:40:20 +00003860}
3861
Calin Juravle9aec02f2014-11-18 23:06:35 +00003862void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3863 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3864
3865 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003866 new (GetGraph()->GetAllocator()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003867
3868 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003869 case DataType::Type::kInt32:
3870 case DataType::Type::kInt64: {
Mark P Mendell73945692015-04-29 14:56:17 +00003871 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003872 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003873 // The shift count needs to be in CL or a constant.
3874 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003875 locations->SetOut(Location::SameAsFirstInput());
3876 break;
3877 }
3878 default:
3879 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3880 }
3881}
3882
3883void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3884 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3885
3886 LocationSummary* locations = op->GetLocations();
3887 Location first = locations->InAt(0);
3888 Location second = locations->InAt(1);
3889 DCHECK(first.Equals(locations->Out()));
3890
3891 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003892 case DataType::Type::kInt32: {
Mark P Mendell73945692015-04-29 14:56:17 +00003893 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003894 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003895 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003896 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003897 DCHECK_EQ(ECX, second_reg);
3898 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003899 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003900 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003901 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003902 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003903 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003904 }
3905 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003906 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003907 if (shift == 0) {
3908 return;
3909 }
3910 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003911 if (op->IsShl()) {
3912 __ shll(first_reg, imm);
3913 } else if (op->IsShr()) {
3914 __ sarl(first_reg, imm);
3915 } else {
3916 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003917 }
3918 }
3919 break;
3920 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003921 case DataType::Type::kInt64: {
Mark P Mendell73945692015-04-29 14:56:17 +00003922 if (second.IsRegister()) {
3923 Register second_reg = second.AsRegister<Register>();
3924 DCHECK_EQ(ECX, second_reg);
3925 if (op->IsShl()) {
3926 GenerateShlLong(first, second_reg);
3927 } else if (op->IsShr()) {
3928 GenerateShrLong(first, second_reg);
3929 } else {
3930 GenerateUShrLong(first, second_reg);
3931 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003932 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003933 // Shift by a constant.
Roland Levillain5b5b9312016-03-22 14:57:31 +00003934 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003935 // Nothing to do if the shift is 0, as the input is already the output.
3936 if (shift != 0) {
3937 if (op->IsShl()) {
3938 GenerateShlLong(first, shift);
3939 } else if (op->IsShr()) {
3940 GenerateShrLong(first, shift);
3941 } else {
3942 GenerateUShrLong(first, shift);
3943 }
3944 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003945 }
3946 break;
3947 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003948 default:
3949 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3950 }
3951}
3952
Mark P Mendell73945692015-04-29 14:56:17 +00003953void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3954 Register low = loc.AsRegisterPairLow<Register>();
3955 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003956 if (shift == 1) {
3957 // This is just an addition.
3958 __ addl(low, low);
3959 __ adcl(high, high);
3960 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003961 // Shift by 32 is easy. High gets low, and low gets 0.
3962 codegen_->EmitParallelMoves(
3963 loc.ToLow(),
3964 loc.ToHigh(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003965 DataType::Type::kInt32,
Mark P Mendell73945692015-04-29 14:56:17 +00003966 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3967 loc.ToLow(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003968 DataType::Type::kInt32);
Mark P Mendell73945692015-04-29 14:56:17 +00003969 } else if (shift > 32) {
3970 // Low part becomes 0. High part is low part << (shift-32).
3971 __ movl(high, low);
3972 __ shll(high, Immediate(shift - 32));
3973 __ xorl(low, low);
3974 } else {
3975 // Between 1 and 31.
3976 __ shld(high, low, Immediate(shift));
3977 __ shll(low, Immediate(shift));
3978 }
3979}
3980
Calin Juravle9aec02f2014-11-18 23:06:35 +00003981void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003982 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003983 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
3984 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
3985 __ testl(shifter, Immediate(32));
3986 __ j(kEqual, &done);
3987 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
3988 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
3989 __ Bind(&done);
3990}
3991
Mark P Mendell73945692015-04-29 14:56:17 +00003992void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
3993 Register low = loc.AsRegisterPairLow<Register>();
3994 Register high = loc.AsRegisterPairHigh<Register>();
3995 if (shift == 32) {
3996 // Need to copy the sign.
3997 DCHECK_NE(low, high);
3998 __ movl(low, high);
3999 __ sarl(high, Immediate(31));
4000 } else if (shift > 32) {
4001 DCHECK_NE(low, high);
4002 // High part becomes sign. Low part is shifted by shift - 32.
4003 __ movl(low, high);
4004 __ sarl(high, Immediate(31));
4005 __ sarl(low, Immediate(shift - 32));
4006 } else {
4007 // Between 1 and 31.
4008 __ shrd(low, high, Immediate(shift));
4009 __ sarl(high, Immediate(shift));
4010 }
4011}
4012
Calin Juravle9aec02f2014-11-18 23:06:35 +00004013void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004014 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004015 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4016 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
4017 __ testl(shifter, Immediate(32));
4018 __ j(kEqual, &done);
4019 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4020 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
4021 __ Bind(&done);
4022}
4023
Mark P Mendell73945692015-04-29 14:56:17 +00004024void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
4025 Register low = loc.AsRegisterPairLow<Register>();
4026 Register high = loc.AsRegisterPairHigh<Register>();
4027 if (shift == 32) {
4028 // Shift by 32 is easy. Low gets high, and high gets 0.
4029 codegen_->EmitParallelMoves(
4030 loc.ToHigh(),
4031 loc.ToLow(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004032 DataType::Type::kInt32,
Mark P Mendell73945692015-04-29 14:56:17 +00004033 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
4034 loc.ToHigh(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004035 DataType::Type::kInt32);
Mark P Mendell73945692015-04-29 14:56:17 +00004036 } else if (shift > 32) {
4037 // Low part is high >> (shift - 32). High part becomes 0.
4038 __ movl(low, high);
4039 __ shrl(low, Immediate(shift - 32));
4040 __ xorl(high, high);
4041 } else {
4042 // Between 1 and 31.
4043 __ shrd(low, high, Immediate(shift));
4044 __ shrl(high, Immediate(shift));
4045 }
4046}
4047
Calin Juravle9aec02f2014-11-18 23:06:35 +00004048void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004049 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004050 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4051 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
4052 __ testl(shifter, Immediate(32));
4053 __ j(kEqual, &done);
4054 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4055 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
4056 __ Bind(&done);
4057}
4058
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004059void LocationsBuilderX86::VisitRor(HRor* ror) {
4060 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004061 new (GetGraph()->GetAllocator()) LocationSummary(ror, LocationSummary::kNoCall);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004062
4063 switch (ror->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004064 case DataType::Type::kInt64:
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004065 // Add the temporary needed.
4066 locations->AddTemp(Location::RequiresRegister());
4067 FALLTHROUGH_INTENDED;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004068 case DataType::Type::kInt32:
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004069 locations->SetInAt(0, Location::RequiresRegister());
4070 // The shift count needs to be in CL (unless it is a constant).
4071 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
4072 locations->SetOut(Location::SameAsFirstInput());
4073 break;
4074 default:
4075 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4076 UNREACHABLE();
4077 }
4078}
4079
4080void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
4081 LocationSummary* locations = ror->GetLocations();
4082 Location first = locations->InAt(0);
4083 Location second = locations->InAt(1);
4084
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004085 if (ror->GetResultType() == DataType::Type::kInt32) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004086 Register first_reg = first.AsRegister<Register>();
4087 if (second.IsRegister()) {
4088 Register second_reg = second.AsRegister<Register>();
4089 __ rorl(first_reg, second_reg);
4090 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004091 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004092 __ rorl(first_reg, imm);
4093 }
4094 return;
4095 }
4096
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004097 DCHECK_EQ(ror->GetResultType(), DataType::Type::kInt64);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004098 Register first_reg_lo = first.AsRegisterPairLow<Register>();
4099 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
4100 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
4101 if (second.IsRegister()) {
4102 Register second_reg = second.AsRegister<Register>();
4103 DCHECK_EQ(second_reg, ECX);
4104 __ movl(temp_reg, first_reg_hi);
4105 __ shrd(first_reg_hi, first_reg_lo, second_reg);
4106 __ shrd(first_reg_lo, temp_reg, second_reg);
4107 __ movl(temp_reg, first_reg_hi);
4108 __ testl(second_reg, Immediate(32));
4109 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
4110 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
4111 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004112 int32_t shift_amt = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004113 if (shift_amt == 0) {
4114 // Already fine.
4115 return;
4116 }
4117 if (shift_amt == 32) {
4118 // Just swap.
4119 __ movl(temp_reg, first_reg_lo);
4120 __ movl(first_reg_lo, first_reg_hi);
4121 __ movl(first_reg_hi, temp_reg);
4122 return;
4123 }
4124
4125 Immediate imm(shift_amt);
4126 // Save the constents of the low value.
4127 __ movl(temp_reg, first_reg_lo);
4128
4129 // Shift right into low, feeding bits from high.
4130 __ shrd(first_reg_lo, first_reg_hi, imm);
4131
4132 // Shift right into high, feeding bits from the original low.
4133 __ shrd(first_reg_hi, temp_reg, imm);
4134
4135 // Swap if needed.
4136 if (shift_amt > 32) {
4137 __ movl(temp_reg, first_reg_lo);
4138 __ movl(first_reg_lo, first_reg_hi);
4139 __ movl(first_reg_hi, temp_reg);
4140 }
4141 }
4142}
4143
Calin Juravle9aec02f2014-11-18 23:06:35 +00004144void LocationsBuilderX86::VisitShl(HShl* shl) {
4145 HandleShift(shl);
4146}
4147
4148void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
4149 HandleShift(shl);
4150}
4151
4152void LocationsBuilderX86::VisitShr(HShr* shr) {
4153 HandleShift(shr);
4154}
4155
4156void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
4157 HandleShift(shr);
4158}
4159
4160void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
4161 HandleShift(ushr);
4162}
4163
4164void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
4165 HandleShift(ushr);
4166}
4167
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004168void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004169 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
4170 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004171 locations->SetOut(Location::RegisterLocation(EAX));
David Brazdil6de19382016-01-08 17:37:10 +00004172 if (instruction->IsStringAlloc()) {
4173 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4174 } else {
4175 InvokeRuntimeCallingConvention calling_convention;
4176 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00004177 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004178}
4179
4180void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004181 // Note: if heap poisoning is enabled, the entry point takes cares
4182 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004183 if (instruction->IsStringAlloc()) {
4184 // String is allocated through StringFactory. Call NewEmptyString entry point.
4185 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07004186 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004187 __ fs()->movl(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString)));
4188 __ call(Address(temp, code_offset.Int32Value()));
4189 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4190 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01004191 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00004192 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00004193 DCHECK(!codegen_->IsLeafMethod());
4194 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004195}
4196
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004197void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004198 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
4199 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004200 locations->SetOut(Location::RegisterLocation(EAX));
4201 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004202 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4203 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004204}
4205
4206void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004207 // Note: if heap poisoning is enabled, the entry point takes cares
4208 // of poisoning the reference.
Nicolas Geoffrayd0958442017-01-30 14:57:16 +00004209 QuickEntrypointEnum entrypoint =
4210 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
4211 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004212 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004213 DCHECK(!codegen_->IsLeafMethod());
4214}
4215
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004216void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004217 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004218 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004219 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4220 if (location.IsStackSlot()) {
4221 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4222 } else if (location.IsDoubleStackSlot()) {
4223 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004224 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004225 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004226}
4227
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004228void InstructionCodeGeneratorX86::VisitParameterValue(
4229 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4230}
4231
4232void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
4233 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004234 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004235 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4236}
4237
4238void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004239}
4240
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004241void LocationsBuilderX86::VisitClassTableGet(HClassTableGet* instruction) {
4242 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004243 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004244 locations->SetInAt(0, Location::RequiresRegister());
4245 locations->SetOut(Location::RequiresRegister());
4246}
4247
4248void InstructionCodeGeneratorX86::VisitClassTableGet(HClassTableGet* instruction) {
4249 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004250 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004251 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004252 instruction->GetIndex(), kX86PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004253 __ movl(locations->Out().AsRegister<Register>(),
4254 Address(locations->InAt(0).AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004255 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004256 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004257 instruction->GetIndex(), kX86PointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004258 __ movl(locations->Out().AsRegister<Register>(),
4259 Address(locations->InAt(0).AsRegister<Register>(),
4260 mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
4261 // temp = temp->GetImtEntryAt(method_offset);
4262 __ movl(locations->Out().AsRegister<Register>(),
4263 Address(locations->Out().AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004264 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004265}
4266
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004267void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004268 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004269 new (GetGraph()->GetAllocator()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004270 locations->SetInAt(0, Location::RequiresRegister());
4271 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004272}
4273
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004274void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
4275 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01004276 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004277 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01004278 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004279 switch (not_->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004280 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004281 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004282 break;
4283
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004284 case DataType::Type::kInt64:
Roland Levillain70566432014-10-24 16:20:17 +01004285 __ notl(out.AsRegisterPairLow<Register>());
4286 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004287 break;
4288
4289 default:
4290 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4291 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004292}
4293
David Brazdil66d126e2015-04-03 16:02:44 +01004294void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
4295 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004296 new (GetGraph()->GetAllocator()) LocationSummary(bool_not, LocationSummary::kNoCall);
David Brazdil66d126e2015-04-03 16:02:44 +01004297 locations->SetInAt(0, Location::RequiresRegister());
4298 locations->SetOut(Location::SameAsFirstInput());
4299}
4300
4301void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004302 LocationSummary* locations = bool_not->GetLocations();
4303 Location in = locations->InAt(0);
4304 Location out = locations->Out();
4305 DCHECK(in.Equals(out));
4306 __ xorl(out.AsRegister<Register>(), Immediate(1));
4307}
4308
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004309void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004310 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004311 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004312 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004313 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004314 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004315 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004316 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004317 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004318 case DataType::Type::kInt32:
4319 case DataType::Type::kInt64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00004320 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00004321 locations->SetInAt(1, Location::Any());
4322 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4323 break;
4324 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004325 case DataType::Type::kFloat32:
4326 case DataType::Type::kFloat64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00004327 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004328 if (compare->InputAt(1)->IsX86LoadFromConstantTable()) {
4329 DCHECK(compare->InputAt(1)->IsEmittedAtUseSite());
4330 } else if (compare->InputAt(1)->IsConstant()) {
4331 locations->SetInAt(1, Location::RequiresFpuRegister());
4332 } else {
4333 locations->SetInAt(1, Location::Any());
4334 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004335 locations->SetOut(Location::RequiresRegister());
4336 break;
4337 }
4338 default:
4339 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4340 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004341}
4342
4343void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004344 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004345 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004346 Location left = locations->InAt(0);
4347 Location right = locations->InAt(1);
4348
Mark Mendell0c9497d2015-08-21 09:30:05 -04004349 NearLabel less, greater, done;
Aart Bika19616e2016-02-01 18:57:58 -08004350 Condition less_cond = kLess;
4351
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004352 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004353 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004354 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004355 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004356 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004357 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004358 case DataType::Type::kInt32: {
Roland Levillain0b671c02016-08-19 12:02:34 +01004359 codegen_->GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08004360 break;
4361 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004362 case DataType::Type::kInt64: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004363 Register left_low = left.AsRegisterPairLow<Register>();
4364 Register left_high = left.AsRegisterPairHigh<Register>();
4365 int32_t val_low = 0;
4366 int32_t val_high = 0;
4367 bool right_is_const = false;
4368
4369 if (right.IsConstant()) {
4370 DCHECK(right.GetConstant()->IsLongConstant());
4371 right_is_const = true;
4372 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
4373 val_low = Low32Bits(val);
4374 val_high = High32Bits(val);
4375 }
4376
Calin Juravleddb7df22014-11-25 20:56:51 +00004377 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004378 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004379 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004380 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004381 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004382 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004383 codegen_->Compare32BitValue(left_high, val_high);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004384 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004385 __ j(kLess, &less); // Signed compare.
4386 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004387 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004388 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004389 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004390 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004391 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004392 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004393 codegen_->Compare32BitValue(left_low, val_low);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004394 }
Aart Bika19616e2016-02-01 18:57:58 -08004395 less_cond = kBelow; // for CF (unsigned).
Calin Juravleddb7df22014-11-25 20:56:51 +00004396 break;
4397 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004398 case DataType::Type::kFloat32: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004399 GenerateFPCompare(left, right, compare, false);
Calin Juravleddb7df22014-11-25 20:56:51 +00004400 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004401 less_cond = kBelow; // for CF (floats).
Calin Juravleddb7df22014-11-25 20:56:51 +00004402 break;
4403 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004404 case DataType::Type::kFloat64: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004405 GenerateFPCompare(left, right, compare, true);
Calin Juravleddb7df22014-11-25 20:56:51 +00004406 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004407 less_cond = kBelow; // for CF (floats).
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004408 break;
4409 }
4410 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004411 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004412 }
Aart Bika19616e2016-02-01 18:57:58 -08004413
Calin Juravleddb7df22014-11-25 20:56:51 +00004414 __ movl(out, Immediate(0));
4415 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08004416 __ j(less_cond, &less);
Calin Juravleddb7df22014-11-25 20:56:51 +00004417
4418 __ Bind(&greater);
4419 __ movl(out, Immediate(1));
4420 __ jmp(&done);
4421
4422 __ Bind(&less);
4423 __ movl(out, Immediate(-1));
4424
4425 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004426}
4427
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004428void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004429 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004430 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004431 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004432 locations->SetInAt(i, Location::Any());
4433 }
4434 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004435}
4436
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004437void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004438 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004439}
4440
Roland Levillain7c1559a2015-12-15 10:55:36 +00004441void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004442 /*
4443 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
4444 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
4445 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4446 */
4447 switch (kind) {
4448 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004449 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004450 break;
4451 }
4452 case MemBarrierKind::kAnyStore:
4453 case MemBarrierKind::kLoadAny:
4454 case MemBarrierKind::kStoreStore: {
4455 // nop
4456 break;
4457 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004458 case MemBarrierKind::kNTStoreStore:
4459 // Non-Temporal Store/Store needs an explicit fence.
4460 MemoryFence(/* non-temporal */ true);
4461 break;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004462 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004463}
4464
Vladimir Markodc151b22015-10-15 18:02:30 +01004465HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
4466 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004467 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004468 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004469}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004470
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004471Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4472 Register temp) {
4473 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Markoc53c0792015-11-19 15:48:33 +00004474 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004475 if (!invoke->GetLocations()->Intrinsified()) {
4476 return location.AsRegister<Register>();
4477 }
4478 // For intrinsics we allow any location, so it may be on the stack.
4479 if (!location.IsRegister()) {
4480 __ movl(temp, Address(ESP, location.GetStackIndex()));
4481 return temp;
4482 }
4483 // For register locations, check if the register was saved. If so, get it from the stack.
4484 // Note: There is a chance that the register was saved but not overwritten, so we could
4485 // save one load. However, since this is just an intrinsic slow path we prefer this
4486 // simple and more robust approach rather that trying to determine if that's the case.
4487 SlowPathCode* slow_path = GetCurrentSlowPath();
Vladimir Marko4ee8e292017-06-02 15:39:30 +00004488 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
4489 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4490 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4491 __ movl(temp, Address(ESP, stack_offset));
4492 return temp;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004493 }
4494 return location.AsRegister<Register>();
4495}
4496
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004497void CodeGeneratorX86::GenerateStaticOrDirectCall(
4498 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Vladimir Marko58155012015-08-19 12:49:41 +00004499 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4500 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004501 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00004502 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004503 uint32_t offset =
4504 GetThreadOffset<kX86PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
4505 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004506 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004507 }
Vladimir Marko58155012015-08-19 12:49:41 +00004508 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004509 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004510 break;
Vladimir Marko65979462017-05-19 17:25:12 +01004511 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
4512 DCHECK(GetCompilerOptions().IsBootImage());
4513 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4514 temp.AsRegister<Register>());
4515 __ leal(temp.AsRegister<Register>(), Address(base_reg, CodeGeneratorX86::kDummy32BitOffset));
4516 RecordBootMethodPatch(invoke);
4517 break;
4518 }
Vladimir Marko58155012015-08-19 12:49:41 +00004519 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4520 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
4521 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004522 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004523 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4524 temp.AsRegister<Register>());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004525 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004526 // Bind a new fixup label at the end of the "movl" insn.
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004527 __ Bind(NewMethodBssEntryPatch(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004528 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress(),
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004529 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex())));
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004530 break;
4531 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004532 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
4533 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
4534 return; // No code pointer retrieval; the runtime performs the call directly.
Vladimir Marko9b688a02015-05-06 14:12:42 +01004535 }
Vladimir Marko58155012015-08-19 12:49:41 +00004536 }
4537
4538 switch (invoke->GetCodePtrLocation()) {
4539 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4540 __ call(GetFrameEntryLabel());
4541 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004542 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4543 // (callee_method + offset_of_quick_compiled_code)()
4544 __ call(Address(callee_method.AsRegister<Register>(),
4545 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07004546 kX86PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004547 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04004548 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004549 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Mark Mendell09ed1a32015-03-25 08:30:06 -04004550
4551 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04004552}
4553
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004554void CodeGeneratorX86::GenerateVirtualCall(
4555 HInvokeVirtual* invoke, Location temp_in, SlowPathCode* slow_path) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004556 Register temp = temp_in.AsRegister<Register>();
4557 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4558 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004559
4560 // Use the calling convention instead of the location of the receiver, as
4561 // intrinsics may have put the receiver in a different register. In the intrinsics
4562 // slow path, the arguments have been moved to the right place, so here we are
4563 // guaranteed that the receiver is the first register of the calling convention.
4564 InvokeDexCallingConvention calling_convention;
4565 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004566 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004567 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004568 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004569 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004570 // Instead of simply (possibly) unpoisoning `temp` here, we should
4571 // emit a read barrier for the previous class reference load.
4572 // However this is not required in practice, as this is an
4573 // intermediate/temporary reference and because the current
4574 // concurrent copying collector keeps the from-space memory
4575 // intact/accessible until the end of the marking phase (the
4576 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004577 __ MaybeUnpoisonHeapReference(temp);
4578 // temp = temp->GetMethodAt(method_offset);
4579 __ movl(temp, Address(temp, method_offset));
4580 // call temp->GetEntryPoint();
4581 __ call(Address(
Andreas Gampe542451c2016-07-26 09:02:02 -07004582 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004583 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004584}
4585
Vladimir Marko65979462017-05-19 17:25:12 +01004586void CodeGeneratorX86::RecordBootMethodPatch(HInvokeStaticOrDirect* invoke) {
4587 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
4588 HX86ComputeBaseMethodAddress* address =
4589 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
4590 boot_image_method_patches_.emplace_back(address,
4591 *invoke->GetTargetMethod().dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07004592 invoke->GetTargetMethod().index);
Vladimir Marko65979462017-05-19 17:25:12 +01004593 __ Bind(&boot_image_method_patches_.back().label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004594}
4595
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004596Label* CodeGeneratorX86::NewMethodBssEntryPatch(
4597 HX86ComputeBaseMethodAddress* method_address,
4598 MethodReference target_method) {
4599 // Add the patch entry and bind its label at the end of the instruction.
4600 method_bss_entry_patches_.emplace_back(method_address,
4601 *target_method.dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07004602 target_method.index);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004603 return &method_bss_entry_patches_.back().label;
4604}
4605
Vladimir Marko1998cd02017-01-13 13:02:58 +00004606void CodeGeneratorX86::RecordBootTypePatch(HLoadClass* load_class) {
Vladimir Marko00916b92017-05-17 17:45:45 +01004607 HX86ComputeBaseMethodAddress* address = load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004608 boot_image_type_patches_.emplace_back(address,
4609 load_class->GetDexFile(),
Vladimir Marko1998cd02017-01-13 13:02:58 +00004610 load_class->GetTypeIndex().index_);
4611 __ Bind(&boot_image_type_patches_.back().label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004612}
4613
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004614Label* CodeGeneratorX86::NewTypeBssEntryPatch(HLoadClass* load_class) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004615 HX86ComputeBaseMethodAddress* address =
4616 load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
4617 type_bss_entry_patches_.emplace_back(
4618 address, load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00004619 return &type_bss_entry_patches_.back().label;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004620}
4621
Vladimir Marko65979462017-05-19 17:25:12 +01004622void CodeGeneratorX86::RecordBootStringPatch(HLoadString* load_string) {
Vladimir Marko65979462017-05-19 17:25:12 +01004623 HX86ComputeBaseMethodAddress* address = load_string->InputAt(0)->AsX86ComputeBaseMethodAddress();
4624 string_patches_.emplace_back(address,
4625 load_string->GetDexFile(),
4626 load_string->GetStringIndex().index_);
4627 __ Bind(&string_patches_.back().label);
4628}
4629
Vladimir Markoaad75c62016-10-03 08:46:48 +00004630Label* CodeGeneratorX86::NewStringBssEntryPatch(HLoadString* load_string) {
4631 DCHECK(!GetCompilerOptions().IsBootImage());
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004632 HX86ComputeBaseMethodAddress* address =
4633 load_string->InputAt(0)->AsX86ComputeBaseMethodAddress();
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004634 string_bss_entry_patches_.emplace_back(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004635 address, load_string->GetDexFile(), load_string->GetStringIndex().index_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004636 return &string_bss_entry_patches_.back().label;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004637}
4638
Vladimir Markoaad75c62016-10-03 08:46:48 +00004639// The label points to the end of the "movl" or another instruction but the literal offset
4640// for method patch needs to point to the embedded constant which occupies the last 4 bytes.
4641constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
4642
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004643template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +00004644inline void CodeGeneratorX86::EmitPcRelativeLinkerPatches(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004645 const ArenaDeque<X86PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004646 ArenaVector<linker::LinkerPatch>* linker_patches) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004647 for (const X86PcRelativePatchInfo& info : infos) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00004648 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004649 linker_patches->push_back(Factory(
4650 literal_offset, &info.dex_file, GetMethodAddressOffset(info.method_address), info.index));
Vladimir Markoaad75c62016-10-03 08:46:48 +00004651 }
4652}
4653
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004654void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Marko58155012015-08-19 12:49:41 +00004655 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004656 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01004657 boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004658 method_bss_entry_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +00004659 boot_image_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01004660 type_bss_entry_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004661 string_patches_.size() +
4662 string_bss_entry_patches_.size();
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004663 linker_patches->reserve(size);
Vladimir Marko764d4542017-05-16 10:31:41 +01004664 if (GetCompilerOptions().IsBootImage()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004665 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
4666 boot_image_method_patches_, linker_patches);
4667 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
4668 boot_image_type_patches_, linker_patches);
4669 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
4670 string_patches_, linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004671 } else {
Vladimir Marko65979462017-05-19 17:25:12 +01004672 DCHECK(boot_image_method_patches_.empty());
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004673 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeClassTablePatch>(
4674 boot_image_type_patches_, linker_patches);
4675 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringInternTablePatch>(
4676 string_patches_, linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004677 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004678 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
4679 method_bss_entry_patches_, linker_patches);
4680 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
4681 type_bss_entry_patches_, linker_patches);
4682 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
4683 string_bss_entry_patches_, linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00004684 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00004685}
4686
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004687void CodeGeneratorX86::MarkGCCard(Register temp,
4688 Register card,
4689 Register object,
4690 Register value,
4691 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004692 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004693 if (value_can_be_null) {
4694 __ testl(value, value);
4695 __ j(kEqual, &is_null);
4696 }
Andreas Gampe542451c2016-07-26 09:02:02 -07004697 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86PointerSize>().Int32Value()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004698 __ movl(temp, object);
4699 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004700 __ movb(Address(temp, card, TIMES_1, 0),
4701 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004702 if (value_can_be_null) {
4703 __ Bind(&is_null);
4704 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004705}
4706
Calin Juravle52c48962014-12-16 17:02:57 +00004707void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4708 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00004709
4710 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004711 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004712 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004713 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
4714 kEmitCompilerReadBarrier
4715 ? LocationSummary::kCallOnSlowPath
4716 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004717 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004718 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004719 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004720 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004721
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004722 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004723 locations->SetOut(Location::RequiresFpuRegister());
4724 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004725 // The output overlaps in case of long: we don't want the low move
4726 // to overwrite the object's location. Likewise, in the case of
4727 // an object field get with read barriers enabled, we do not want
4728 // the move to overwrite the object's location, as we need it to emit
4729 // the read barrier.
4730 locations->SetOut(
4731 Location::RequiresRegister(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004732 (object_field_get_with_read_barrier || instruction->GetType() == DataType::Type::kInt64) ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00004733 Location::kOutputOverlap :
4734 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004735 }
Calin Juravle52c48962014-12-16 17:02:57 +00004736
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004737 if (field_info.IsVolatile() && (field_info.GetFieldType() == DataType::Type::kInt64)) {
Calin Juravle52c48962014-12-16 17:02:57 +00004738 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00004739 // So we use an XMM register as a temp to achieve atomicity (first
4740 // load the temp into the XMM and then copy the XMM into the
4741 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00004742 locations->AddTemp(Location::RequiresFpuRegister());
4743 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004744}
4745
Calin Juravle52c48962014-12-16 17:02:57 +00004746void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
4747 const FieldInfo& field_info) {
4748 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004749
Calin Juravle52c48962014-12-16 17:02:57 +00004750 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004751 Location base_loc = locations->InAt(0);
4752 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004753 Location out = locations->Out();
4754 bool is_volatile = field_info.IsVolatile();
Vladimir Marko61b92282017-10-11 13:23:17 +01004755 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
4756 DataType::Type load_type = instruction->GetType();
Calin Juravle52c48962014-12-16 17:02:57 +00004757 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4758
Vladimir Marko61b92282017-10-11 13:23:17 +01004759 switch (load_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004760 case DataType::Type::kBool:
4761 case DataType::Type::kUint8: {
Calin Juravle52c48962014-12-16 17:02:57 +00004762 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004763 break;
4764 }
4765
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004766 case DataType::Type::kInt8: {
Calin Juravle52c48962014-12-16 17:02:57 +00004767 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004768 break;
4769 }
4770
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004771 case DataType::Type::kUint16: {
4772 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004773 break;
4774 }
4775
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004776 case DataType::Type::kInt16: {
4777 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004778 break;
4779 }
4780
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004781 case DataType::Type::kInt32:
Calin Juravle52c48962014-12-16 17:02:57 +00004782 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004783 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00004784
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004785 case DataType::Type::kReference: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004786 // /* HeapReference<Object> */ out = *(base + offset)
4787 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004788 // Note that a potential implicit null check is handled in this
4789 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4790 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00004791 instruction, out, base, offset, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00004792 if (is_volatile) {
4793 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4794 }
4795 } else {
4796 __ movl(out.AsRegister<Register>(), Address(base, offset));
4797 codegen_->MaybeRecordImplicitNullCheck(instruction);
4798 if (is_volatile) {
4799 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4800 }
4801 // If read barriers are enabled, emit read barriers other than
4802 // Baker's using a slow path (and also unpoison the loaded
4803 // reference, if heap poisoning is enabled).
4804 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4805 }
4806 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004807 }
4808
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004809 case DataType::Type::kInt64: {
Calin Juravle52c48962014-12-16 17:02:57 +00004810 if (is_volatile) {
4811 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4812 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004813 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004814 __ movd(out.AsRegisterPairLow<Register>(), temp);
4815 __ psrlq(temp, Immediate(32));
4816 __ movd(out.AsRegisterPairHigh<Register>(), temp);
4817 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004818 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00004819 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004820 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004821 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
4822 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004823 break;
4824 }
4825
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004826 case DataType::Type::kFloat32: {
Calin Juravle52c48962014-12-16 17:02:57 +00004827 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004828 break;
4829 }
4830
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004831 case DataType::Type::kFloat64: {
Calin Juravle52c48962014-12-16 17:02:57 +00004832 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004833 break;
4834 }
4835
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004836 case DataType::Type::kVoid:
Vladimir Marko61b92282017-10-11 13:23:17 +01004837 LOG(FATAL) << "Unreachable type " << load_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004838 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004839 }
Calin Juravle52c48962014-12-16 17:02:57 +00004840
Vladimir Marko61b92282017-10-11 13:23:17 +01004841 if (load_type == DataType::Type::kReference || load_type == DataType::Type::kInt64) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004842 // Potential implicit null checks, in the case of reference or
4843 // long fields, are handled in the previous switch statement.
4844 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004845 codegen_->MaybeRecordImplicitNullCheck(instruction);
4846 }
4847
Calin Juravle52c48962014-12-16 17:02:57 +00004848 if (is_volatile) {
Vladimir Marko61b92282017-10-11 13:23:17 +01004849 if (load_type == DataType::Type::kReference) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004850 // Memory barriers, in the case of references, are also handled
4851 // in the previous switch statement.
4852 } else {
4853 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4854 }
Roland Levillain4d027112015-07-01 15:41:14 +01004855 }
Calin Juravle52c48962014-12-16 17:02:57 +00004856}
4857
4858void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4859 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4860
4861 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004862 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00004863 locations->SetInAt(0, Location::RequiresRegister());
4864 bool is_volatile = field_info.IsVolatile();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004865 DataType::Type field_type = field_info.GetFieldType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004866 bool is_byte_type = DataType::Size(field_type) == 1u;
Calin Juravle52c48962014-12-16 17:02:57 +00004867
4868 // The register allocator does not support multiple
4869 // inputs that die at entry with one in a specific register.
4870 if (is_byte_type) {
4871 // Ensure the value is in a byte register.
4872 locations->SetInAt(1, Location::RegisterLocation(EAX));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004873 } else if (DataType::IsFloatingPointType(field_type)) {
4874 if (is_volatile && field_type == DataType::Type::kFloat64) {
Mark Mendell81489372015-11-04 11:30:41 -05004875 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4876 locations->SetInAt(1, Location::RequiresFpuRegister());
4877 } else {
4878 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4879 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004880 } else if (is_volatile && field_type == DataType::Type::kInt64) {
Mark Mendell81489372015-11-04 11:30:41 -05004881 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00004882 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004883
Calin Juravle52c48962014-12-16 17:02:57 +00004884 // 64bits value can be atomically written to an address with movsd and an XMM register.
4885 // We need two XMM registers because there's no easier way to (bit) copy a register pair
4886 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
4887 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
4888 // isolated cases when we need this it isn't worth adding the extra complexity.
4889 locations->AddTemp(Location::RequiresFpuRegister());
4890 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004891 } else {
4892 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4893
4894 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4895 // Temporary registers for the write barrier.
4896 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
4897 // Ensure the card is in a byte register.
4898 locations->AddTemp(Location::RegisterLocation(ECX));
4899 }
Calin Juravle52c48962014-12-16 17:02:57 +00004900 }
4901}
4902
4903void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004904 const FieldInfo& field_info,
4905 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004906 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4907
4908 LocationSummary* locations = instruction->GetLocations();
4909 Register base = locations->InAt(0).AsRegister<Register>();
4910 Location value = locations->InAt(1);
4911 bool is_volatile = field_info.IsVolatile();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004912 DataType::Type field_type = field_info.GetFieldType();
Calin Juravle52c48962014-12-16 17:02:57 +00004913 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004914 bool needs_write_barrier =
4915 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004916
4917 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004918 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004919 }
4920
Mark Mendell81489372015-11-04 11:30:41 -05004921 bool maybe_record_implicit_null_check_done = false;
4922
Calin Juravle52c48962014-12-16 17:02:57 +00004923 switch (field_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004924 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004925 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004926 case DataType::Type::kInt8: {
Calin Juravle52c48962014-12-16 17:02:57 +00004927 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
4928 break;
4929 }
4930
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004931 case DataType::Type::kUint16:
4932 case DataType::Type::kInt16: {
Mark Mendell81489372015-11-04 11:30:41 -05004933 if (value.IsConstant()) {
Nicolas Geoffray78612082017-07-24 14:18:53 +01004934 __ movw(Address(base, offset),
4935 Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Mark Mendell81489372015-11-04 11:30:41 -05004936 } else {
4937 __ movw(Address(base, offset), value.AsRegister<Register>());
4938 }
Calin Juravle52c48962014-12-16 17:02:57 +00004939 break;
4940 }
4941
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004942 case DataType::Type::kInt32:
4943 case DataType::Type::kReference: {
Roland Levillain4d027112015-07-01 15:41:14 +01004944 if (kPoisonHeapReferences && needs_write_barrier) {
4945 // Note that in the case where `value` is a null reference,
4946 // we do not enter this block, as the reference does not
4947 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004948 DCHECK_EQ(field_type, DataType::Type::kReference);
Roland Levillain4d027112015-07-01 15:41:14 +01004949 Register temp = locations->GetTemp(0).AsRegister<Register>();
4950 __ movl(temp, value.AsRegister<Register>());
4951 __ PoisonHeapReference(temp);
4952 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05004953 } else if (value.IsConstant()) {
4954 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4955 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01004956 } else {
Nicolas Geoffray03971632016-03-17 10:44:24 +00004957 DCHECK(value.IsRegister()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01004958 __ movl(Address(base, offset), value.AsRegister<Register>());
4959 }
Calin Juravle52c48962014-12-16 17:02:57 +00004960 break;
4961 }
4962
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004963 case DataType::Type::kInt64: {
Calin Juravle52c48962014-12-16 17:02:57 +00004964 if (is_volatile) {
4965 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4966 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
4967 __ movd(temp1, value.AsRegisterPairLow<Register>());
4968 __ movd(temp2, value.AsRegisterPairHigh<Register>());
4969 __ punpckldq(temp1, temp2);
4970 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00004971 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05004972 } else if (value.IsConstant()) {
4973 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4974 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4975 codegen_->MaybeRecordImplicitNullCheck(instruction);
4976 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00004977 } else {
4978 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004979 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004980 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
4981 }
Mark Mendell81489372015-11-04 11:30:41 -05004982 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00004983 break;
4984 }
4985
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004986 case DataType::Type::kFloat32: {
Mark Mendell81489372015-11-04 11:30:41 -05004987 if (value.IsConstant()) {
4988 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4989 __ movl(Address(base, offset), Immediate(v));
4990 } else {
4991 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4992 }
Calin Juravle52c48962014-12-16 17:02:57 +00004993 break;
4994 }
4995
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004996 case DataType::Type::kFloat64: {
Mark Mendell81489372015-11-04 11:30:41 -05004997 if (value.IsConstant()) {
4998 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4999 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
5000 codegen_->MaybeRecordImplicitNullCheck(instruction);
5001 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
5002 maybe_record_implicit_null_check_done = true;
5003 } else {
5004 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
5005 }
Calin Juravle52c48962014-12-16 17:02:57 +00005006 break;
5007 }
5008
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005009 case DataType::Type::kVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00005010 LOG(FATAL) << "Unreachable type " << field_type;
5011 UNREACHABLE();
5012 }
5013
Mark Mendell81489372015-11-04 11:30:41 -05005014 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005015 codegen_->MaybeRecordImplicitNullCheck(instruction);
5016 }
5017
Roland Levillain4d027112015-07-01 15:41:14 +01005018 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005019 Register temp = locations->GetTemp(0).AsRegister<Register>();
5020 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005021 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00005022 }
5023
Calin Juravle52c48962014-12-16 17:02:57 +00005024 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005025 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00005026 }
5027}
5028
5029void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5030 HandleFieldGet(instruction, instruction->GetFieldInfo());
5031}
5032
5033void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5034 HandleFieldGet(instruction, instruction->GetFieldInfo());
5035}
5036
5037void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5038 HandleFieldSet(instruction, instruction->GetFieldInfo());
5039}
5040
5041void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005042 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005043}
5044
5045void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5046 HandleFieldSet(instruction, instruction->GetFieldInfo());
5047}
5048
5049void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005050 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005051}
5052
5053void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5054 HandleFieldGet(instruction, instruction->GetFieldInfo());
5055}
5056
5057void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5058 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005059}
5060
Calin Juravlee460d1d2015-09-29 04:52:17 +01005061void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
5062 HUnresolvedInstanceFieldGet* instruction) {
5063 FieldAccessCallingConventionX86 calling_convention;
5064 codegen_->CreateUnresolvedFieldLocationSummary(
5065 instruction, instruction->GetFieldType(), calling_convention);
5066}
5067
5068void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
5069 HUnresolvedInstanceFieldGet* instruction) {
5070 FieldAccessCallingConventionX86 calling_convention;
5071 codegen_->GenerateUnresolvedFieldAccess(instruction,
5072 instruction->GetFieldType(),
5073 instruction->GetFieldIndex(),
5074 instruction->GetDexPc(),
5075 calling_convention);
5076}
5077
5078void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
5079 HUnresolvedInstanceFieldSet* instruction) {
5080 FieldAccessCallingConventionX86 calling_convention;
5081 codegen_->CreateUnresolvedFieldLocationSummary(
5082 instruction, instruction->GetFieldType(), calling_convention);
5083}
5084
5085void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
5086 HUnresolvedInstanceFieldSet* instruction) {
5087 FieldAccessCallingConventionX86 calling_convention;
5088 codegen_->GenerateUnresolvedFieldAccess(instruction,
5089 instruction->GetFieldType(),
5090 instruction->GetFieldIndex(),
5091 instruction->GetDexPc(),
5092 calling_convention);
5093}
5094
5095void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
5096 HUnresolvedStaticFieldGet* instruction) {
5097 FieldAccessCallingConventionX86 calling_convention;
5098 codegen_->CreateUnresolvedFieldLocationSummary(
5099 instruction, instruction->GetFieldType(), calling_convention);
5100}
5101
5102void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
5103 HUnresolvedStaticFieldGet* instruction) {
5104 FieldAccessCallingConventionX86 calling_convention;
5105 codegen_->GenerateUnresolvedFieldAccess(instruction,
5106 instruction->GetFieldType(),
5107 instruction->GetFieldIndex(),
5108 instruction->GetDexPc(),
5109 calling_convention);
5110}
5111
5112void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
5113 HUnresolvedStaticFieldSet* instruction) {
5114 FieldAccessCallingConventionX86 calling_convention;
5115 codegen_->CreateUnresolvedFieldLocationSummary(
5116 instruction, instruction->GetFieldType(), calling_convention);
5117}
5118
5119void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
5120 HUnresolvedStaticFieldSet* instruction) {
5121 FieldAccessCallingConventionX86 calling_convention;
5122 codegen_->GenerateUnresolvedFieldAccess(instruction,
5123 instruction->GetFieldType(),
5124 instruction->GetFieldIndex(),
5125 instruction->GetDexPc(),
5126 calling_convention);
5127}
5128
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005129void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005130 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5131 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
5132 ? Location::RequiresRegister()
5133 : Location::Any();
5134 locations->SetInAt(0, loc);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005135}
5136
Calin Juravle2ae48182016-03-16 14:05:09 +00005137void CodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
5138 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005139 return;
5140 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005141 LocationSummary* locations = instruction->GetLocations();
5142 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005143
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005144 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00005145 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005146}
5147
Calin Juravle2ae48182016-03-16 14:05:09 +00005148void CodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005149 SlowPathCode* slow_path = new (GetScopedAllocator()) NullCheckSlowPathX86(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005150 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005151
5152 LocationSummary* locations = instruction->GetLocations();
5153 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005154
5155 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04005156 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005157 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005158 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005159 } else {
5160 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00005161 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005162 __ jmp(slow_path->GetEntryLabel());
5163 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005164 }
5165 __ j(kEqual, slow_path->GetEntryLabel());
5166}
5167
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005168void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005169 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005170}
5171
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005172void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005173 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005174 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005175 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005176 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
5177 object_array_get_with_read_barrier
5178 ? LocationSummary::kCallOnSlowPath
5179 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005180 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005181 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005182 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005183 locations->SetInAt(0, Location::RequiresRegister());
5184 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005185 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005186 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5187 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005188 // The output overlaps in case of long: we don't want the low move
5189 // to overwrite the array's location. Likewise, in the case of an
5190 // object array get with read barriers enabled, we do not want the
5191 // move to overwrite the array's location, as we need it to emit
5192 // the read barrier.
5193 locations->SetOut(
5194 Location::RequiresRegister(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005195 (instruction->GetType() == DataType::Type::kInt64 || object_array_get_with_read_barrier)
5196 ? Location::kOutputOverlap
5197 : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005198 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005199}
5200
5201void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
5202 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005203 Location obj_loc = locations->InAt(0);
5204 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005205 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005206 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005207 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005208
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005209 DataType::Type type = instruction->GetType();
Calin Juravle77520bc2015-01-12 18:45:46 +00005210 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005211 case DataType::Type::kBool:
5212 case DataType::Type::kUint8: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005213 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005214 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005215 break;
5216 }
5217
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005218 case DataType::Type::kInt8: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005219 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005220 __ movsxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005221 break;
5222 }
5223
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005224 case DataType::Type::kUint16: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005225 Register out = out_loc.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07005226 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5227 // Branch cases into compressed and uncompressed for each index's type.
5228 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
5229 NearLabel done, not_compressed;
Vladimir Marko3c89d422017-02-17 11:30:23 +00005230 __ testb(Address(obj, count_offset), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005231 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005232 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
5233 "Expecting 0=compressed, 1=uncompressed");
5234 __ j(kNotZero, &not_compressed);
jessicahandojo4877b792016-09-08 19:49:13 -07005235 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
5236 __ jmp(&done);
5237 __ Bind(&not_compressed);
5238 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5239 __ Bind(&done);
5240 } else {
5241 // Common case for charAt of array of char or when string compression's
5242 // feature is turned off.
5243 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5244 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005245 break;
5246 }
5247
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005248 case DataType::Type::kInt16: {
5249 Register out = out_loc.AsRegister<Register>();
5250 __ movsxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5251 break;
5252 }
5253
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005254 case DataType::Type::kInt32: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005255 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005256 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005257 break;
5258 }
5259
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005260 case DataType::Type::kReference: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005261 static_assert(
5262 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5263 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00005264 // /* HeapReference<Object> */ out =
5265 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5266 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005267 // Note that a potential implicit null check is handled in this
5268 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
5269 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00005270 instruction, out_loc, obj, data_offset, index, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005271 } else {
5272 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005273 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
5274 codegen_->MaybeRecordImplicitNullCheck(instruction);
5275 // If read barriers are enabled, emit read barriers other than
5276 // Baker's using a slow path (and also unpoison the loaded
5277 // reference, if heap poisoning is enabled).
Roland Levillain7c1559a2015-12-15 10:55:36 +00005278 if (index.IsConstant()) {
5279 uint32_t offset =
5280 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005281 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5282 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005283 codegen_->MaybeGenerateReadBarrierSlow(
5284 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5285 }
5286 }
5287 break;
5288 }
5289
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005290 case DataType::Type::kInt64: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005291 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005292 __ movl(out_loc.AsRegisterPairLow<Register>(),
5293 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
5294 codegen_->MaybeRecordImplicitNullCheck(instruction);
5295 __ movl(out_loc.AsRegisterPairHigh<Register>(),
5296 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005297 break;
5298 }
5299
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005300 case DataType::Type::kFloat32: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005301 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005302 __ movss(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005303 break;
5304 }
5305
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005306 case DataType::Type::kFloat64: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005307 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005308 __ movsd(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005309 break;
5310 }
5311
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005312 case DataType::Type::kVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00005313 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005314 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005315 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005316
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005317 if (type == DataType::Type::kReference || type == DataType::Type::kInt64) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005318 // Potential implicit null checks, in the case of reference or
5319 // long arrays, are handled in the previous switch statement.
5320 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005321 codegen_->MaybeRecordImplicitNullCheck(instruction);
5322 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005323}
5324
5325void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005326 DataType::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005327
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005328 bool needs_write_barrier =
5329 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005330 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005331
Vladimir Markoca6fff82017-10-03 14:49:14 +01005332 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Nicolas Geoffray39468442014-09-02 15:17:15 +01005333 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01005334 may_need_runtime_call_for_type_check ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00005335 LocationSummary::kCallOnSlowPath :
5336 LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005337
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005338 bool is_byte_type = DataType::Size(value_type) == 1u;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005339 // We need the inputs to be different than the output in case of long operation.
5340 // In case of a byte operation, the register allocator does not support multiple
5341 // inputs that die at entry with one in a specific register.
5342 locations->SetInAt(0, Location::RequiresRegister());
5343 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5344 if (is_byte_type) {
5345 // Ensure the value is in a byte register.
5346 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005347 } else if (DataType::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05005348 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005349 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005350 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
5351 }
5352 if (needs_write_barrier) {
5353 // Temporary registers for the write barrier.
5354 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5355 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005356 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005357 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005358}
5359
5360void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
5361 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005362 Location array_loc = locations->InAt(0);
5363 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005364 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005365 Location value = locations->InAt(2);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005366 DataType::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005367 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5368 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5369 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005370 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005371 bool needs_write_barrier =
5372 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005373
5374 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005375 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005376 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005377 case DataType::Type::kInt8: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005378 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005379 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_1, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005380 if (value.IsRegister()) {
5381 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005382 } else {
Nicolas Geoffray78612082017-07-24 14:18:53 +01005383 __ movb(address, Immediate(CodeGenerator::GetInt8ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005384 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005385 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005386 break;
5387 }
5388
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005389 case DataType::Type::kUint16:
5390 case DataType::Type::kInt16: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005391 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005392 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_2, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005393 if (value.IsRegister()) {
5394 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005395 } else {
Nicolas Geoffray78612082017-07-24 14:18:53 +01005396 __ movw(address, Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005397 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005398 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005399 break;
5400 }
5401
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005402 case DataType::Type::kReference: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005403 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005404 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005405
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005406 if (!value.IsRegister()) {
5407 // Just setting null.
5408 DCHECK(instruction->InputAt(2)->IsNullConstant());
5409 DCHECK(value.IsConstant()) << value;
5410 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00005411 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005412 DCHECK(!needs_write_barrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005413 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005414 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005415 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005416
5417 DCHECK(needs_write_barrier);
5418 Register register_value = value.AsRegister<Register>();
Roland Levillain16d9f942016-08-25 17:27:56 +01005419 // We cannot use a NearLabel for `done`, as its range may be too
5420 // short when Baker read barriers are enabled.
5421 Label done;
5422 NearLabel not_null, do_put;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005423 SlowPathCode* slow_path = nullptr;
Roland Levillain16d9f942016-08-25 17:27:56 +01005424 Location temp_loc = locations->GetTemp(0);
5425 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005426 if (may_need_runtime_call_for_type_check) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005427 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathX86(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005428 codegen_->AddSlowPath(slow_path);
5429 if (instruction->GetValueCanBeNull()) {
5430 __ testl(register_value, register_value);
5431 __ j(kNotEqual, &not_null);
5432 __ movl(address, Immediate(0));
5433 codegen_->MaybeRecordImplicitNullCheck(instruction);
5434 __ jmp(&done);
5435 __ Bind(&not_null);
5436 }
5437
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005438 // Note that when Baker read barriers are enabled, the type
5439 // checks are performed without read barriers. This is fine,
5440 // even in the case where a class object is in the from-space
5441 // after the flip, as a comparison involving such a type would
5442 // not produce a false positive; it may of course produce a
5443 // false negative, in which case we would take the ArraySet
5444 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01005445
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005446 // /* HeapReference<Class> */ temp = array->klass_
5447 __ movl(temp, Address(array, class_offset));
5448 codegen_->MaybeRecordImplicitNullCheck(instruction);
5449 __ MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01005450
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005451 // /* HeapReference<Class> */ temp = temp->component_type_
5452 __ movl(temp, Address(temp, component_offset));
5453 // If heap poisoning is enabled, no need to unpoison `temp`
5454 // nor the object reference in `register_value->klass`, as
5455 // we are comparing two poisoned references.
5456 __ cmpl(temp, Address(register_value, class_offset));
Roland Levillain16d9f942016-08-25 17:27:56 +01005457
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005458 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5459 __ j(kEqual, &do_put);
5460 // If heap poisoning is enabled, the `temp` reference has
5461 // not been unpoisoned yet; unpoison it now.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005462 __ MaybeUnpoisonHeapReference(temp);
5463
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005464 // If heap poisoning is enabled, no need to unpoison the
5465 // heap reference loaded below, as it is only used for a
5466 // comparison with null.
5467 __ cmpl(Address(temp, super_offset), Immediate(0));
5468 __ j(kNotEqual, slow_path->GetEntryLabel());
5469 __ Bind(&do_put);
5470 } else {
5471 __ j(kNotEqual, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005472 }
5473 }
5474
5475 if (kPoisonHeapReferences) {
5476 __ movl(temp, register_value);
5477 __ PoisonHeapReference(temp);
5478 __ movl(address, temp);
5479 } else {
5480 __ movl(address, register_value);
5481 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005482 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005483 codegen_->MaybeRecordImplicitNullCheck(instruction);
5484 }
5485
5486 Register card = locations->GetTemp(1).AsRegister<Register>();
5487 codegen_->MarkGCCard(
5488 temp, card, array, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
5489 __ Bind(&done);
5490
5491 if (slow_path != nullptr) {
5492 __ Bind(slow_path->GetExitLabel());
5493 }
5494
5495 break;
5496 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005497
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005498 case DataType::Type::kInt32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005499 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005500 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005501 if (value.IsRegister()) {
5502 __ movl(address, value.AsRegister<Register>());
5503 } else {
5504 DCHECK(value.IsConstant()) << value;
5505 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5506 __ movl(address, Immediate(v));
5507 }
5508 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005509 break;
5510 }
5511
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005512 case DataType::Type::kInt64: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005513 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005514 if (value.IsRegisterPair()) {
5515 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
5516 value.AsRegisterPairLow<Register>());
5517 codegen_->MaybeRecordImplicitNullCheck(instruction);
5518 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
5519 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005520 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005521 DCHECK(value.IsConstant());
5522 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
5523 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
5524 Immediate(Low32Bits(val)));
5525 codegen_->MaybeRecordImplicitNullCheck(instruction);
5526 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
5527 Immediate(High32Bits(val)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005528 }
5529 break;
5530 }
5531
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005532 case DataType::Type::kFloat32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005533 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005534 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005535 if (value.IsFpuRegister()) {
5536 __ movss(address, value.AsFpuRegister<XmmRegister>());
5537 } else {
5538 DCHECK(value.IsConstant());
5539 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
5540 __ movl(address, Immediate(v));
5541 }
5542 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005543 break;
5544 }
5545
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005546 case DataType::Type::kFloat64: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005547 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005548 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005549 if (value.IsFpuRegister()) {
5550 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5551 } else {
5552 DCHECK(value.IsConstant());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005553 Address address_hi =
5554 CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset + kX86WordSize);
Mark Mendell81489372015-11-04 11:30:41 -05005555 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5556 __ movl(address, Immediate(Low32Bits(v)));
5557 codegen_->MaybeRecordImplicitNullCheck(instruction);
5558 __ movl(address_hi, Immediate(High32Bits(v)));
5559 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005560 break;
5561 }
5562
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005563 case DataType::Type::kVoid:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005564 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005565 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005566 }
5567}
5568
5569void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005570 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005571 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04005572 if (!instruction->IsEmittedAtUseSite()) {
5573 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5574 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005575}
5576
5577void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04005578 if (instruction->IsEmittedAtUseSite()) {
5579 return;
5580 }
5581
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005582 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005583 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005584 Register obj = locations->InAt(0).AsRegister<Register>();
5585 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005586 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005587 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo4877b792016-09-08 19:49:13 -07005588 // Mask out most significant bit in case the array is String's array of char.
5589 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005590 __ shrl(out, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005591 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005592}
5593
5594void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005595 RegisterSet caller_saves = RegisterSet::Empty();
5596 InvokeRuntimeCallingConvention calling_convention;
5597 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5598 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5599 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005600 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04005601 HInstruction* length = instruction->InputAt(1);
5602 if (!length->IsEmittedAtUseSite()) {
5603 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5604 }
jessicahandojo4877b792016-09-08 19:49:13 -07005605 // Need register to see array's length.
5606 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5607 locations->AddTemp(Location::RequiresRegister());
5608 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005609}
5610
5611void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
jessicahandojo4877b792016-09-08 19:49:13 -07005612 const bool is_string_compressed_char_at =
5613 mirror::kUseStringCompression && instruction->IsStringCharAt();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005614 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005615 Location index_loc = locations->InAt(0);
5616 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07005617 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01005618 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005619
Mark Mendell99dbd682015-04-22 16:18:52 -04005620 if (length_loc.IsConstant()) {
5621 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5622 if (index_loc.IsConstant()) {
5623 // BCE will remove the bounds check if we are guarenteed to pass.
5624 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5625 if (index < 0 || index >= length) {
5626 codegen_->AddSlowPath(slow_path);
5627 __ jmp(slow_path->GetEntryLabel());
5628 } else {
5629 // Some optimization after BCE may have generated this, and we should not
5630 // generate a bounds check if it is a valid range.
5631 }
5632 return;
5633 }
5634
5635 // We have to reverse the jump condition because the length is the constant.
5636 Register index_reg = index_loc.AsRegister<Register>();
5637 __ cmpl(index_reg, Immediate(length));
5638 codegen_->AddSlowPath(slow_path);
5639 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005640 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04005641 HInstruction* array_length = instruction->InputAt(1);
5642 if (array_length->IsEmittedAtUseSite()) {
5643 // Address the length field in the array.
5644 DCHECK(array_length->IsArrayLength());
5645 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
5646 Location array_loc = array_length->GetLocations()->InAt(0);
5647 Address array_len(array_loc.AsRegister<Register>(), len_offset);
jessicahandojo4877b792016-09-08 19:49:13 -07005648 if (is_string_compressed_char_at) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005649 // TODO: if index_loc.IsConstant(), compare twice the index (to compensate for
5650 // the string compression flag) with the in-memory length and avoid the temporary.
jessicahandojo4877b792016-09-08 19:49:13 -07005651 Register length_reg = locations->GetTemp(0).AsRegister<Register>();
5652 __ movl(length_reg, array_len);
5653 codegen_->MaybeRecordImplicitNullCheck(array_length);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005654 __ shrl(length_reg, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005655 codegen_->GenerateIntCompare(length_reg, index_loc);
Mark Mendellee8d9712016-07-12 11:13:15 -04005656 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07005657 // Checking bounds for general case:
5658 // Array of char or string's array with feature compression off.
5659 if (index_loc.IsConstant()) {
5660 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5661 __ cmpl(array_len, Immediate(value));
5662 } else {
5663 __ cmpl(array_len, index_loc.AsRegister<Register>());
5664 }
5665 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendellee8d9712016-07-12 11:13:15 -04005666 }
Mark Mendell99dbd682015-04-22 16:18:52 -04005667 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005668 codegen_->GenerateIntCompare(length_loc, index_loc);
Mark Mendell99dbd682015-04-22 16:18:52 -04005669 }
5670 codegen_->AddSlowPath(slow_path);
5671 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005672 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005673}
5674
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005675void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005676 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005677}
5678
5679void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01005680 if (instruction->GetNext()->IsSuspendCheck() &&
5681 instruction->GetBlock()->GetLoopInformation() != nullptr) {
5682 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
5683 // The back edge will generate the suspend check.
5684 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
5685 }
5686
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005687 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5688}
5689
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005690void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005691 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5692 instruction, LocationSummary::kCallOnSlowPath);
Aart Bikb13c65b2017-03-21 20:14:07 -07005693 // In suspend check slow path, usually there are no caller-save registers at all.
5694 // If SIMD instructions are present, however, we force spilling all live SIMD
5695 // registers in full width (since the runtime only saves/restores lower part).
Aart Bik5576f372017-03-23 16:17:37 -07005696 locations->SetCustomSlowPathCallerSaves(
5697 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005698}
5699
5700void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005701 HBasicBlock* block = instruction->GetBlock();
5702 if (block->GetLoopInformation() != nullptr) {
5703 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5704 // The back edge will generate the suspend check.
5705 return;
5706 }
5707 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5708 // The goto will generate the suspend check.
5709 return;
5710 }
5711 GenerateSuspendCheck(instruction, nullptr);
5712}
5713
5714void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
5715 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005716 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005717 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
5718 if (slow_path == nullptr) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005719 slow_path =
5720 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005721 instruction->SetSlowPath(slow_path);
5722 codegen_->AddSlowPath(slow_path);
5723 if (successor != nullptr) {
5724 DCHECK(successor->IsLoopHeader());
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005725 }
5726 } else {
5727 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5728 }
5729
Andreas Gampe542451c2016-07-26 09:02:02 -07005730 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86PointerSize>().Int32Value()),
Roland Levillain7c1559a2015-12-15 10:55:36 +00005731 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005732 if (successor == nullptr) {
5733 __ j(kNotEqual, slow_path->GetEntryLabel());
5734 __ Bind(slow_path->GetReturnLabel());
5735 } else {
5736 __ j(kEqual, codegen_->GetLabelOf(successor));
5737 __ jmp(slow_path->GetEntryLabel());
5738 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005739}
5740
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005741X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
5742 return codegen_->GetAssembler();
5743}
5744
Aart Bikcfe50bb2017-12-12 14:54:12 -08005745void ParallelMoveResolverX86::MoveMemoryToMemory(int dst, int src, int number_of_words) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005746 ScratchRegisterScope ensure_scratch(
5747 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5748 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5749 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
Mark Mendell7c8d0092015-01-26 11:21:33 -05005750
Aart Bikcfe50bb2017-12-12 14:54:12 -08005751 // Now that temp register is available (possibly spilled), move blocks of memory.
5752 for (int i = 0; i < number_of_words; i++) {
5753 __ movl(temp_reg, Address(ESP, src + stack_offset));
5754 __ movl(Address(ESP, dst + stack_offset), temp_reg);
5755 stack_offset += kX86WordSize;
5756 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005757}
5758
5759void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005760 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005761 Location source = move->GetSource();
5762 Location destination = move->GetDestination();
5763
5764 if (source.IsRegister()) {
5765 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005766 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005767 } else if (destination.IsFpuRegister()) {
5768 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005769 } else {
5770 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005771 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005772 }
David Brazdil74eb1b22015-12-14 11:44:01 +00005773 } else if (source.IsRegisterPair()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005774 size_t elem_size = DataType::Size(DataType::Type::kInt32);
David Brazdil74eb1b22015-12-14 11:44:01 +00005775 // Create stack space for 2 elements.
5776 __ subl(ESP, Immediate(2 * elem_size));
5777 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
5778 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
5779 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
5780 // And remove the temporary stack space we allocated.
5781 __ addl(ESP, Immediate(2 * elem_size));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005782 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005783 if (destination.IsRegister()) {
5784 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
5785 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005786 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005787 } else if (destination.IsRegisterPair()) {
5788 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
5789 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
5790 __ psrlq(src_reg, Immediate(32));
5791 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005792 } else if (destination.IsStackSlot()) {
5793 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07005794 } else if (destination.IsDoubleStackSlot()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005795 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07005796 } else {
5797 DCHECK(destination.IsSIMDStackSlot());
5798 __ movups(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005799 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005800 } else if (source.IsStackSlot()) {
5801 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005802 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005803 } else if (destination.IsFpuRegister()) {
5804 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005805 } else {
5806 DCHECK(destination.IsStackSlot());
Aart Bikcfe50bb2017-12-12 14:54:12 -08005807 MoveMemoryToMemory(destination.GetStackIndex(), source.GetStackIndex(), 1);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005808 }
5809 } else if (source.IsDoubleStackSlot()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005810 if (destination.IsRegisterPair()) {
5811 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
5812 __ movl(destination.AsRegisterPairHigh<Register>(),
5813 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
5814 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005815 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
5816 } else {
5817 DCHECK(destination.IsDoubleStackSlot()) << destination;
Aart Bikcfe50bb2017-12-12 14:54:12 -08005818 MoveMemoryToMemory(destination.GetStackIndex(), source.GetStackIndex(), 2);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005819 }
Aart Bik5576f372017-03-23 16:17:37 -07005820 } else if (source.IsSIMDStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08005821 if (destination.IsFpuRegister()) {
5822 __ movups(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
5823 } else {
5824 DCHECK(destination.IsSIMDStackSlot());
5825 MoveMemoryToMemory(destination.GetStackIndex(), source.GetStackIndex(), 4);
5826 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005827 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005828 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005829 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005830 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005831 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005832 if (value == 0) {
5833 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
5834 } else {
5835 __ movl(destination.AsRegister<Register>(), Immediate(value));
5836 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005837 } else {
5838 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05005839 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005840 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005841 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005842 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005843 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005844 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005845 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005846 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5847 if (value == 0) {
5848 // Easy handling of 0.0.
5849 __ xorps(dest, dest);
5850 } else {
5851 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005852 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5853 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
5854 __ movl(temp, Immediate(value));
5855 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005856 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005857 } else {
5858 DCHECK(destination.IsStackSlot()) << destination;
5859 __ movl(Address(ESP, destination.GetStackIndex()), imm);
5860 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005861 } else if (constant->IsLongConstant()) {
5862 int64_t value = constant->AsLongConstant()->GetValue();
5863 int32_t low_value = Low32Bits(value);
5864 int32_t high_value = High32Bits(value);
5865 Immediate low(low_value);
5866 Immediate high(high_value);
5867 if (destination.IsDoubleStackSlot()) {
5868 __ movl(Address(ESP, destination.GetStackIndex()), low);
5869 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5870 } else {
5871 __ movl(destination.AsRegisterPairLow<Register>(), low);
5872 __ movl(destination.AsRegisterPairHigh<Register>(), high);
5873 }
5874 } else {
5875 DCHECK(constant->IsDoubleConstant());
5876 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005877 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005878 int32_t low_value = Low32Bits(value);
5879 int32_t high_value = High32Bits(value);
5880 Immediate low(low_value);
5881 Immediate high(high_value);
5882 if (destination.IsFpuRegister()) {
5883 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5884 if (value == 0) {
5885 // Easy handling of 0.0.
5886 __ xorpd(dest, dest);
5887 } else {
5888 __ pushl(high);
5889 __ pushl(low);
5890 __ movsd(dest, Address(ESP, 0));
5891 __ addl(ESP, Immediate(8));
5892 }
5893 } else {
5894 DCHECK(destination.IsDoubleStackSlot()) << destination;
5895 __ movl(Address(ESP, destination.GetStackIndex()), low);
5896 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5897 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005898 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005899 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005900 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005901 }
5902}
5903
Mark Mendella5c19ce2015-04-01 12:51:05 -04005904void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005905 Register suggested_scratch = reg == EAX ? EBX : EAX;
5906 ScratchRegisterScope ensure_scratch(
5907 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
5908
5909 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5910 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
5911 __ movl(Address(ESP, mem + stack_offset), reg);
5912 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005913}
5914
Mark Mendell7c8d0092015-01-26 11:21:33 -05005915void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005916 ScratchRegisterScope ensure_scratch(
5917 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5918
5919 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5920 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5921 __ movl(temp_reg, Address(ESP, mem + stack_offset));
5922 __ movss(Address(ESP, mem + stack_offset), reg);
5923 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005924}
5925
Aart Bikcfe50bb2017-12-12 14:54:12 -08005926void ParallelMoveResolverX86::Exchange128(XmmRegister reg, int mem) {
5927 size_t extra_slot = 4 * kX86WordSize;
5928 __ subl(ESP, Immediate(extra_slot));
5929 __ movups(Address(ESP, 0), XmmRegister(reg));
5930 ExchangeMemory(0, mem + extra_slot, 4);
5931 __ movups(XmmRegister(reg), Address(ESP, 0));
5932 __ addl(ESP, Immediate(extra_slot));
5933}
5934
5935void ParallelMoveResolverX86::ExchangeMemory(int mem1, int mem2, int number_of_words) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005936 ScratchRegisterScope ensure_scratch1(
5937 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005938
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005939 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
5940 ScratchRegisterScope ensure_scratch2(
5941 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005942
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005943 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
5944 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
Aart Bikcfe50bb2017-12-12 14:54:12 -08005945
5946 // Now that temp registers are available (possibly spilled), exchange blocks of memory.
5947 for (int i = 0; i < number_of_words; i++) {
5948 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
5949 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
5950 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
5951 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
5952 stack_offset += kX86WordSize;
5953 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005954}
5955
5956void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005957 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005958 Location source = move->GetSource();
5959 Location destination = move->GetDestination();
5960
5961 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04005962 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
5963 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
5964 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
5965 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
5966 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005967 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005968 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005969 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005970 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005971 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08005972 ExchangeMemory(destination.GetStackIndex(), source.GetStackIndex(), 1);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005973 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
5974 // Use XOR Swap algorithm to avoid a temporary.
5975 DCHECK_NE(source.reg(), destination.reg());
5976 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5977 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5978 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5979 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
5980 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
5981 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
5982 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005983 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
5984 // Take advantage of the 16 bytes in the XMM register.
5985 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
5986 Address stack(ESP, destination.GetStackIndex());
5987 // Load the double into the high doubleword.
5988 __ movhpd(reg, stack);
5989
5990 // Store the low double into the destination.
5991 __ movsd(stack, reg);
5992
5993 // Move the high double to the low double.
5994 __ psrldq(reg, Immediate(8));
5995 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
5996 // Take advantage of the 16 bytes in the XMM register.
5997 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
5998 Address stack(ESP, source.GetStackIndex());
5999 // Load the double into the high doubleword.
6000 __ movhpd(reg, stack);
6001
6002 // Store the low double into the destination.
6003 __ movsd(stack, reg);
6004
6005 // Move the high double to the low double.
6006 __ psrldq(reg, Immediate(8));
6007 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08006008 ExchangeMemory(destination.GetStackIndex(), source.GetStackIndex(), 2);
6009 } else if (source.IsSIMDStackSlot() && destination.IsSIMDStackSlot()) {
6010 ExchangeMemory(destination.GetStackIndex(), source.GetStackIndex(), 4);
6011 } else if (source.IsFpuRegister() && destination.IsSIMDStackSlot()) {
6012 Exchange128(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
6013 } else if (destination.IsFpuRegister() && source.IsSIMDStackSlot()) {
6014 Exchange128(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006015 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006016 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006017 }
6018}
6019
6020void ParallelMoveResolverX86::SpillScratch(int reg) {
6021 __ pushl(static_cast<Register>(reg));
6022}
6023
6024void ParallelMoveResolverX86::RestoreScratch(int reg) {
6025 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006026}
6027
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006028HLoadClass::LoadKind CodeGeneratorX86::GetSupportedLoadClassKind(
6029 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006030 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006031 case HLoadClass::LoadKind::kInvalid:
6032 LOG(FATAL) << "UNREACHABLE";
6033 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006034 case HLoadClass::LoadKind::kReferrersClass:
6035 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006036 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006037 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006038 case HLoadClass::LoadKind::kBssEntry:
Vladimir Marko764d4542017-05-16 10:31:41 +01006039 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006040 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006041 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006042 DCHECK(Runtime::Current()->UseJitCompilation());
6043 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01006044 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006045 case HLoadClass::LoadKind::kRuntimeCall:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006046 break;
6047 }
6048 return desired_class_load_kind;
6049}
6050
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006051void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00006052 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006053 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006054 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00006055 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006056 cls,
6057 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00006058 Location::RegisterLocation(EAX));
Vladimir Markoea4c1262017-02-06 19:59:33 +00006059 DCHECK_EQ(calling_convention.GetRegisterAt(0), EAX);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006060 return;
6061 }
Vladimir Marko41559982017-01-06 14:04:23 +00006062 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006063
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006064 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
6065 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006066 ? LocationSummary::kCallOnSlowPath
6067 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01006068 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006069 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006070 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006071 }
6072
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006073 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006074 load_kind == HLoadClass::LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006075 load_kind == HLoadClass::LoadKind::kBootImageClassTable ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006076 load_kind == HLoadClass::LoadKind::kBssEntry) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006077 locations->SetInAt(0, Location::RequiresRegister());
6078 }
6079 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006080 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
6081 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6082 // Rely on the type resolution and/or initialization to save everything.
6083 RegisterSet caller_saves = RegisterSet::Empty();
6084 InvokeRuntimeCallingConvention calling_convention;
6085 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6086 locations->SetCustomSlowPathCallerSaves(caller_saves);
6087 } else {
6088 // For non-Baker read barrier we have a temp-clobbering call.
6089 }
6090 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006091}
6092
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006093Label* CodeGeneratorX86::NewJitRootClassPatch(const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01006094 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006095 Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006096 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006097 // Add a patch entry and return the label.
Vladimir Marko174b2e22017-10-12 13:34:49 +01006098 jit_class_patches_.emplace_back(dex_file, type_index.index_);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006099 PatchInfo<Label>* info = &jit_class_patches_.back();
6100 return &info->label;
6101}
6102
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006103// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6104// move.
6105void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00006106 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006107 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00006108 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01006109 return;
6110 }
Vladimir Marko41559982017-01-06 14:04:23 +00006111 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01006112
Vladimir Marko41559982017-01-06 14:04:23 +00006113 LocationSummary* locations = cls->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006114 Location out_loc = locations->Out();
6115 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006116
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006117 bool generate_null_check = false;
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006118 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
6119 ? kWithoutReadBarrier
6120 : kCompilerReadBarrierOption;
Vladimir Marko41559982017-01-06 14:04:23 +00006121 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006122 case HLoadClass::LoadKind::kReferrersClass: {
6123 DCHECK(!cls->CanCallRuntime());
6124 DCHECK(!cls->MustGenerateClinitCheck());
6125 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6126 Register current_method = locations->InAt(0).AsRegister<Register>();
6127 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006128 cls,
6129 out_loc,
6130 Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()),
Roland Levillain00468f32016-10-27 18:02:48 +01006131 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006132 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006133 break;
6134 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006135 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006136 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006137 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006138 Register method_address = locations->InAt(0).AsRegister<Register>();
6139 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Marko1998cd02017-01-13 13:02:58 +00006140 codegen_->RecordBootTypePatch(cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006141 break;
6142 }
6143 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006144 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006145 uint32_t address = dchecked_integral_cast<uint32_t>(
6146 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
6147 DCHECK_NE(address, 0u);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006148 __ movl(out, Immediate(address));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006149 break;
6150 }
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006151 case HLoadClass::LoadKind::kBootImageClassTable: {
6152 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6153 Register method_address = locations->InAt(0).AsRegister<Register>();
6154 __ movl(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
6155 codegen_->RecordBootTypePatch(cls);
6156 // Extract the reference from the slot data, i.e. clear the hash bits.
6157 int32_t masked_hash = ClassTable::TableSlot::MaskHash(
6158 ComputeModifiedUtf8Hash(cls->GetDexFile().StringByTypeIdx(cls->GetTypeIndex())));
6159 if (masked_hash != 0) {
6160 __ subl(out, Immediate(masked_hash));
6161 }
6162 break;
6163 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006164 case HLoadClass::LoadKind::kBssEntry: {
6165 Register method_address = locations->InAt(0).AsRegister<Register>();
6166 Address address(method_address, CodeGeneratorX86::kDummy32BitOffset);
6167 Label* fixup_label = codegen_->NewTypeBssEntryPatch(cls);
6168 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
6169 generate_null_check = true;
6170 break;
6171 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006172 case HLoadClass::LoadKind::kJitTableAddress: {
6173 Address address = Address::Absolute(CodeGeneratorX86::kDummy32BitOffset);
6174 Label* fixup_label = codegen_->NewJitRootClassPatch(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006175 cls->GetDexFile(), cls->GetTypeIndex(), cls->GetClass());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006176 // /* GcRoot<mirror::Class> */ out = *address
Vladimir Markoea4c1262017-02-06 19:59:33 +00006177 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006178 break;
6179 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006180 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006181 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00006182 LOG(FATAL) << "UNREACHABLE";
6183 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006184 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006185
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006186 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6187 DCHECK(cls->CanCallRuntime());
Vladimir Marko174b2e22017-10-12 13:34:49 +01006188 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathX86(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006189 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
6190 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006191
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006192 if (generate_null_check) {
6193 __ testl(out, out);
6194 __ j(kEqual, slow_path->GetEntryLabel());
6195 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006196
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006197 if (cls->MustGenerateClinitCheck()) {
6198 GenerateClassInitializationCheck(slow_path, out);
6199 } else {
6200 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006201 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006202 }
6203}
6204
6205void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
6206 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006207 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006208 locations->SetInAt(0, Location::RequiresRegister());
6209 if (check->HasUses()) {
6210 locations->SetOut(Location::SameAsFirstInput());
6211 }
6212}
6213
6214void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006215 // We assume the class to not be null.
Vladimir Marko174b2e22017-10-12 13:34:49 +01006216 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006217 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006218 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00006219 GenerateClassInitializationCheck(slow_path,
6220 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006221}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006222
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006223void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07006224 SlowPathCode* slow_path, Register class_reg) {
Vladimir Markodc682aa2018-01-04 18:42:57 +00006225 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
6226 const size_t status_byte_offset =
6227 mirror::Class::StatusOffset().SizeValue() + (status_lsb_position / kBitsPerByte);
6228 constexpr uint32_t shifted_initialized_value =
6229 enum_cast<uint32_t>(ClassStatus::kInitialized) << (status_lsb_position % kBitsPerByte);
6230
6231 __ cmpb(Address(class_reg, status_byte_offset), Immediate(shifted_initialized_value));
Vladimir Marko2c64a832018-01-04 11:31:56 +00006232 __ j(kBelow, slow_path->GetEntryLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006233 __ Bind(slow_path->GetExitLabel());
6234 // No need for memory fence, thanks to the X86 memory model.
6235}
6236
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006237HLoadString::LoadKind CodeGeneratorX86::GetSupportedLoadStringKind(
6238 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006239 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006240 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006241 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00006242 case HLoadString::LoadKind::kBssEntry:
Vladimir Marko764d4542017-05-16 10:31:41 +01006243 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006244 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006245 case HLoadString::LoadKind::kJitTableAddress:
6246 DCHECK(Runtime::Current()->UseJitCompilation());
6247 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01006248 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006249 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006250 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006251 }
6252 return desired_string_load_kind;
6253}
6254
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006255void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006256 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01006257 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006258 HLoadString::LoadKind load_kind = load->GetLoadKind();
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006259 if (load_kind == HLoadString::LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006260 load_kind == HLoadString::LoadKind::kBootImageInternTable ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00006261 load_kind == HLoadString::LoadKind::kBssEntry) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006262 locations->SetInAt(0, Location::RequiresRegister());
6263 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006264 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006265 locations->SetOut(Location::RegisterLocation(EAX));
6266 } else {
6267 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006268 if (load_kind == HLoadString::LoadKind::kBssEntry) {
6269 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00006270 // Rely on the pResolveString to save everything.
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006271 RegisterSet caller_saves = RegisterSet::Empty();
6272 InvokeRuntimeCallingConvention calling_convention;
6273 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6274 locations->SetCustomSlowPathCallerSaves(caller_saves);
6275 } else {
6276 // For non-Baker read barrier we have a temp-clobbering call.
6277 }
6278 }
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006279 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006280}
6281
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006282Label* CodeGeneratorX86::NewJitRootStringPatch(const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01006283 dex::StringIndex string_index,
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006284 Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006285 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006286 // Add a patch entry and return the label.
Vladimir Marko174b2e22017-10-12 13:34:49 +01006287 jit_string_patches_.emplace_back(dex_file, string_index.index_);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006288 PatchInfo<Label>* info = &jit_string_patches_.back();
6289 return &info->label;
6290}
6291
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006292// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6293// move.
6294void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006295 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006296 Location out_loc = locations->Out();
6297 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006298
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006299 switch (load->GetLoadKind()) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006300 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006301 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006302 Register method_address = locations->InAt(0).AsRegister<Register>();
6303 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Markoaad75c62016-10-03 08:46:48 +00006304 codegen_->RecordBootStringPatch(load);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006305 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006306 }
6307 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006308 uint32_t address = dchecked_integral_cast<uint32_t>(
6309 reinterpret_cast<uintptr_t>(load->GetString().Get()));
6310 DCHECK_NE(address, 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006311 __ movl(out, Immediate(address));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006312 return;
6313 }
6314 case HLoadString::LoadKind::kBootImageInternTable: {
6315 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6316 Register method_address = locations->InAt(0).AsRegister<Register>();
6317 __ movl(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
6318 codegen_->RecordBootStringPatch(load);
6319 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006320 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00006321 case HLoadString::LoadKind::kBssEntry: {
6322 Register method_address = locations->InAt(0).AsRegister<Register>();
6323 Address address = Address(method_address, CodeGeneratorX86::kDummy32BitOffset);
6324 Label* fixup_label = codegen_->NewStringBssEntryPatch(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006325 // /* GcRoot<mirror::String> */ out = *address /* PC-relative */
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006326 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
Vladimir Marko174b2e22017-10-12 13:34:49 +01006327 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadStringSlowPathX86(load);
Vladimir Markoaad75c62016-10-03 08:46:48 +00006328 codegen_->AddSlowPath(slow_path);
6329 __ testl(out, out);
6330 __ j(kEqual, slow_path->GetEntryLabel());
6331 __ Bind(slow_path->GetExitLabel());
6332 return;
6333 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006334 case HLoadString::LoadKind::kJitTableAddress: {
6335 Address address = Address::Absolute(CodeGeneratorX86::kDummy32BitOffset);
6336 Label* fixup_label = codegen_->NewJitRootStringPatch(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006337 load->GetDexFile(), load->GetStringIndex(), load->GetString());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006338 // /* GcRoot<mirror::String> */ out = *address
6339 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
6340 return;
6341 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006342 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006343 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006344 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006345
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006346 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006347 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006348 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006349 __ movl(calling_convention.GetRegisterAt(0), Immediate(load->GetStringIndex().index_));
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006350 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6351 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006352}
6353
David Brazdilcb1c0552015-08-04 16:22:25 +01006354static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006355 return Address::Absolute(Thread::ExceptionOffset<kX86PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01006356}
6357
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006358void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
6359 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006360 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006361 locations->SetOut(Location::RequiresRegister());
6362}
6363
6364void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01006365 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
6366}
6367
6368void LocationsBuilderX86::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006369 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
David Brazdilcb1c0552015-08-04 16:22:25 +01006370}
6371
6372void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6373 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006374}
6375
6376void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006377 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6378 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006379 InvokeRuntimeCallingConvention calling_convention;
6380 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6381}
6382
6383void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006384 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006385 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006386}
6387
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006388// Temp is used for read barrier.
6389static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
6390 if (kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00006391 !kUseBakerReadBarrier &&
6392 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain7c1559a2015-12-15 10:55:36 +00006393 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006394 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
6395 return 1;
6396 }
6397 return 0;
6398}
6399
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006400// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006401// interface pointer, one for loading the current interface.
6402// The other checks have one temp for loading the object's class.
6403static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
Vladimir Markoe619f6c2017-12-12 16:00:01 +00006404 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006405 return 2;
6406 }
6407 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006408}
6409
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006410void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006411 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006412 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01006413 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006414 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006415 case TypeCheckKind::kExactCheck:
6416 case TypeCheckKind::kAbstractClassCheck:
6417 case TypeCheckKind::kClassHierarchyCheck:
Vladimir Marko87584542017-12-12 17:47:52 +00006418 case TypeCheckKind::kArrayObjectCheck: {
6419 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
6420 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
6421 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006422 break;
Vladimir Marko87584542017-12-12 17:47:52 +00006423 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006424 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006425 case TypeCheckKind::kUnresolvedCheck:
6426 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006427 call_kind = LocationSummary::kCallOnSlowPath;
6428 break;
6429 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006430
Vladimir Markoca6fff82017-10-03 14:49:14 +01006431 LocationSummary* locations =
6432 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01006433 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006434 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006435 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006436 locations->SetInAt(0, Location::RequiresRegister());
6437 locations->SetInAt(1, Location::Any());
6438 // Note that TypeCheckSlowPathX86 uses this "out" register too.
6439 locations->SetOut(Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006440 // When read barriers are enabled, we need a temporary register for some cases.
6441 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006442}
6443
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006444void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006445 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006446 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006447 Location obj_loc = locations->InAt(0);
6448 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006449 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006450 Location out_loc = locations->Out();
6451 Register out = out_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006452 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
6453 DCHECK_LE(num_temps, 1u);
6454 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006455 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006456 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6457 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6458 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07006459 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006460 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006461
6462 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006463 // Avoid null check if we know obj is not null.
6464 if (instruction->MustDoNullCheck()) {
6465 __ testl(obj, obj);
6466 __ j(kEqual, &zero);
6467 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006468
Roland Levillain7c1559a2015-12-15 10:55:36 +00006469 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006470 case TypeCheckKind::kExactCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00006471 ReadBarrierOption read_barrier_option =
6472 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006473 // /* HeapReference<Class> */ out = obj->klass_
6474 GenerateReferenceLoadTwoRegisters(instruction,
6475 out_loc,
6476 obj_loc,
6477 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00006478 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006479 if (cls.IsRegister()) {
6480 __ cmpl(out, cls.AsRegister<Register>());
6481 } else {
6482 DCHECK(cls.IsStackSlot()) << cls;
6483 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6484 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006485
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006486 // Classes must be equal for the instanceof to succeed.
6487 __ j(kNotEqual, &zero);
6488 __ movl(out, Immediate(1));
6489 __ jmp(&done);
6490 break;
6491 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006492
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006493 case TypeCheckKind::kAbstractClassCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00006494 ReadBarrierOption read_barrier_option =
6495 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006496 // /* HeapReference<Class> */ out = obj->klass_
6497 GenerateReferenceLoadTwoRegisters(instruction,
6498 out_loc,
6499 obj_loc,
6500 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00006501 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006502 // If the class is abstract, we eagerly fetch the super class of the
6503 // object to avoid doing a comparison we know will fail.
6504 NearLabel loop;
6505 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006506 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006507 GenerateReferenceLoadOneRegister(instruction,
6508 out_loc,
6509 super_offset,
6510 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00006511 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006512 __ testl(out, out);
6513 // If `out` is null, we use it for the result, and jump to `done`.
6514 __ j(kEqual, &done);
6515 if (cls.IsRegister()) {
6516 __ cmpl(out, cls.AsRegister<Register>());
6517 } else {
6518 DCHECK(cls.IsStackSlot()) << cls;
6519 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6520 }
6521 __ j(kNotEqual, &loop);
6522 __ movl(out, Immediate(1));
6523 if (zero.IsLinked()) {
6524 __ jmp(&done);
6525 }
6526 break;
6527 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006528
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006529 case TypeCheckKind::kClassHierarchyCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00006530 ReadBarrierOption read_barrier_option =
6531 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006532 // /* HeapReference<Class> */ out = obj->klass_
6533 GenerateReferenceLoadTwoRegisters(instruction,
6534 out_loc,
6535 obj_loc,
6536 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00006537 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006538 // Walk over the class hierarchy to find a match.
6539 NearLabel loop, success;
6540 __ Bind(&loop);
6541 if (cls.IsRegister()) {
6542 __ cmpl(out, cls.AsRegister<Register>());
6543 } else {
6544 DCHECK(cls.IsStackSlot()) << cls;
6545 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6546 }
6547 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006548 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006549 GenerateReferenceLoadOneRegister(instruction,
6550 out_loc,
6551 super_offset,
6552 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00006553 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006554 __ testl(out, out);
6555 __ j(kNotEqual, &loop);
6556 // If `out` is null, we use it for the result, and jump to `done`.
6557 __ jmp(&done);
6558 __ Bind(&success);
6559 __ movl(out, Immediate(1));
6560 if (zero.IsLinked()) {
6561 __ jmp(&done);
6562 }
6563 break;
6564 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006565
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006566 case TypeCheckKind::kArrayObjectCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00006567 ReadBarrierOption read_barrier_option =
6568 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006569 // /* HeapReference<Class> */ out = obj->klass_
6570 GenerateReferenceLoadTwoRegisters(instruction,
6571 out_loc,
6572 obj_loc,
6573 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00006574 read_barrier_option);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006575 // Do an exact check.
6576 NearLabel exact_check;
6577 if (cls.IsRegister()) {
6578 __ cmpl(out, cls.AsRegister<Register>());
6579 } else {
6580 DCHECK(cls.IsStackSlot()) << cls;
6581 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6582 }
6583 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006584 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006585 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006586 GenerateReferenceLoadOneRegister(instruction,
6587 out_loc,
6588 component_offset,
6589 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00006590 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006591 __ testl(out, out);
6592 // If `out` is null, we use it for the result, and jump to `done`.
6593 __ j(kEqual, &done);
6594 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
6595 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006596 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006597 __ movl(out, Immediate(1));
6598 __ jmp(&done);
6599 break;
6600 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006601
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006602 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006603 // No read barrier since the slow path will retry upon failure.
6604 // /* HeapReference<Class> */ out = obj->klass_
6605 GenerateReferenceLoadTwoRegisters(instruction,
6606 out_loc,
6607 obj_loc,
6608 class_offset,
6609 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006610 if (cls.IsRegister()) {
6611 __ cmpl(out, cls.AsRegister<Register>());
6612 } else {
6613 DCHECK(cls.IsStackSlot()) << cls;
6614 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6615 }
6616 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01006617 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
6618 instruction, /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006619 codegen_->AddSlowPath(slow_path);
6620 __ j(kNotEqual, slow_path->GetEntryLabel());
6621 __ movl(out, Immediate(1));
6622 if (zero.IsLinked()) {
6623 __ jmp(&done);
6624 }
6625 break;
6626 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006627
Calin Juravle98893e12015-10-02 21:05:03 +01006628 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006629 case TypeCheckKind::kInterfaceCheck: {
6630 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006631 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00006632 // cases.
6633 //
6634 // We cannot directly call the InstanceofNonTrivial runtime
6635 // entry point without resorting to a type checking slow path
6636 // here (i.e. by calling InvokeRuntime directly), as it would
6637 // require to assign fixed registers for the inputs of this
6638 // HInstanceOf instruction (following the runtime calling
6639 // convention), which might be cluttered by the potential first
6640 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006641 //
6642 // TODO: Introduce a new runtime entry point taking the object
6643 // to test (instead of its class) as argument, and let it deal
6644 // with the read barrier issues. This will let us refactor this
6645 // case of the `switch` code as it was previously (with a direct
6646 // call to the runtime not using a type checking slow path).
6647 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006648 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01006649 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
6650 instruction, /* is_fatal */ false);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006651 codegen_->AddSlowPath(slow_path);
6652 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006653 if (zero.IsLinked()) {
6654 __ jmp(&done);
6655 }
6656 break;
6657 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006658 }
6659
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006660 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006661 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006662 __ xorl(out, out);
6663 }
6664
6665 if (done.IsLinked()) {
6666 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006667 }
6668
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006669 if (slow_path != nullptr) {
6670 __ Bind(slow_path->GetExitLabel());
6671 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006672}
6673
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006674void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006675 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko87584542017-12-12 17:47:52 +00006676 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01006677 LocationSummary* locations =
6678 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006679 locations->SetInAt(0, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006680 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
6681 // Require a register for the interface check since there is a loop that compares the class to
6682 // a memory address.
6683 locations->SetInAt(1, Location::RequiresRegister());
6684 } else {
6685 locations->SetInAt(1, Location::Any());
6686 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006687 // Note that TypeCheckSlowPathX86 uses this "temp" register too.
6688 locations->AddTemp(Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006689 // When read barriers are enabled, we need an additional temporary register for some cases.
6690 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
6691}
6692
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006693void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006694 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006695 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006696 Location obj_loc = locations->InAt(0);
6697 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006698 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006699 Location temp_loc = locations->GetTemp(0);
6700 Register temp = temp_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006701 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
6702 DCHECK_GE(num_temps, 1u);
6703 DCHECK_LE(num_temps, 2u);
6704 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
6705 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6706 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6707 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6708 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
6709 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
6710 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
6711 const uint32_t object_array_data_offset =
6712 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006713
Vladimir Marko87584542017-12-12 17:47:52 +00006714 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006715 SlowPathCode* type_check_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006716 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
6717 instruction, is_type_check_slow_path_fatal);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006718 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006719
Roland Levillain0d5a2812015-11-13 10:07:31 +00006720 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006721 // Avoid null check if we know obj is not null.
6722 if (instruction->MustDoNullCheck()) {
6723 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006724 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006725 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006726
Roland Levillain0d5a2812015-11-13 10:07:31 +00006727 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006728 case TypeCheckKind::kExactCheck:
6729 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006730 // /* HeapReference<Class> */ temp = obj->klass_
6731 GenerateReferenceLoadTwoRegisters(instruction,
6732 temp_loc,
6733 obj_loc,
6734 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006735 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006736
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006737 if (cls.IsRegister()) {
6738 __ cmpl(temp, cls.AsRegister<Register>());
6739 } else {
6740 DCHECK(cls.IsStackSlot()) << cls;
6741 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6742 }
6743 // Jump to slow path for throwing the exception or doing a
6744 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006745 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006746 break;
6747 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006748
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006749 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006750 // /* HeapReference<Class> */ temp = obj->klass_
6751 GenerateReferenceLoadTwoRegisters(instruction,
6752 temp_loc,
6753 obj_loc,
6754 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006755 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006756
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006757 // If the class is abstract, we eagerly fetch the super class of the
6758 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006759 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006760 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006761 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006762 GenerateReferenceLoadOneRegister(instruction,
6763 temp_loc,
6764 super_offset,
6765 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006766 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006767
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006768 // If the class reference currently in `temp` is null, jump to the slow path to throw the
6769 // exception.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006770 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006771 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006772
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006773 // Otherwise, compare the classes
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006774 if (cls.IsRegister()) {
6775 __ cmpl(temp, cls.AsRegister<Register>());
6776 } else {
6777 DCHECK(cls.IsStackSlot()) << cls;
6778 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6779 }
6780 __ j(kNotEqual, &loop);
6781 break;
6782 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006783
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006784 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006785 // /* HeapReference<Class> */ temp = obj->klass_
6786 GenerateReferenceLoadTwoRegisters(instruction,
6787 temp_loc,
6788 obj_loc,
6789 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006790 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006791
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006792 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006793 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006794 __ Bind(&loop);
6795 if (cls.IsRegister()) {
6796 __ cmpl(temp, cls.AsRegister<Register>());
6797 } else {
6798 DCHECK(cls.IsStackSlot()) << cls;
6799 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6800 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006801 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006802
Roland Levillain0d5a2812015-11-13 10:07:31 +00006803 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006804 GenerateReferenceLoadOneRegister(instruction,
6805 temp_loc,
6806 super_offset,
6807 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006808 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006809
6810 // If the class reference currently in `temp` is not null, jump
6811 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006812 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006813 __ j(kNotZero, &loop);
6814 // Otherwise, jump to the slow path to throw the exception.;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006815 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006816 break;
6817 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006818
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006819 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006820 // /* HeapReference<Class> */ temp = obj->klass_
6821 GenerateReferenceLoadTwoRegisters(instruction,
6822 temp_loc,
6823 obj_loc,
6824 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006825 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006826
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006827 // Do an exact check.
6828 if (cls.IsRegister()) {
6829 __ cmpl(temp, cls.AsRegister<Register>());
6830 } else {
6831 DCHECK(cls.IsStackSlot()) << cls;
6832 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6833 }
6834 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006835
6836 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006837 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006838 GenerateReferenceLoadOneRegister(instruction,
6839 temp_loc,
6840 component_offset,
6841 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006842 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006843
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006844 // If the component type is null (i.e. the object not an array), jump to the slow path to
6845 // throw the exception. Otherwise proceed with the check.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006846 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006847 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006848
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006849 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006850 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006851 break;
6852 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006853
Calin Juravle98893e12015-10-02 21:05:03 +01006854 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006855 // We always go into the type check slow path for the unresolved check case.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006856 // We cannot directly call the CheckCast runtime entry point
6857 // without resorting to a type checking slow path here (i.e. by
6858 // calling InvokeRuntime directly), as it would require to
6859 // assign fixed registers for the inputs of this HInstanceOf
6860 // instruction (following the runtime calling convention), which
6861 // might be cluttered by the potential first read barrier
6862 // emission at the beginning of this method.
6863 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006864 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006865
6866 case TypeCheckKind::kInterfaceCheck: {
Vladimir Markoe619f6c2017-12-12 16:00:01 +00006867 // Fast path for the interface check. Try to avoid read barriers to improve the fast path.
6868 // We can not get false positives by doing this.
6869 // /* HeapReference<Class> */ temp = obj->klass_
6870 GenerateReferenceLoadTwoRegisters(instruction,
6871 temp_loc,
6872 obj_loc,
6873 class_offset,
6874 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006875
Vladimir Markoe619f6c2017-12-12 16:00:01 +00006876 // /* HeapReference<Class> */ temp = temp->iftable_
6877 GenerateReferenceLoadTwoRegisters(instruction,
6878 temp_loc,
6879 temp_loc,
6880 iftable_offset,
6881 kWithoutReadBarrier);
6882 // Iftable is never null.
6883 __ movl(maybe_temp2_loc.AsRegister<Register>(), Address(temp, array_length_offset));
6884 // Maybe poison the `cls` for direct comparison with memory.
6885 __ MaybePoisonHeapReference(cls.AsRegister<Register>());
6886 // Loop through the iftable and check if any class matches.
6887 NearLabel start_loop;
6888 __ Bind(&start_loop);
6889 // Need to subtract first to handle the empty array case.
6890 __ subl(maybe_temp2_loc.AsRegister<Register>(), Immediate(2));
6891 __ j(kNegative, type_check_slow_path->GetEntryLabel());
6892 // Go to next interface if the classes do not match.
6893 __ cmpl(cls.AsRegister<Register>(),
6894 CodeGeneratorX86::ArrayAddress(temp,
6895 maybe_temp2_loc,
6896 TIMES_4,
6897 object_array_data_offset));
6898 __ j(kNotEqual, &start_loop);
6899 // If `cls` was poisoned above, unpoison it.
6900 __ MaybeUnpoisonHeapReference(cls.AsRegister<Register>());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006901 break;
6902 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006903 }
6904 __ Bind(&done);
6905
Roland Levillain0d5a2812015-11-13 10:07:31 +00006906 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006907}
6908
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006909void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006910 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6911 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006912 InvokeRuntimeCallingConvention calling_convention;
6913 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6914}
6915
6916void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006917 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject
6918 : kQuickUnlockObject,
Alexandre Rames8158f282015-08-07 10:26:17 +01006919 instruction,
Serban Constantinescuba45db02016-07-12 22:53:02 +01006920 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006921 if (instruction->IsEnter()) {
6922 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6923 } else {
6924 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6925 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006926}
6927
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006928void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6929void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6930void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6931
6932void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6933 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006934 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006935 DCHECK(instruction->GetResultType() == DataType::Type::kInt32
6936 || instruction->GetResultType() == DataType::Type::kInt64);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006937 locations->SetInAt(0, Location::RequiresRegister());
6938 locations->SetInAt(1, Location::Any());
6939 locations->SetOut(Location::SameAsFirstInput());
6940}
6941
6942void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
6943 HandleBitwiseOperation(instruction);
6944}
6945
6946void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
6947 HandleBitwiseOperation(instruction);
6948}
6949
6950void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
6951 HandleBitwiseOperation(instruction);
6952}
6953
6954void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6955 LocationSummary* locations = instruction->GetLocations();
6956 Location first = locations->InAt(0);
6957 Location second = locations->InAt(1);
6958 DCHECK(first.Equals(locations->Out()));
6959
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006960 if (instruction->GetResultType() == DataType::Type::kInt32) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006961 if (second.IsRegister()) {
6962 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006963 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006964 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006965 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006966 } else {
6967 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006968 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006969 }
6970 } else if (second.IsConstant()) {
6971 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006972 __ andl(first.AsRegister<Register>(),
6973 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006974 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006975 __ orl(first.AsRegister<Register>(),
6976 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006977 } else {
6978 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00006979 __ xorl(first.AsRegister<Register>(),
6980 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006981 }
6982 } else {
6983 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006984 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006985 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006986 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006987 } else {
6988 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006989 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006990 }
6991 }
6992 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006993 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006994 if (second.IsRegisterPair()) {
6995 if (instruction->IsAnd()) {
6996 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6997 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6998 } else if (instruction->IsOr()) {
6999 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7000 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7001 } else {
7002 DCHECK(instruction->IsXor());
7003 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7004 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7005 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007006 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007007 if (instruction->IsAnd()) {
7008 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7009 __ andl(first.AsRegisterPairHigh<Register>(),
7010 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7011 } else if (instruction->IsOr()) {
7012 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7013 __ orl(first.AsRegisterPairHigh<Register>(),
7014 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7015 } else {
7016 DCHECK(instruction->IsXor());
7017 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7018 __ xorl(first.AsRegisterPairHigh<Register>(),
7019 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7020 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007021 } else {
7022 DCHECK(second.IsConstant()) << second;
7023 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007024 int32_t low_value = Low32Bits(value);
7025 int32_t high_value = High32Bits(value);
7026 Immediate low(low_value);
7027 Immediate high(high_value);
7028 Register first_low = first.AsRegisterPairLow<Register>();
7029 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007030 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007031 if (low_value == 0) {
7032 __ xorl(first_low, first_low);
7033 } else if (low_value != -1) {
7034 __ andl(first_low, low);
7035 }
7036 if (high_value == 0) {
7037 __ xorl(first_high, first_high);
7038 } else if (high_value != -1) {
7039 __ andl(first_high, high);
7040 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007041 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007042 if (low_value != 0) {
7043 __ orl(first_low, low);
7044 }
7045 if (high_value != 0) {
7046 __ orl(first_high, high);
7047 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007048 } else {
7049 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007050 if (low_value != 0) {
7051 __ xorl(first_low, low);
7052 }
7053 if (high_value != 0) {
7054 __ xorl(first_high, high);
7055 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007056 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007057 }
7058 }
7059}
7060
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007061void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(
7062 HInstruction* instruction,
7063 Location out,
7064 uint32_t offset,
7065 Location maybe_temp,
7066 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007067 Register out_reg = out.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007068 if (read_barrier_option == kWithReadBarrier) {
7069 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007070 if (kUseBakerReadBarrier) {
7071 // Load with fast path based Baker's read barrier.
7072 // /* HeapReference<Object> */ out = *(out + offset)
7073 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00007074 instruction, out, out_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007075 } else {
7076 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007077 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain7c1559a2015-12-15 10:55:36 +00007078 // in the following move operation, as we will need it for the
7079 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00007080 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007081 __ movl(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007082 // /* HeapReference<Object> */ out = *(out + offset)
7083 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007084 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007085 }
7086 } else {
7087 // Plain load with no read barrier.
7088 // /* HeapReference<Object> */ out = *(out + offset)
7089 __ movl(out_reg, Address(out_reg, offset));
7090 __ MaybeUnpoisonHeapReference(out_reg);
7091 }
7092}
7093
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007094void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(
7095 HInstruction* instruction,
7096 Location out,
7097 Location obj,
7098 uint32_t offset,
7099 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007100 Register out_reg = out.AsRegister<Register>();
7101 Register obj_reg = obj.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007102 if (read_barrier_option == kWithReadBarrier) {
7103 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007104 if (kUseBakerReadBarrier) {
7105 // Load with fast path based Baker's read barrier.
7106 // /* HeapReference<Object> */ out = *(obj + offset)
7107 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00007108 instruction, out, obj_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007109 } else {
7110 // Load with slow path based read barrier.
7111 // /* HeapReference<Object> */ out = *(obj + offset)
7112 __ movl(out_reg, Address(obj_reg, offset));
7113 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
7114 }
7115 } else {
7116 // Plain load with no read barrier.
7117 // /* HeapReference<Object> */ out = *(obj + offset)
7118 __ movl(out_reg, Address(obj_reg, offset));
7119 __ MaybeUnpoisonHeapReference(out_reg);
7120 }
7121}
7122
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007123void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(
7124 HInstruction* instruction,
7125 Location root,
7126 const Address& address,
7127 Label* fixup_label,
7128 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007129 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007130 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07007131 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007132 if (kUseBakerReadBarrier) {
7133 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
7134 // Baker's read barrier are used:
7135 //
Roland Levillaind966ce72017-02-09 16:20:14 +00007136 // root = obj.field;
7137 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
7138 // if (temp != null) {
7139 // root = temp(root)
Roland Levillain7c1559a2015-12-15 10:55:36 +00007140 // }
7141
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007142 // /* GcRoot<mirror::Object> */ root = *address
7143 __ movl(root_reg, address);
7144 if (fixup_label != nullptr) {
7145 __ Bind(fixup_label);
7146 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007147 static_assert(
7148 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
7149 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
7150 "have different sizes.");
7151 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
7152 "art::mirror::CompressedReference<mirror::Object> and int32_t "
7153 "have different sizes.");
7154
Vladimir Marko953437b2016-08-24 08:30:46 +00007155 // Slow path marking the GC root `root`.
Vladimir Marko174b2e22017-10-12 13:34:49 +01007156 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathX86(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007157 instruction, root, /* unpoison_ref_before_marking */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007158 codegen_->AddSlowPath(slow_path);
7159
Roland Levillaind966ce72017-02-09 16:20:14 +00007160 // Test the entrypoint (`Thread::Current()->pReadBarrierMarkReg ## root.reg()`).
7161 const int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +01007162 Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(root.reg());
Roland Levillaind966ce72017-02-09 16:20:14 +00007163 __ fs()->cmpl(Address::Absolute(entry_point_offset), Immediate(0));
7164 // The entrypoint is null when the GC is not marking.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007165 __ j(kNotEqual, slow_path->GetEntryLabel());
7166 __ Bind(slow_path->GetExitLabel());
7167 } else {
7168 // GC root loaded through a slow path for read barriers other
7169 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007170 // /* GcRoot<mirror::Object>* */ root = address
7171 __ leal(root_reg, address);
7172 if (fixup_label != nullptr) {
7173 __ Bind(fixup_label);
7174 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007175 // /* mirror::Object* */ root = root->Read()
7176 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
7177 }
7178 } else {
7179 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007180 // /* GcRoot<mirror::Object> */ root = *address
7181 __ movl(root_reg, address);
7182 if (fixup_label != nullptr) {
7183 __ Bind(fixup_label);
7184 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007185 // Note that GC roots are not affected by heap poisoning, thus we
7186 // do not have to unpoison `root_reg` here.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007187 }
7188}
7189
7190void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
7191 Location ref,
7192 Register obj,
7193 uint32_t offset,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007194 bool needs_null_check) {
7195 DCHECK(kEmitCompilerReadBarrier);
7196 DCHECK(kUseBakerReadBarrier);
7197
7198 // /* HeapReference<Object> */ ref = *(obj + offset)
7199 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007200 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007201}
7202
7203void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
7204 Location ref,
7205 Register obj,
7206 uint32_t data_offset,
7207 Location index,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007208 bool needs_null_check) {
7209 DCHECK(kEmitCompilerReadBarrier);
7210 DCHECK(kUseBakerReadBarrier);
7211
Roland Levillain3d312422016-06-23 13:53:42 +01007212 static_assert(
7213 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
7214 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00007215 // /* HeapReference<Object> */ ref =
7216 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007217 Address src = CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007218 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007219}
7220
7221void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
7222 Location ref,
7223 Register obj,
7224 const Address& src,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007225 bool needs_null_check,
7226 bool always_update_field,
7227 Register* temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007228 DCHECK(kEmitCompilerReadBarrier);
7229 DCHECK(kUseBakerReadBarrier);
7230
7231 // In slow path based read barriers, the read barrier call is
7232 // inserted after the original load. However, in fast path based
7233 // Baker's read barriers, we need to perform the load of
7234 // mirror::Object::monitor_ *before* the original reference load.
7235 // This load-load ordering is required by the read barrier.
7236 // The fast path/slow path (for Baker's algorithm) should look like:
7237 //
7238 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7239 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7240 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007241 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain7c1559a2015-12-15 10:55:36 +00007242 // if (is_gray) {
7243 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7244 // }
7245 //
7246 // Note: the original implementation in ReadBarrier::Barrier is
7247 // slightly more complex as:
7248 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007249 // the high-bits of rb_state, which are expected to be all zeroes
7250 // (we use CodeGeneratorX86::GenerateMemoryBarrier instead here,
7251 // which is a no-op thanks to the x86 memory model);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007252 // - it performs additional checks that we do not do here for
7253 // performance reasons.
7254
7255 Register ref_reg = ref.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +00007256 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7257
Vladimir Marko953437b2016-08-24 08:30:46 +00007258 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007259 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
7260 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko953437b2016-08-24 08:30:46 +00007261 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
7262 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
7263 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
7264
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007265 // if (rb_state == ReadBarrier::GrayState())
Vladimir Marko953437b2016-08-24 08:30:46 +00007266 // ref = ReadBarrier::Mark(ref);
7267 // At this point, just do the "if" and make sure that flags are preserved until the branch.
7268 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain7c1559a2015-12-15 10:55:36 +00007269 if (needs_null_check) {
7270 MaybeRecordImplicitNullCheck(instruction);
7271 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007272
7273 // Load fence to prevent load-load reordering.
7274 // Note that this is a no-op, thanks to the x86 memory model.
7275 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
7276
7277 // The actual reference load.
7278 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00007279 __ movl(ref_reg, src); // Flags are unaffected.
7280
7281 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
7282 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007283 SlowPathCode* slow_path;
7284 if (always_update_field) {
7285 DCHECK(temp != nullptr);
Vladimir Marko174b2e22017-10-12 13:34:49 +01007286 slow_path = new (GetScopedAllocator()) ReadBarrierMarkAndUpdateFieldSlowPathX86(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007287 instruction, ref, obj, src, /* unpoison_ref_before_marking */ true, *temp);
7288 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01007289 slow_path = new (GetScopedAllocator()) ReadBarrierMarkSlowPathX86(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007290 instruction, ref, /* unpoison_ref_before_marking */ true);
7291 }
Vladimir Marko953437b2016-08-24 08:30:46 +00007292 AddSlowPath(slow_path);
7293
7294 // We have done the "if" of the gray bit check above, now branch based on the flags.
7295 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain7c1559a2015-12-15 10:55:36 +00007296
7297 // Object* ref = ref_addr->AsMirrorPtr()
7298 __ MaybeUnpoisonHeapReference(ref_reg);
7299
Roland Levillain7c1559a2015-12-15 10:55:36 +00007300 __ Bind(slow_path->GetExitLabel());
7301}
7302
7303void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
7304 Location out,
7305 Location ref,
7306 Location obj,
7307 uint32_t offset,
7308 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007309 DCHECK(kEmitCompilerReadBarrier);
7310
Roland Levillain7c1559a2015-12-15 10:55:36 +00007311 // Insert a slow path based read barrier *after* the reference load.
7312 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007313 // If heap poisoning is enabled, the unpoisoning of the loaded
7314 // reference will be carried out by the runtime within the slow
7315 // path.
7316 //
7317 // Note that `ref` currently does not get unpoisoned (when heap
7318 // poisoning is enabled), which is alright as the `ref` argument is
7319 // not used by the artReadBarrierSlow entry point.
7320 //
7321 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01007322 SlowPathCode* slow_path = new (GetScopedAllocator())
Roland Levillain0d5a2812015-11-13 10:07:31 +00007323 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
7324 AddSlowPath(slow_path);
7325
Roland Levillain0d5a2812015-11-13 10:07:31 +00007326 __ jmp(slow_path->GetEntryLabel());
7327 __ Bind(slow_path->GetExitLabel());
7328}
7329
Roland Levillain7c1559a2015-12-15 10:55:36 +00007330void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7331 Location out,
7332 Location ref,
7333 Location obj,
7334 uint32_t offset,
7335 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007336 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007337 // Baker's read barriers shall be handled by the fast path
7338 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
7339 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007340 // If heap poisoning is enabled, unpoisoning will be taken care of
7341 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007342 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007343 } else if (kPoisonHeapReferences) {
7344 __ UnpoisonHeapReference(out.AsRegister<Register>());
7345 }
7346}
7347
Roland Levillain7c1559a2015-12-15 10:55:36 +00007348void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7349 Location out,
7350 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007351 DCHECK(kEmitCompilerReadBarrier);
7352
Roland Levillain7c1559a2015-12-15 10:55:36 +00007353 // Insert a slow path based read barrier *after* the GC root load.
7354 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007355 // Note that GC roots are not affected by heap poisoning, so we do
7356 // not need to do anything special for this here.
7357 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007358 new (GetScopedAllocator()) ReadBarrierForRootSlowPathX86(instruction, out, root);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007359 AddSlowPath(slow_path);
7360
Roland Levillain0d5a2812015-11-13 10:07:31 +00007361 __ jmp(slow_path->GetEntryLabel());
7362 __ Bind(slow_path->GetExitLabel());
7363}
7364
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007365void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007366 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007367 LOG(FATAL) << "Unreachable";
7368}
7369
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007370void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007371 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007372 LOG(FATAL) << "Unreachable";
7373}
7374
Mark Mendellfe57faa2015-09-18 09:26:15 -04007375// Simple implementation of packed switch - generate cascaded compare/jumps.
7376void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7377 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007378 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendellfe57faa2015-09-18 09:26:15 -04007379 locations->SetInAt(0, Location::RequiresRegister());
7380}
7381
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007382void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
7383 int32_t lower_bound,
7384 uint32_t num_entries,
7385 HBasicBlock* switch_block,
7386 HBasicBlock* default_block) {
7387 // Figure out the correct compare values and jump conditions.
7388 // Handle the first compare/branch as a special case because it might
7389 // jump to the default case.
7390 DCHECK_GT(num_entries, 2u);
7391 Condition first_condition;
7392 uint32_t index;
7393 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
7394 if (lower_bound != 0) {
7395 first_condition = kLess;
7396 __ cmpl(value_reg, Immediate(lower_bound));
7397 __ j(first_condition, codegen_->GetLabelOf(default_block));
7398 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007399
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007400 index = 1;
7401 } else {
7402 // Handle all the compare/jumps below.
7403 first_condition = kBelow;
7404 index = 0;
7405 }
7406
7407 // Handle the rest of the compare/jumps.
7408 for (; index + 1 < num_entries; index += 2) {
7409 int32_t compare_to_value = lower_bound + index + 1;
7410 __ cmpl(value_reg, Immediate(compare_to_value));
7411 // Jump to successors[index] if value < case_value[index].
7412 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
7413 // Jump to successors[index + 1] if value == case_value[index + 1].
7414 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
7415 }
7416
7417 if (index != num_entries) {
7418 // There are an odd number of entries. Handle the last one.
7419 DCHECK_EQ(index + 1, num_entries);
7420 __ cmpl(value_reg, Immediate(lower_bound + index));
7421 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007422 }
7423
7424 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007425 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
7426 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007427 }
7428}
7429
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007430void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7431 int32_t lower_bound = switch_instr->GetStartValue();
7432 uint32_t num_entries = switch_instr->GetNumEntries();
7433 LocationSummary* locations = switch_instr->GetLocations();
7434 Register value_reg = locations->InAt(0).AsRegister<Register>();
7435
7436 GenPackedSwitchWithCompares(value_reg,
7437 lower_bound,
7438 num_entries,
7439 switch_instr->GetBlock(),
7440 switch_instr->GetDefaultBlock());
7441}
7442
Mark Mendell805b3b52015-09-18 14:10:29 -04007443void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7444 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007445 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendell805b3b52015-09-18 14:10:29 -04007446 locations->SetInAt(0, Location::RequiresRegister());
7447
7448 // Constant area pointer.
7449 locations->SetInAt(1, Location::RequiresRegister());
7450
7451 // And the temporary we need.
7452 locations->AddTemp(Location::RequiresRegister());
7453}
7454
7455void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7456 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007457 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04007458 LocationSummary* locations = switch_instr->GetLocations();
7459 Register value_reg = locations->InAt(0).AsRegister<Register>();
7460 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7461
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007462 if (num_entries <= kPackedSwitchJumpTableThreshold) {
7463 GenPackedSwitchWithCompares(value_reg,
7464 lower_bound,
7465 num_entries,
7466 switch_instr->GetBlock(),
7467 default_block);
7468 return;
7469 }
7470
Mark Mendell805b3b52015-09-18 14:10:29 -04007471 // Optimizing has a jump area.
7472 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7473 Register constant_area = locations->InAt(1).AsRegister<Register>();
7474
7475 // Remove the bias, if needed.
7476 if (lower_bound != 0) {
7477 __ leal(temp_reg, Address(value_reg, -lower_bound));
7478 value_reg = temp_reg;
7479 }
7480
7481 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007482 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04007483 __ cmpl(value_reg, Immediate(num_entries - 1));
7484 __ j(kAbove, codegen_->GetLabelOf(default_block));
7485
7486 // We are in the range of the table.
7487 // Load (target-constant_area) from the jump table, indexing by the value.
7488 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
7489
7490 // Compute the actual target address by adding in constant_area.
7491 __ addl(temp_reg, constant_area);
7492
7493 // And jump.
7494 __ jmp(temp_reg);
7495}
7496
Mark Mendell0616ae02015-04-17 12:49:27 -04007497void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
7498 HX86ComputeBaseMethodAddress* insn) {
7499 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007500 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Mark Mendell0616ae02015-04-17 12:49:27 -04007501 locations->SetOut(Location::RequiresRegister());
7502}
7503
7504void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
7505 HX86ComputeBaseMethodAddress* insn) {
7506 LocationSummary* locations = insn->GetLocations();
7507 Register reg = locations->Out().AsRegister<Register>();
7508
7509 // Generate call to next instruction.
7510 Label next_instruction;
7511 __ call(&next_instruction);
7512 __ Bind(&next_instruction);
7513
7514 // Remember this offset for later use with constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007515 codegen_->AddMethodAddressOffset(insn, GetAssembler()->CodeSize());
Mark Mendell0616ae02015-04-17 12:49:27 -04007516
7517 // Grab the return address off the stack.
7518 __ popl(reg);
7519}
7520
7521void LocationsBuilderX86::VisitX86LoadFromConstantTable(
7522 HX86LoadFromConstantTable* insn) {
7523 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007524 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Mark Mendell0616ae02015-04-17 12:49:27 -04007525
7526 locations->SetInAt(0, Location::RequiresRegister());
7527 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
7528
7529 // If we don't need to be materialized, we only need the inputs to be set.
David Brazdilb3e773e2016-01-26 11:28:37 +00007530 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007531 return;
7532 }
7533
7534 switch (insn->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007535 case DataType::Type::kFloat32:
7536 case DataType::Type::kFloat64:
Mark Mendell0616ae02015-04-17 12:49:27 -04007537 locations->SetOut(Location::RequiresFpuRegister());
7538 break;
7539
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007540 case DataType::Type::kInt32:
Mark Mendell0616ae02015-04-17 12:49:27 -04007541 locations->SetOut(Location::RequiresRegister());
7542 break;
7543
7544 default:
7545 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7546 }
7547}
7548
7549void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
David Brazdilb3e773e2016-01-26 11:28:37 +00007550 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007551 return;
7552 }
7553
7554 LocationSummary* locations = insn->GetLocations();
7555 Location out = locations->Out();
7556 Register const_area = locations->InAt(0).AsRegister<Register>();
7557 HConstant *value = insn->GetConstant();
7558
7559 switch (insn->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007560 case DataType::Type::kFloat32:
Mark Mendell0616ae02015-04-17 12:49:27 -04007561 __ movss(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007562 codegen_->LiteralFloatAddress(
7563 value->AsFloatConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04007564 break;
7565
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007566 case DataType::Type::kFloat64:
Mark Mendell0616ae02015-04-17 12:49:27 -04007567 __ movsd(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007568 codegen_->LiteralDoubleAddress(
7569 value->AsDoubleConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04007570 break;
7571
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007572 case DataType::Type::kInt32:
Mark Mendell0616ae02015-04-17 12:49:27 -04007573 __ movl(out.AsRegister<Register>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007574 codegen_->LiteralInt32Address(
7575 value->AsIntConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04007576 break;
7577
7578 default:
7579 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7580 }
7581}
7582
Mark Mendell0616ae02015-04-17 12:49:27 -04007583/**
7584 * Class to handle late fixup of offsets into constant area.
7585 */
Vladimir Marko5233f932015-09-29 19:01:15 +01007586class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04007587 public:
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007588 RIPFixup(CodeGeneratorX86& codegen,
7589 HX86ComputeBaseMethodAddress* base_method_address,
7590 size_t offset)
7591 : codegen_(&codegen),
7592 base_method_address_(base_method_address),
7593 offset_into_constant_area_(offset) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04007594
7595 protected:
7596 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
7597
7598 CodeGeneratorX86* codegen_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007599 HX86ComputeBaseMethodAddress* base_method_address_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007600
7601 private:
7602 void Process(const MemoryRegion& region, int pos) OVERRIDE {
7603 // Patch the correct offset for the instruction. The place to patch is the
7604 // last 4 bytes of the instruction.
7605 // The value to patch is the distance from the offset in the constant area
7606 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04007607 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007608 int32_t relative_position =
7609 constant_offset - codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell0616ae02015-04-17 12:49:27 -04007610
7611 // Patch in the right value.
7612 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
7613 }
7614
Mark Mendell0616ae02015-04-17 12:49:27 -04007615 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04007616 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007617};
7618
Mark Mendell805b3b52015-09-18 14:10:29 -04007619/**
7620 * Class to handle late fixup of offsets to a jump table that will be created in the
7621 * constant area.
7622 */
7623class JumpTableRIPFixup : public RIPFixup {
7624 public:
7625 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007626 : RIPFixup(codegen, switch_instr->GetBaseMethodAddress(), static_cast<size_t>(-1)),
7627 switch_instr_(switch_instr) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04007628
7629 void CreateJumpTable() {
7630 X86Assembler* assembler = codegen_->GetAssembler();
7631
7632 // Ensure that the reference to the jump table has the correct offset.
7633 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
7634 SetOffset(offset_in_constant_table);
7635
7636 // The label values in the jump table are computed relative to the
7637 // instruction addressing the constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007638 const int32_t relative_offset = codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell805b3b52015-09-18 14:10:29 -04007639
7640 // Populate the jump table with the correct values for the jump table.
7641 int32_t num_entries = switch_instr_->GetNumEntries();
7642 HBasicBlock* block = switch_instr_->GetBlock();
7643 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
7644 // The value that we want is the target offset - the position of the table.
7645 for (int32_t i = 0; i < num_entries; i++) {
7646 HBasicBlock* b = successors[i];
7647 Label* l = codegen_->GetLabelOf(b);
7648 DCHECK(l->IsBound());
7649 int32_t offset_to_block = l->Position() - relative_offset;
7650 assembler->AppendInt32(offset_to_block);
7651 }
7652 }
7653
7654 private:
7655 const HX86PackedSwitch* switch_instr_;
7656};
7657
7658void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
7659 // Generate the constant area if needed.
7660 X86Assembler* assembler = GetAssembler();
7661 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
7662 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
7663 // byte values.
7664 assembler->Align(4, 0);
7665 constant_area_start_ = assembler->CodeSize();
7666
7667 // Populate any jump tables.
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007668 for (JumpTableRIPFixup* jump_table : fixups_to_jump_tables_) {
Mark Mendell805b3b52015-09-18 14:10:29 -04007669 jump_table->CreateJumpTable();
7670 }
7671
7672 // And now add the constant area to the generated code.
7673 assembler->AddConstantArea();
7674 }
7675
7676 // And finish up.
7677 CodeGenerator::Finalize(allocator);
7678}
7679
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007680Address CodeGeneratorX86::LiteralDoubleAddress(double v,
7681 HX86ComputeBaseMethodAddress* method_base,
7682 Register reg) {
7683 AssemblerFixup* fixup =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007684 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddDouble(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04007685 return Address(reg, kDummy32BitOffset, fixup);
7686}
7687
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007688Address CodeGeneratorX86::LiteralFloatAddress(float v,
7689 HX86ComputeBaseMethodAddress* method_base,
7690 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007691 AssemblerFixup* fixup =
7692 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddFloat(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04007693 return Address(reg, kDummy32BitOffset, fixup);
7694}
7695
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007696Address CodeGeneratorX86::LiteralInt32Address(int32_t v,
7697 HX86ComputeBaseMethodAddress* method_base,
7698 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007699 AssemblerFixup* fixup =
7700 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddInt32(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04007701 return Address(reg, kDummy32BitOffset, fixup);
7702}
7703
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007704Address CodeGeneratorX86::LiteralInt64Address(int64_t v,
7705 HX86ComputeBaseMethodAddress* method_base,
7706 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007707 AssemblerFixup* fixup =
7708 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddInt64(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04007709 return Address(reg, kDummy32BitOffset, fixup);
7710}
7711
Aart Bika19616e2016-02-01 18:57:58 -08007712void CodeGeneratorX86::Load32BitValue(Register dest, int32_t value) {
7713 if (value == 0) {
7714 __ xorl(dest, dest);
7715 } else {
7716 __ movl(dest, Immediate(value));
7717 }
7718}
7719
7720void CodeGeneratorX86::Compare32BitValue(Register dest, int32_t value) {
7721 if (value == 0) {
7722 __ testl(dest, dest);
7723 } else {
7724 __ cmpl(dest, Immediate(value));
7725 }
7726}
7727
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007728void CodeGeneratorX86::GenerateIntCompare(Location lhs, Location rhs) {
7729 Register lhs_reg = lhs.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07007730 GenerateIntCompare(lhs_reg, rhs);
7731}
7732
7733void CodeGeneratorX86::GenerateIntCompare(Register lhs, Location rhs) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007734 if (rhs.IsConstant()) {
7735 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
jessicahandojo4877b792016-09-08 19:49:13 -07007736 Compare32BitValue(lhs, value);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007737 } else if (rhs.IsStackSlot()) {
jessicahandojo4877b792016-09-08 19:49:13 -07007738 __ cmpl(lhs, Address(ESP, rhs.GetStackIndex()));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007739 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07007740 __ cmpl(lhs, rhs.AsRegister<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007741 }
7742}
7743
7744Address CodeGeneratorX86::ArrayAddress(Register obj,
7745 Location index,
7746 ScaleFactor scale,
7747 uint32_t data_offset) {
7748 return index.IsConstant() ?
7749 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << scale) + data_offset) :
7750 Address(obj, index.AsRegister<Register>(), scale, data_offset);
7751}
7752
Mark Mendell805b3b52015-09-18 14:10:29 -04007753Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
7754 Register reg,
7755 Register value) {
7756 // Create a fixup to be used to create and address the jump table.
7757 JumpTableRIPFixup* table_fixup =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007758 new (GetGraph()->GetAllocator()) JumpTableRIPFixup(*this, switch_instr);
Mark Mendell805b3b52015-09-18 14:10:29 -04007759
7760 // We have to populate the jump tables.
7761 fixups_to_jump_tables_.push_back(table_fixup);
7762
7763 // We want a scaled address, as we are extracting the correct offset from the table.
7764 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
7765}
7766
Andreas Gampe85b62f22015-09-09 13:15:38 -07007767// TODO: target as memory.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007768void CodeGeneratorX86::MoveFromReturnRegister(Location target, DataType::Type type) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07007769 if (!target.IsValid()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007770 DCHECK_EQ(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007771 return;
7772 }
7773
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007774 DCHECK_NE(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007775
7776 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
7777 if (target.Equals(return_loc)) {
7778 return;
7779 }
7780
7781 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7782 // with the else branch.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007783 if (type == DataType::Type::kInt64) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007784 HParallelMove parallel_move(GetGraph()->GetAllocator());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007785 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), DataType::Type::kInt32, nullptr);
7786 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), DataType::Type::kInt32, nullptr);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007787 GetMoveResolver()->EmitNativeCode(&parallel_move);
7788 } else {
7789 // Let the parallel move resolver take care of all of this.
Vladimir Markoca6fff82017-10-03 14:49:14 +01007790 HParallelMove parallel_move(GetGraph()->GetAllocator());
Andreas Gampe85b62f22015-09-09 13:15:38 -07007791 parallel_move.AddMove(return_loc, target, type, nullptr);
7792 GetMoveResolver()->EmitNativeCode(&parallel_move);
7793 }
7794}
7795
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007796void CodeGeneratorX86::PatchJitRootUse(uint8_t* code,
7797 const uint8_t* roots_data,
7798 const PatchInfo<Label>& info,
7799 uint64_t index_in_table) const {
7800 uint32_t code_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
7801 uintptr_t address =
7802 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
7803 typedef __attribute__((__aligned__(1))) uint32_t unaligned_uint32_t;
7804 reinterpret_cast<unaligned_uint32_t*>(code + code_offset)[0] =
7805 dchecked_integral_cast<uint32_t>(address);
7806}
7807
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007808void CodeGeneratorX86::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
7809 for (const PatchInfo<Label>& info : jit_string_patches_) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01007810 StringReference string_reference(&info.dex_file, dex::StringIndex(info.index));
7811 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007812 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007813 }
7814
7815 for (const PatchInfo<Label>& info : jit_class_patches_) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01007816 TypeReference type_reference(&info.dex_file, dex::TypeIndex(info.index));
7817 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007818 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007819 }
7820}
7821
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007822void LocationsBuilderX86::VisitIntermediateAddress(HIntermediateAddress* instruction
7823 ATTRIBUTE_UNUSED) {
7824 LOG(FATAL) << "Unreachable";
7825}
7826
7827void InstructionCodeGeneratorX86::VisitIntermediateAddress(HIntermediateAddress* instruction
7828 ATTRIBUTE_UNUSED) {
7829 LOG(FATAL) << "Unreachable";
7830}
7831
Roland Levillain4d027112015-07-01 15:41:14 +01007832#undef __
7833
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007834} // namespace x86
7835} // namespace art