blob: c52c7ff7f1bb94bf4845977e002ab33c0aee89d0 [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 Geoffray8d728322018-01-18 22:44:32 +00001064 if (GetCompilerOptions().CountHotnessInCompiledCode()) {
1065 __ addw(Address(kMethodRegisterArgument, ArtMethod::HotnessCountOffset().Int32Value()),
1066 Immediate(1));
1067 }
1068
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001069 if (!skip_overflow_check) {
Vladimir Marko33bff252017-11-01 14:35:42 +00001070 size_t reserved_bytes = GetStackOverflowReservedBytes(InstructionSet::kX86);
1071 __ testl(EAX, Address(ESP, -static_cast<int32_t>(reserved_bytes)));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001072 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +01001073 }
1074
Mark Mendell5f874182015-03-04 15:42:45 -05001075 if (HasEmptyFrame()) {
1076 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001077 }
Mark Mendell5f874182015-03-04 15:42:45 -05001078
1079 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
1080 Register reg = kCoreCalleeSaves[i];
1081 if (allocated_registers_.ContainsCoreRegister(reg)) {
1082 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001083 __ cfi().AdjustCFAOffset(kX86WordSize);
1084 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -05001085 }
1086 }
1087
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001088 int adjust = GetFrameSize() - FrameEntrySpillSize();
1089 __ subl(ESP, Immediate(adjust));
1090 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001091 // Save the current method if we need it. Note that we do not
1092 // do this in HCurrentMethod, as the instruction might have been removed
1093 // in the SSA graph.
1094 if (RequiresCurrentMethod()) {
1095 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
1096 }
Nicolas Geoffrayf7893532017-06-15 12:34:36 +01001097
1098 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1099 // Initialize should_deoptimize flag to 0.
1100 __ movl(Address(ESP, GetStackOffsetOfShouldDeoptimizeFlag()), Immediate(0));
1101 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001102}
1103
1104void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001105 __ cfi().RememberState();
1106 if (!HasEmptyFrame()) {
1107 int adjust = GetFrameSize() - FrameEntrySpillSize();
1108 __ addl(ESP, Immediate(adjust));
1109 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -05001110
David Srbeckyc34dc932015-04-12 09:27:43 +01001111 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1112 Register reg = kCoreCalleeSaves[i];
1113 if (allocated_registers_.ContainsCoreRegister(reg)) {
1114 __ popl(reg);
1115 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
1116 __ cfi().Restore(DWARFReg(reg));
1117 }
Mark Mendell5f874182015-03-04 15:42:45 -05001118 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001119 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001120 __ ret();
1121 __ cfi().RestoreState();
1122 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001123}
1124
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001125void CodeGeneratorX86::Bind(HBasicBlock* block) {
1126 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001127}
1128
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001129Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(DataType::Type type) const {
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001130 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001131 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001132 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001133 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001134 case DataType::Type::kInt8:
1135 case DataType::Type::kUint16:
1136 case DataType::Type::kInt16:
1137 case DataType::Type::kInt32:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001138 return Location::RegisterLocation(EAX);
1139
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001140 case DataType::Type::kInt64:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001141 return Location::RegisterPairLocation(EAX, EDX);
1142
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001143 case DataType::Type::kVoid:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001144 return Location::NoLocation();
1145
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001146 case DataType::Type::kFloat64:
1147 case DataType::Type::kFloat32:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001148 return Location::FpuRegisterLocation(XMM0);
1149 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001150
1151 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001152}
1153
1154Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
1155 return Location::RegisterLocation(kMethodRegisterArgument);
1156}
1157
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001158Location InvokeDexCallingConventionVisitorX86::GetNextLocation(DataType::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001159 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001160 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001161 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001162 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001163 case DataType::Type::kInt8:
1164 case DataType::Type::kUint16:
1165 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001166 case DataType::Type::kInt32: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001167 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001168 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001169 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001170 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001171 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001172 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001173 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001174 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001175
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001176 case DataType::Type::kInt64: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001177 uint32_t index = gp_index_;
1178 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001179 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001180 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001181 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
1182 calling_convention.GetRegisterPairAt(index));
1183 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001184 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001185 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1186 }
1187 }
1188
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001189 case DataType::Type::kFloat32: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001190 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001191 stack_index_++;
1192 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1193 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1194 } else {
1195 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1196 }
1197 }
1198
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001199 case DataType::Type::kFloat64: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001200 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001201 stack_index_ += 2;
1202 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1203 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1204 } else {
1205 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001206 }
1207 }
1208
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001209 case DataType::Type::kVoid:
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001210 LOG(FATAL) << "Unexpected parameter type " << type;
1211 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001212 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00001213 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001214}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001215
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001216void CodeGeneratorX86::Move32(Location destination, Location source) {
1217 if (source.Equals(destination)) {
1218 return;
1219 }
1220 if (destination.IsRegister()) {
1221 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001222 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001223 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001224 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001225 } else {
1226 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001227 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001228 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001229 } else if (destination.IsFpuRegister()) {
1230 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001231 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001232 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001233 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001234 } else {
1235 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001236 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001237 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001238 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001239 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001240 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001241 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001242 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001243 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05001244 } else if (source.IsConstant()) {
1245 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001246 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05001247 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001248 } else {
1249 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001250 __ pushl(Address(ESP, source.GetStackIndex()));
1251 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001252 }
1253 }
1254}
1255
1256void CodeGeneratorX86::Move64(Location destination, Location source) {
1257 if (source.Equals(destination)) {
1258 return;
1259 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001260 if (destination.IsRegisterPair()) {
1261 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001262 EmitParallelMoves(
1263 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1264 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001265 DataType::Type::kInt32,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001266 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001267 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001268 DataType::Type::kInt32);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001269 } else if (source.IsFpuRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001270 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
1271 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
1272 __ psrlq(src_reg, Immediate(32));
1273 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001274 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001275 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001276 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001277 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
1278 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001279 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
1280 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001281 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05001282 if (source.IsFpuRegister()) {
1283 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
1284 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001285 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001286 } else if (source.IsRegisterPair()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001287 size_t elem_size = DataType::Size(DataType::Type::kInt32);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001288 // Create stack space for 2 elements.
1289 __ subl(ESP, Immediate(2 * elem_size));
1290 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
1291 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
1292 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
1293 // And remove the temporary stack space we allocated.
1294 __ addl(ESP, Immediate(2 * elem_size));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001295 } else {
1296 LOG(FATAL) << "Unimplemented";
1297 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001298 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001299 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001300 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001301 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001302 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001303 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001304 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001305 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001306 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001307 } else if (source.IsConstant()) {
1308 HConstant* constant = source.GetConstant();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001309 DCHECK(constant->IsLongConstant() || constant->IsDoubleConstant());
1310 int64_t value = GetInt64ValueOf(constant);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001311 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001312 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
1313 Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001314 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001315 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001316 EmitParallelMoves(
1317 Location::StackSlot(source.GetStackIndex()),
1318 Location::StackSlot(destination.GetStackIndex()),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001319 DataType::Type::kInt32,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001320 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001321 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001322 DataType::Type::kInt32);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001323 }
1324 }
1325}
1326
Calin Juravle175dc732015-08-25 15:42:32 +01001327void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
1328 DCHECK(location.IsRegister());
1329 __ movl(location.AsRegister<Register>(), Immediate(value));
1330}
1331
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001332void CodeGeneratorX86::MoveLocation(Location dst, Location src, DataType::Type dst_type) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001333 HParallelMove move(GetGraph()->GetAllocator());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001334 if (dst_type == DataType::Type::kInt64 && !src.IsConstant() && !src.IsFpuRegister()) {
1335 move.AddMove(src.ToLow(), dst.ToLow(), DataType::Type::kInt32, nullptr);
1336 move.AddMove(src.ToHigh(), dst.ToHigh(), DataType::Type::kInt32, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001337 } else {
David Brazdil74eb1b22015-12-14 11:44:01 +00001338 move.AddMove(src, dst, dst_type, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001339 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001340 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001341}
1342
1343void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1344 if (location.IsRegister()) {
1345 locations->AddTemp(location);
1346 } else if (location.IsRegisterPair()) {
1347 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1348 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1349 } else {
1350 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1351 }
1352}
1353
David Brazdilfc6a86a2015-06-26 10:33:45 +00001354void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08001355 if (successor->IsExitBlock()) {
1356 DCHECK(got->GetPrevious()->AlwaysThrows());
1357 return; // no code needed
1358 }
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001359
1360 HBasicBlock* block = got->GetBlock();
1361 HInstruction* previous = got->GetPrevious();
1362
1363 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001364 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray8d728322018-01-18 22:44:32 +00001365 if (codegen_->GetCompilerOptions().CountHotnessInCompiledCode()) {
1366 __ pushl(EAX);
1367 __ movl(EAX, Address(ESP, kX86WordSize));
1368 __ addw(Address(EAX, ArtMethod::HotnessCountOffset().Int32Value()), Immediate(1));
1369 __ popl(EAX);
1370 }
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001371 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1372 return;
1373 }
1374
1375 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1376 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1377 }
1378 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001379 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001380 }
1381}
1382
David Brazdilfc6a86a2015-06-26 10:33:45 +00001383void LocationsBuilderX86::VisitGoto(HGoto* got) {
1384 got->SetLocations(nullptr);
1385}
1386
1387void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1388 HandleGoto(got, got->GetSuccessor());
1389}
1390
1391void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1392 try_boundary->SetLocations(nullptr);
1393}
1394
1395void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1396 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1397 if (!successor->IsExitBlock()) {
1398 HandleGoto(try_boundary, successor);
1399 }
1400}
1401
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001402void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001403 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001404}
1405
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001406void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001407}
1408
Mark Mendell152408f2015-12-31 12:28:50 -05001409template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001410void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001411 LabelType* true_label,
1412 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001413 if (cond->IsFPConditionTrueIfNaN()) {
1414 __ j(kUnordered, true_label);
1415 } else if (cond->IsFPConditionFalseIfNaN()) {
1416 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001417 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001418 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001419}
1420
Mark Mendell152408f2015-12-31 12:28:50 -05001421template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001422void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001423 LabelType* true_label,
1424 LabelType* false_label) {
Mark Mendellc4701932015-04-10 13:18:51 -04001425 LocationSummary* locations = cond->GetLocations();
1426 Location left = locations->InAt(0);
1427 Location right = locations->InAt(1);
1428 IfCondition if_cond = cond->GetCondition();
1429
Mark Mendellc4701932015-04-10 13:18:51 -04001430 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001431 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001432 IfCondition true_high_cond = if_cond;
1433 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001434 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001435
1436 // Set the conditions for the test, remembering that == needs to be
1437 // decided using the low words.
1438 switch (if_cond) {
1439 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001440 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001441 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001442 break;
1443 case kCondLT:
1444 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001445 break;
1446 case kCondLE:
1447 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001448 break;
1449 case kCondGT:
1450 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001451 break;
1452 case kCondGE:
1453 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001454 break;
Aart Bike9f37602015-10-09 11:15:55 -07001455 case kCondB:
1456 false_high_cond = kCondA;
1457 break;
1458 case kCondBE:
1459 true_high_cond = kCondB;
1460 break;
1461 case kCondA:
1462 false_high_cond = kCondB;
1463 break;
1464 case kCondAE:
1465 true_high_cond = kCondA;
1466 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001467 }
1468
1469 if (right.IsConstant()) {
1470 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001471 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001472 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001473
Aart Bika19616e2016-02-01 18:57:58 -08001474 codegen_->Compare32BitValue(left_high, val_high);
Mark Mendellc4701932015-04-10 13:18:51 -04001475 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001476 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001477 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001478 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001479 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001480 __ j(X86Condition(true_high_cond), true_label);
1481 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001482 }
1483 // Must be equal high, so compare the lows.
Aart Bika19616e2016-02-01 18:57:58 -08001484 codegen_->Compare32BitValue(left_low, val_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001485 } else if (right.IsRegisterPair()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001486 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001487 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001488
1489 __ cmpl(left_high, right_high);
1490 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001491 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001492 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001493 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001494 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001495 __ j(X86Condition(true_high_cond), true_label);
1496 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001497 }
1498 // Must be equal high, so compare the lows.
1499 __ cmpl(left_low, right_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001500 } else {
1501 DCHECK(right.IsDoubleStackSlot());
1502 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
1503 if (if_cond == kCondNE) {
1504 __ j(X86Condition(true_high_cond), true_label);
1505 } else if (if_cond == kCondEQ) {
1506 __ j(X86Condition(false_high_cond), false_label);
1507 } else {
1508 __ j(X86Condition(true_high_cond), true_label);
1509 __ j(X86Condition(false_high_cond), false_label);
1510 }
1511 // Must be equal high, so compare the lows.
1512 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Mark Mendellc4701932015-04-10 13:18:51 -04001513 }
1514 // The last comparison might be unsigned.
1515 __ j(final_condition, true_label);
1516}
1517
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001518void InstructionCodeGeneratorX86::GenerateFPCompare(Location lhs,
1519 Location rhs,
1520 HInstruction* insn,
1521 bool is_double) {
1522 HX86LoadFromConstantTable* const_area = insn->InputAt(1)->AsX86LoadFromConstantTable();
1523 if (is_double) {
1524 if (rhs.IsFpuRegister()) {
1525 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1526 } else if (const_area != nullptr) {
1527 DCHECK(const_area->IsEmittedAtUseSite());
1528 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(),
1529 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001530 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
1531 const_area->GetBaseMethodAddress(),
1532 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001533 } else {
1534 DCHECK(rhs.IsDoubleStackSlot());
1535 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1536 }
1537 } else {
1538 if (rhs.IsFpuRegister()) {
1539 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1540 } else if (const_area != nullptr) {
1541 DCHECK(const_area->IsEmittedAtUseSite());
1542 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(),
1543 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001544 const_area->GetConstant()->AsFloatConstant()->GetValue(),
1545 const_area->GetBaseMethodAddress(),
1546 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001547 } else {
1548 DCHECK(rhs.IsStackSlot());
1549 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1550 }
1551 }
1552}
1553
Mark Mendell152408f2015-12-31 12:28:50 -05001554template<class LabelType>
David Brazdil0debae72015-11-12 18:37:00 +00001555void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendell152408f2015-12-31 12:28:50 -05001556 LabelType* true_target_in,
1557 LabelType* false_target_in) {
David Brazdil0debae72015-11-12 18:37:00 +00001558 // Generated branching requires both targets to be explicit. If either of the
1559 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Mark Mendell152408f2015-12-31 12:28:50 -05001560 LabelType fallthrough_target;
1561 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1562 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
David Brazdil0debae72015-11-12 18:37:00 +00001563
Mark Mendellc4701932015-04-10 13:18:51 -04001564 LocationSummary* locations = condition->GetLocations();
1565 Location left = locations->InAt(0);
1566 Location right = locations->InAt(1);
1567
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001568 DataType::Type type = condition->InputAt(0)->GetType();
Mark Mendellc4701932015-04-10 13:18:51 -04001569 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001570 case DataType::Type::kInt64:
Mark Mendellc4701932015-04-10 13:18:51 -04001571 GenerateLongComparesAndJumps(condition, true_target, false_target);
1572 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001573 case DataType::Type::kFloat32:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001574 GenerateFPCompare(left, right, condition, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001575 GenerateFPJumps(condition, true_target, false_target);
1576 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001577 case DataType::Type::kFloat64:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001578 GenerateFPCompare(left, right, condition, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001579 GenerateFPJumps(condition, true_target, false_target);
1580 break;
1581 default:
1582 LOG(FATAL) << "Unexpected compare type " << type;
1583 }
1584
David Brazdil0debae72015-11-12 18:37:00 +00001585 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001586 __ jmp(false_target);
1587 }
David Brazdil0debae72015-11-12 18:37:00 +00001588
1589 if (fallthrough_target.IsLinked()) {
1590 __ Bind(&fallthrough_target);
1591 }
Mark Mendellc4701932015-04-10 13:18:51 -04001592}
1593
David Brazdil0debae72015-11-12 18:37:00 +00001594static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1595 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1596 // are set only strictly before `branch`. We can't use the eflags on long/FP
1597 // conditions if they are materialized due to the complex branching.
1598 return cond->IsCondition() &&
1599 cond->GetNext() == branch &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001600 cond->InputAt(0)->GetType() != DataType::Type::kInt64 &&
1601 !DataType::IsFloatingPointType(cond->InputAt(0)->GetType());
David Brazdil0debae72015-11-12 18:37:00 +00001602}
1603
Mark Mendell152408f2015-12-31 12:28:50 -05001604template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001605void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001606 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001607 LabelType* true_target,
1608 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001609 HInstruction* cond = instruction->InputAt(condition_input_index);
1610
1611 if (true_target == nullptr && false_target == nullptr) {
1612 // Nothing to do. The code always falls through.
1613 return;
1614 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001615 // Constant condition, statically compared against "true" (integer value 1).
1616 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001617 if (true_target != nullptr) {
1618 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001619 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001620 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001621 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001622 if (false_target != nullptr) {
1623 __ jmp(false_target);
1624 }
1625 }
1626 return;
1627 }
1628
1629 // The following code generates these patterns:
1630 // (1) true_target == nullptr && false_target != nullptr
1631 // - opposite condition true => branch to false_target
1632 // (2) true_target != nullptr && false_target == nullptr
1633 // - condition true => branch to true_target
1634 // (3) true_target != nullptr && false_target != nullptr
1635 // - condition true => branch to true_target
1636 // - branch to false_target
1637 if (IsBooleanValueOrMaterializedCondition(cond)) {
1638 if (AreEflagsSetFrom(cond, instruction)) {
1639 if (true_target == nullptr) {
1640 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
1641 } else {
1642 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
1643 }
1644 } else {
1645 // Materialized condition, compare against 0.
1646 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1647 if (lhs.IsRegister()) {
1648 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1649 } else {
1650 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1651 }
1652 if (true_target == nullptr) {
1653 __ j(kEqual, false_target);
1654 } else {
1655 __ j(kNotEqual, true_target);
1656 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001657 }
1658 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001659 // Condition has not been materialized, use its inputs as the comparison and
1660 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001661 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001662
1663 // If this is a long or FP comparison that has been folded into
1664 // the HCondition, generate the comparison directly.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001665 DataType::Type type = condition->InputAt(0)->GetType();
1666 if (type == DataType::Type::kInt64 || DataType::IsFloatingPointType(type)) {
David Brazdil0debae72015-11-12 18:37:00 +00001667 GenerateCompareTestAndBranch(condition, true_target, false_target);
1668 return;
1669 }
1670
1671 Location lhs = condition->GetLocations()->InAt(0);
1672 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001673 // LHS is guaranteed to be in a register (see LocationsBuilderX86::HandleCondition).
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001674 codegen_->GenerateIntCompare(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00001675 if (true_target == nullptr) {
1676 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1677 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001678 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001679 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001680 }
David Brazdil0debae72015-11-12 18:37:00 +00001681
1682 // If neither branch falls through (case 3), the conditional branch to `true_target`
1683 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1684 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001685 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001686 }
1687}
1688
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001689void LocationsBuilderX86::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001690 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00001691 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001692 locations->SetInAt(0, Location::Any());
1693 }
1694}
1695
1696void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001697 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1698 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1699 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1700 nullptr : codegen_->GetLabelOf(true_successor);
1701 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1702 nullptr : codegen_->GetLabelOf(false_successor);
1703 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001704}
1705
1706void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001707 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001708 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01001709 InvokeRuntimeCallingConvention calling_convention;
1710 RegisterSet caller_saves = RegisterSet::Empty();
1711 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1712 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00001713 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001714 locations->SetInAt(0, Location::Any());
1715 }
1716}
1717
1718void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001719 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001720 GenerateTestAndBranch<Label>(deoptimize,
1721 /* condition_input_index */ 0,
1722 slow_path->GetEntryLabel(),
1723 /* false_target */ nullptr);
1724}
1725
Mingyao Yang063fc772016-08-02 11:02:54 -07001726void LocationsBuilderX86::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001727 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yang063fc772016-08-02 11:02:54 -07001728 LocationSummary(flag, LocationSummary::kNoCall);
1729 locations->SetOut(Location::RequiresRegister());
1730}
1731
1732void InstructionCodeGeneratorX86::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
1733 __ movl(flag->GetLocations()->Out().AsRegister<Register>(),
1734 Address(ESP, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
1735}
1736
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001737static bool SelectCanUseCMOV(HSelect* select) {
1738 // There are no conditional move instructions for XMMs.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001739 if (DataType::IsFloatingPointType(select->GetType())) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001740 return false;
1741 }
1742
1743 // A FP condition doesn't generate the single CC that we need.
1744 // In 32 bit mode, a long condition doesn't generate a single CC either.
1745 HInstruction* condition = select->GetCondition();
1746 if (condition->IsCondition()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001747 DataType::Type compare_type = condition->InputAt(0)->GetType();
1748 if (compare_type == DataType::Type::kInt64 ||
1749 DataType::IsFloatingPointType(compare_type)) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001750 return false;
1751 }
1752 }
1753
1754 // We can generate a CMOV for this Select.
1755 return true;
1756}
1757
David Brazdil74eb1b22015-12-14 11:44:01 +00001758void LocationsBuilderX86::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001759 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001760 if (DataType::IsFloatingPointType(select->GetType())) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001761 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001762 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001763 } else {
1764 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001765 if (SelectCanUseCMOV(select)) {
1766 if (select->InputAt(1)->IsConstant()) {
1767 // Cmov can't handle a constant value.
1768 locations->SetInAt(1, Location::RequiresRegister());
1769 } else {
1770 locations->SetInAt(1, Location::Any());
1771 }
1772 } else {
1773 locations->SetInAt(1, Location::Any());
1774 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001775 }
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001776 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1777 locations->SetInAt(2, Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00001778 }
1779 locations->SetOut(Location::SameAsFirstInput());
1780}
1781
1782void InstructionCodeGeneratorX86::VisitSelect(HSelect* select) {
1783 LocationSummary* locations = select->GetLocations();
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001784 DCHECK(locations->InAt(0).Equals(locations->Out()));
1785 if (SelectCanUseCMOV(select)) {
1786 // If both the condition and the source types are integer, we can generate
1787 // a CMOV to implement Select.
1788
1789 HInstruction* select_condition = select->GetCondition();
1790 Condition cond = kNotEqual;
1791
1792 // Figure out how to test the 'condition'.
1793 if (select_condition->IsCondition()) {
1794 HCondition* condition = select_condition->AsCondition();
1795 if (!condition->IsEmittedAtUseSite()) {
1796 // This was a previously materialized condition.
1797 // Can we use the existing condition code?
1798 if (AreEflagsSetFrom(condition, select)) {
1799 // Materialization was the previous instruction. Condition codes are right.
1800 cond = X86Condition(condition->GetCondition());
1801 } else {
1802 // No, we have to recreate the condition code.
1803 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1804 __ testl(cond_reg, cond_reg);
1805 }
1806 } else {
1807 // We can't handle FP or long here.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001808 DCHECK_NE(condition->InputAt(0)->GetType(), DataType::Type::kInt64);
1809 DCHECK(!DataType::IsFloatingPointType(condition->InputAt(0)->GetType()));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001810 LocationSummary* cond_locations = condition->GetLocations();
Roland Levillain0b671c02016-08-19 12:02:34 +01001811 codegen_->GenerateIntCompare(cond_locations->InAt(0), cond_locations->InAt(1));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001812 cond = X86Condition(condition->GetCondition());
1813 }
1814 } else {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01001815 // Must be a Boolean condition, which needs to be compared to 0.
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001816 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1817 __ testl(cond_reg, cond_reg);
1818 }
1819
1820 // If the condition is true, overwrite the output, which already contains false.
1821 Location false_loc = locations->InAt(0);
1822 Location true_loc = locations->InAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001823 if (select->GetType() == DataType::Type::kInt64) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001824 // 64 bit conditional move.
1825 Register false_high = false_loc.AsRegisterPairHigh<Register>();
1826 Register false_low = false_loc.AsRegisterPairLow<Register>();
1827 if (true_loc.IsRegisterPair()) {
1828 __ cmovl(cond, false_high, true_loc.AsRegisterPairHigh<Register>());
1829 __ cmovl(cond, false_low, true_loc.AsRegisterPairLow<Register>());
1830 } else {
1831 __ cmovl(cond, false_high, Address(ESP, true_loc.GetHighStackIndex(kX86WordSize)));
1832 __ cmovl(cond, false_low, Address(ESP, true_loc.GetStackIndex()));
1833 }
1834 } else {
1835 // 32 bit conditional move.
1836 Register false_reg = false_loc.AsRegister<Register>();
1837 if (true_loc.IsRegister()) {
1838 __ cmovl(cond, false_reg, true_loc.AsRegister<Register>());
1839 } else {
1840 __ cmovl(cond, false_reg, Address(ESP, true_loc.GetStackIndex()));
1841 }
1842 }
1843 } else {
1844 NearLabel false_target;
1845 GenerateTestAndBranch<NearLabel>(
1846 select, /* condition_input_index */ 2, /* true_target */ nullptr, &false_target);
1847 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1848 __ Bind(&false_target);
1849 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001850}
1851
David Srbecky0cf44932015-12-09 14:09:59 +00001852void LocationsBuilderX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001853 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00001854}
1855
David Srbeckyd28f4a02016-03-14 17:14:24 +00001856void InstructionCodeGeneratorX86::VisitNativeDebugInfo(HNativeDebugInfo*) {
1857 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001858}
1859
1860void CodeGeneratorX86::GenerateNop() {
1861 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001862}
1863
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001864void LocationsBuilderX86::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001865 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01001866 new (GetGraph()->GetAllocator()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001867 // Handle the long/FP comparisons made in instruction simplification.
1868 switch (cond->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001869 case DataType::Type::kInt64: {
Mark Mendellc4701932015-04-10 13:18:51 -04001870 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell8659e842016-02-16 10:41:46 -05001871 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001872 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001873 locations->SetOut(Location::RequiresRegister());
1874 }
1875 break;
1876 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001877 case DataType::Type::kFloat32:
1878 case DataType::Type::kFloat64: {
Mark Mendellc4701932015-04-10 13:18:51 -04001879 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001880 if (cond->InputAt(1)->IsX86LoadFromConstantTable()) {
1881 DCHECK(cond->InputAt(1)->IsEmittedAtUseSite());
1882 } else if (cond->InputAt(1)->IsConstant()) {
1883 locations->SetInAt(1, Location::RequiresFpuRegister());
1884 } else {
1885 locations->SetInAt(1, Location::Any());
1886 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001887 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001888 locations->SetOut(Location::RequiresRegister());
1889 }
1890 break;
1891 }
1892 default:
1893 locations->SetInAt(0, Location::RequiresRegister());
1894 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001895 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001896 // We need a byte register.
1897 locations->SetOut(Location::RegisterLocation(ECX));
1898 }
1899 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001900 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001901}
1902
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001903void InstructionCodeGeneratorX86::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001904 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001905 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001906 }
Mark Mendellc4701932015-04-10 13:18:51 -04001907
1908 LocationSummary* locations = cond->GetLocations();
1909 Location lhs = locations->InAt(0);
1910 Location rhs = locations->InAt(1);
1911 Register reg = locations->Out().AsRegister<Register>();
Mark Mendell152408f2015-12-31 12:28:50 -05001912 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001913
1914 switch (cond->InputAt(0)->GetType()) {
1915 default: {
1916 // Integer case.
1917
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01001918 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04001919 __ xorl(reg, reg);
Roland Levillain0b671c02016-08-19 12:02:34 +01001920 codegen_->GenerateIntCompare(lhs, rhs);
Aart Bike9f37602015-10-09 11:15:55 -07001921 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001922 return;
1923 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001924 case DataType::Type::kInt64:
Mark Mendellc4701932015-04-10 13:18:51 -04001925 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1926 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001927 case DataType::Type::kFloat32:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001928 GenerateFPCompare(lhs, rhs, cond, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001929 GenerateFPJumps(cond, &true_label, &false_label);
1930 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001931 case DataType::Type::kFloat64:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001932 GenerateFPCompare(lhs, rhs, cond, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001933 GenerateFPJumps(cond, &true_label, &false_label);
1934 break;
1935 }
1936
1937 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001938 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001939
Roland Levillain4fa13f62015-07-06 18:11:54 +01001940 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001941 __ Bind(&false_label);
1942 __ xorl(reg, reg);
1943 __ jmp(&done_label);
1944
Roland Levillain4fa13f62015-07-06 18:11:54 +01001945 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001946 __ Bind(&true_label);
1947 __ movl(reg, Immediate(1));
1948 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001949}
1950
1951void LocationsBuilderX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001952 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001953}
1954
1955void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001956 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001957}
1958
1959void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001960 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001961}
1962
1963void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001964 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001965}
1966
1967void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001968 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001969}
1970
1971void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001972 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001973}
1974
1975void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001976 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001977}
1978
1979void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001980 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001981}
1982
1983void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001984 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001985}
1986
1987void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001988 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001989}
1990
1991void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001992 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001993}
1994
1995void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001996 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001997}
1998
Aart Bike9f37602015-10-09 11:15:55 -07001999void LocationsBuilderX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002000 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002001}
2002
2003void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002004 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002005}
2006
2007void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002008 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002009}
2010
2011void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002012 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002013}
2014
2015void LocationsBuilderX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002016 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002017}
2018
2019void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002020 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002021}
2022
2023void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002024 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002025}
2026
2027void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002028 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002029}
2030
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002031void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002032 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002033 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002034 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002035}
2036
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002037void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002038 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002039}
2040
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002041void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
2042 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002043 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002044 locations->SetOut(Location::ConstantLocation(constant));
2045}
2046
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002047void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002048 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002049}
2050
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002051void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002052 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002053 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002054 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002055}
2056
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002057void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002058 // Will be generated at use site.
2059}
2060
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002061void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
2062 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002063 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002064 locations->SetOut(Location::ConstantLocation(constant));
2065}
2066
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002067void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002068 // Will be generated at use site.
2069}
2070
2071void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
2072 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002073 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002074 locations->SetOut(Location::ConstantLocation(constant));
2075}
2076
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002077void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002078 // Will be generated at use site.
2079}
2080
Igor Murashkind01745e2017-04-05 16:40:31 -07002081void LocationsBuilderX86::VisitConstructorFence(HConstructorFence* constructor_fence) {
2082 constructor_fence->SetLocations(nullptr);
2083}
2084
2085void InstructionCodeGeneratorX86::VisitConstructorFence(
2086 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
2087 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
2088}
2089
Calin Juravle27df7582015-04-17 19:12:31 +01002090void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2091 memory_barrier->SetLocations(nullptr);
2092}
2093
2094void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00002095 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002096}
2097
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002098void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002099 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002100}
2101
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002102void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002103 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002104}
2105
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002106void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002107 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002108 new (GetGraph()->GetAllocator()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002109 switch (ret->InputAt(0)->GetType()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002110 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002111 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002112 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002113 case DataType::Type::kInt8:
2114 case DataType::Type::kUint16:
2115 case DataType::Type::kInt16:
2116 case DataType::Type::kInt32:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002117 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002118 break;
2119
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002120 case DataType::Type::kInt64:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002121 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002122 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002123 break;
2124
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002125 case DataType::Type::kFloat32:
2126 case DataType::Type::kFloat64:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002127 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002128 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002129 break;
2130
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002131 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002132 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002133 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002134}
2135
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002136void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002137 if (kIsDebugBuild) {
2138 switch (ret->InputAt(0)->GetType()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002139 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002140 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002141 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002142 case DataType::Type::kInt8:
2143 case DataType::Type::kUint16:
2144 case DataType::Type::kInt16:
2145 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002146 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002147 break;
2148
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002149 case DataType::Type::kInt64:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002150 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
2151 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002152 break;
2153
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002154 case DataType::Type::kFloat32:
2155 case DataType::Type::kFloat64:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002156 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002157 break;
2158
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002159 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002160 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002161 }
2162 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002163 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002164}
2165
Calin Juravle175dc732015-08-25 15:42:32 +01002166void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2167 // The trampoline uses the same calling convention as dex calling conventions,
2168 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2169 // the method_idx.
2170 HandleInvoke(invoke);
2171}
2172
2173void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2174 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2175}
2176
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002177void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002178 // Explicit clinit checks triggered by static invokes must have been pruned by
2179 // art::PrepareForRegisterAllocation.
2180 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002181
Mark Mendellfb8d2792015-03-31 22:16:59 -04002182 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002183 if (intrinsic.TryDispatch(invoke)) {
Vladimir Marko65979462017-05-19 17:25:12 +01002184 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeMethodLoadKind()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00002185 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002186 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04002187 return;
2188 }
2189
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002190 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002191
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002192 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
Vladimir Marko65979462017-05-19 17:25:12 +01002193 if (invoke->HasPcRelativeMethodLoadKind()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00002194 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002195 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002196}
2197
Mark Mendell09ed1a32015-03-25 08:30:06 -04002198static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
2199 if (invoke->GetLocations()->Intrinsified()) {
2200 IntrinsicCodeGeneratorX86 intrinsic(codegen);
2201 intrinsic.Dispatch(invoke);
2202 return true;
2203 }
2204 return false;
2205}
2206
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002207void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002208 // Explicit clinit checks triggered by static invokes must have been pruned by
2209 // art::PrepareForRegisterAllocation.
2210 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002211
Mark Mendell09ed1a32015-03-25 08:30:06 -04002212 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2213 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002214 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002215
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002216 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04002217 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002218 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002219}
2220
2221void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00002222 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
2223 if (intrinsic.TryDispatch(invoke)) {
2224 return;
2225 }
2226
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002227 HandleInvoke(invoke);
2228}
2229
2230void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002231 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002232 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002233}
2234
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002235void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002236 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2237 return;
2238 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002239
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002240 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002241 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002242}
2243
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002244void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002245 // This call to HandleInvoke allocates a temporary (core) register
2246 // which is also used to transfer the hidden argument from FP to
2247 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002248 HandleInvoke(invoke);
2249 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002250 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002251}
2252
2253void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
2254 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002255 LocationSummary* locations = invoke->GetLocations();
2256 Register temp = locations->GetTemp(0).AsRegister<Register>();
2257 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002258 Location receiver = locations->InAt(0);
2259 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2260
Roland Levillain0d5a2812015-11-13 10:07:31 +00002261 // Set the hidden argument. This is safe to do this here, as XMM7
2262 // won't be modified thereafter, before the `call` instruction.
2263 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002264 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002265 __ movd(hidden_reg, temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002266
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002267 if (receiver.IsStackSlot()) {
2268 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002269 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002270 __ movl(temp, Address(temp, class_offset));
2271 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002272 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002273 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002274 }
Roland Levillain4d027112015-07-01 15:41:14 +01002275 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002276 // Instead of simply (possibly) unpoisoning `temp` here, we should
2277 // emit a read barrier for the previous class reference load.
2278 // However this is not required in practice, as this is an
2279 // intermediate/temporary reference and because the current
2280 // concurrent copying collector keeps the from-space memory
2281 // intact/accessible until the end of the marking phase (the
2282 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002283 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002284 // temp = temp->GetAddressOfIMT()
2285 __ movl(temp,
2286 Address(temp, mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002287 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002288 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002289 invoke->GetImtIndex(), kX86PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002290 __ movl(temp, Address(temp, method_offset));
2291 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002292 __ call(Address(temp,
Andreas Gampe542451c2016-07-26 09:02:02 -07002293 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002294
2295 DCHECK(!codegen_->IsLeafMethod());
2296 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2297}
2298
Orion Hodsonac141392017-01-13 11:53:47 +00002299void LocationsBuilderX86::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2300 HandleInvoke(invoke);
2301}
2302
2303void InstructionCodeGeneratorX86::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2304 codegen_->GenerateInvokePolymorphicCall(invoke);
2305}
2306
Roland Levillain88cb1752014-10-20 16:36:47 +01002307void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2308 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002309 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Roland Levillain88cb1752014-10-20 16:36:47 +01002310 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002311 case DataType::Type::kInt32:
2312 case DataType::Type::kInt64:
Roland Levillain88cb1752014-10-20 16:36:47 +01002313 locations->SetInAt(0, Location::RequiresRegister());
2314 locations->SetOut(Location::SameAsFirstInput());
2315 break;
2316
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002317 case DataType::Type::kFloat32:
Roland Levillain5368c212014-11-27 15:03:41 +00002318 locations->SetInAt(0, Location::RequiresFpuRegister());
2319 locations->SetOut(Location::SameAsFirstInput());
2320 locations->AddTemp(Location::RequiresRegister());
2321 locations->AddTemp(Location::RequiresFpuRegister());
2322 break;
2323
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002324 case DataType::Type::kFloat64:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002325 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002326 locations->SetOut(Location::SameAsFirstInput());
2327 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002328 break;
2329
2330 default:
2331 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2332 }
2333}
2334
2335void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2336 LocationSummary* locations = neg->GetLocations();
2337 Location out = locations->Out();
2338 Location in = locations->InAt(0);
2339 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002340 case DataType::Type::kInt32:
Roland Levillain88cb1752014-10-20 16:36:47 +01002341 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002342 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002343 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002344 break;
2345
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002346 case DataType::Type::kInt64:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002347 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002348 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002349 __ negl(out.AsRegisterPairLow<Register>());
2350 // Negation is similar to subtraction from zero. The least
2351 // significant byte triggers a borrow when it is different from
2352 // zero; to take it into account, add 1 to the most significant
2353 // byte if the carry flag (CF) is set to 1 after the first NEGL
2354 // operation.
2355 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2356 __ negl(out.AsRegisterPairHigh<Register>());
2357 break;
2358
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002359 case DataType::Type::kFloat32: {
Roland Levillain5368c212014-11-27 15:03:41 +00002360 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002361 Register constant = locations->GetTemp(0).AsRegister<Register>();
2362 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002363 // Implement float negation with an exclusive or with value
2364 // 0x80000000 (mask for bit 31, representing the sign of a
2365 // single-precision floating-point number).
2366 __ movl(constant, Immediate(INT32_C(0x80000000)));
2367 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002368 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002369 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002370 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002371
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002372 case DataType::Type::kFloat64: {
Roland Levillain5368c212014-11-27 15:03:41 +00002373 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002374 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002375 // Implement double negation with an exclusive or with value
2376 // 0x8000000000000000 (mask for bit 63, representing the sign of
2377 // a double-precision floating-point number).
2378 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002379 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002380 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002381 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002382
2383 default:
2384 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2385 }
2386}
2387
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002388void LocationsBuilderX86::VisitX86FPNeg(HX86FPNeg* neg) {
2389 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002390 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002391 DCHECK(DataType::IsFloatingPointType(neg->GetType()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002392 locations->SetInAt(0, Location::RequiresFpuRegister());
2393 locations->SetInAt(1, Location::RequiresRegister());
2394 locations->SetOut(Location::SameAsFirstInput());
2395 locations->AddTemp(Location::RequiresFpuRegister());
2396}
2397
2398void InstructionCodeGeneratorX86::VisitX86FPNeg(HX86FPNeg* neg) {
2399 LocationSummary* locations = neg->GetLocations();
2400 Location out = locations->Out();
2401 DCHECK(locations->InAt(0).Equals(out));
2402
2403 Register constant_area = locations->InAt(1).AsRegister<Register>();
2404 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002405 if (neg->GetType() == DataType::Type::kFloat32) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00002406 __ movss(mask, codegen_->LiteralInt32Address(INT32_C(0x80000000),
2407 neg->GetBaseMethodAddress(),
2408 constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002409 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
2410 } else {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00002411 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000),
2412 neg->GetBaseMethodAddress(),
2413 constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002414 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
2415 }
2416}
2417
Roland Levillaindff1f282014-11-05 14:15:05 +00002418void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002419 DataType::Type result_type = conversion->GetResultType();
2420 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002421 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
2422 << input_type << " -> " << result_type;
Roland Levillain624279f2014-12-04 11:54:28 +00002423
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002424 // The float-to-long and double-to-long type conversions rely on a
2425 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002426 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002427 ((input_type == DataType::Type::kFloat32 || input_type == DataType::Type::kFloat64)
2428 && result_type == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002429 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002430 : LocationSummary::kNoCall;
2431 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002432 new (GetGraph()->GetAllocator()) LocationSummary(conversion, call_kind);
Roland Levillain624279f2014-12-04 11:54:28 +00002433
Roland Levillaindff1f282014-11-05 14:15:05 +00002434 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002435 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002436 case DataType::Type::kInt8:
Roland Levillain51d3fc42014-11-13 14:11:42 +00002437 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002438 case DataType::Type::kUint8:
2439 case DataType::Type::kInt8:
2440 case DataType::Type::kUint16:
2441 case DataType::Type::kInt16:
2442 case DataType::Type::kInt32:
2443 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2444 // Make the output overlap to please the register allocator. This greatly simplifies
2445 // the validation of the linear scan implementation
2446 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2447 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002448 case DataType::Type::kInt64: {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002449 HInstruction* input = conversion->InputAt(0);
2450 Location input_location = input->IsConstant()
2451 ? Location::ConstantLocation(input->AsConstant())
2452 : Location::RegisterPairLocation(EAX, EDX);
2453 locations->SetInAt(0, input_location);
2454 // Make the output overlap to please the register allocator. This greatly simplifies
2455 // the validation of the linear scan implementation
2456 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2457 break;
2458 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002459
2460 default:
2461 LOG(FATAL) << "Unexpected type conversion from " << input_type
2462 << " to " << result_type;
2463 }
2464 break;
2465
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002466 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002467 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002468 DCHECK(DataType::IsIntegralType(input_type)) << input_type;
2469 locations->SetInAt(0, Location::Any());
2470 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Roland Levillain01a8d712014-11-14 16:27:39 +00002471 break;
2472
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002473 case DataType::Type::kInt32:
Roland Levillain946e1432014-11-11 17:35:19 +00002474 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002475 case DataType::Type::kInt64:
Roland Levillain946e1432014-11-11 17:35:19 +00002476 locations->SetInAt(0, Location::Any());
2477 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2478 break;
2479
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002480 case DataType::Type::kFloat32:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002481 locations->SetInAt(0, Location::RequiresFpuRegister());
2482 locations->SetOut(Location::RequiresRegister());
2483 locations->AddTemp(Location::RequiresFpuRegister());
2484 break;
2485
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002486 case DataType::Type::kFloat64:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002487 locations->SetInAt(0, Location::RequiresFpuRegister());
2488 locations->SetOut(Location::RequiresRegister());
2489 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002490 break;
2491
2492 default:
2493 LOG(FATAL) << "Unexpected type conversion from " << input_type
2494 << " to " << result_type;
2495 }
2496 break;
2497
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002498 case DataType::Type::kInt64:
Roland Levillaindff1f282014-11-05 14:15:05 +00002499 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002500 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002501 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002502 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002503 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002504 case DataType::Type::kInt16:
2505 case DataType::Type::kInt32:
Roland Levillaindff1f282014-11-05 14:15:05 +00002506 locations->SetInAt(0, Location::RegisterLocation(EAX));
2507 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2508 break;
2509
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002510 case DataType::Type::kFloat32:
2511 case DataType::Type::kFloat64: {
Vladimir Marko949c91f2015-01-27 10:48:44 +00002512 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002513 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2514 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2515
Vladimir Marko949c91f2015-01-27 10:48:44 +00002516 // The runtime helper puts the result in EAX, EDX.
2517 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002518 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002519 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002520
2521 default:
2522 LOG(FATAL) << "Unexpected type conversion from " << input_type
2523 << " to " << result_type;
2524 }
2525 break;
2526
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002527 case DataType::Type::kFloat32:
Roland Levillaincff13742014-11-17 14:32:17 +00002528 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002529 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002530 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002531 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002532 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002533 case DataType::Type::kInt16:
2534 case DataType::Type::kInt32:
Roland Levillaincff13742014-11-17 14:32:17 +00002535 locations->SetInAt(0, Location::RequiresRegister());
2536 locations->SetOut(Location::RequiresFpuRegister());
2537 break;
2538
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002539 case DataType::Type::kInt64:
Roland Levillain232ade02015-04-20 15:14:36 +01002540 locations->SetInAt(0, Location::Any());
2541 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002542 break;
2543
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002544 case DataType::Type::kFloat64:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002545 locations->SetInAt(0, Location::RequiresFpuRegister());
2546 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002547 break;
2548
2549 default:
2550 LOG(FATAL) << "Unexpected type conversion from " << input_type
2551 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08002552 }
Roland Levillaincff13742014-11-17 14:32:17 +00002553 break;
2554
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002555 case DataType::Type::kFloat64:
Roland Levillaincff13742014-11-17 14:32:17 +00002556 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002557 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002558 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002559 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002560 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002561 case DataType::Type::kInt16:
2562 case DataType::Type::kInt32:
Roland Levillaincff13742014-11-17 14:32:17 +00002563 locations->SetInAt(0, Location::RequiresRegister());
2564 locations->SetOut(Location::RequiresFpuRegister());
2565 break;
2566
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002567 case DataType::Type::kInt64:
Roland Levillain232ade02015-04-20 15:14:36 +01002568 locations->SetInAt(0, Location::Any());
2569 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002570 break;
2571
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002572 case DataType::Type::kFloat32:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002573 locations->SetInAt(0, Location::RequiresFpuRegister());
2574 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002575 break;
2576
2577 default:
2578 LOG(FATAL) << "Unexpected type conversion from " << input_type
2579 << " to " << result_type;
2580 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002581 break;
2582
2583 default:
2584 LOG(FATAL) << "Unexpected type conversion from " << input_type
2585 << " to " << result_type;
2586 }
2587}
2588
2589void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2590 LocationSummary* locations = conversion->GetLocations();
2591 Location out = locations->Out();
2592 Location in = locations->InAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002593 DataType::Type result_type = conversion->GetResultType();
2594 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002595 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
2596 << input_type << " -> " << result_type;
Roland Levillaindff1f282014-11-05 14:15:05 +00002597 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002598 case DataType::Type::kUint8:
2599 switch (input_type) {
2600 case DataType::Type::kInt8:
2601 case DataType::Type::kUint16:
2602 case DataType::Type::kInt16:
2603 case DataType::Type::kInt32:
2604 if (in.IsRegister()) {
2605 __ movzxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
2606 } else {
2607 DCHECK(in.GetConstant()->IsIntConstant());
2608 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2609 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint8_t>(value)));
2610 }
2611 break;
2612 case DataType::Type::kInt64:
2613 if (in.IsRegisterPair()) {
2614 __ movzxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2615 } else {
2616 DCHECK(in.GetConstant()->IsLongConstant());
2617 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2618 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint8_t>(value)));
2619 }
2620 break;
2621
2622 default:
2623 LOG(FATAL) << "Unexpected type conversion from " << input_type
2624 << " to " << result_type;
2625 }
2626 break;
2627
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002628 case DataType::Type::kInt8:
Roland Levillain51d3fc42014-11-13 14:11:42 +00002629 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002630 case DataType::Type::kUint8:
2631 case DataType::Type::kUint16:
2632 case DataType::Type::kInt16:
2633 case DataType::Type::kInt32:
2634 if (in.IsRegister()) {
2635 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
2636 } else {
2637 DCHECK(in.GetConstant()->IsIntConstant());
2638 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2639 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2640 }
2641 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002642 case DataType::Type::kInt64:
Vladimir Markob52bbde2016-02-12 12:06:05 +00002643 if (in.IsRegisterPair()) {
2644 __ movsxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2645 } else {
2646 DCHECK(in.GetConstant()->IsLongConstant());
2647 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2648 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2649 }
2650 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002651
2652 default:
2653 LOG(FATAL) << "Unexpected type conversion from " << input_type
2654 << " to " << result_type;
2655 }
2656 break;
2657
2658 case DataType::Type::kUint16:
2659 switch (input_type) {
2660 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002661 case DataType::Type::kInt16:
2662 case DataType::Type::kInt32:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002663 if (in.IsRegister()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002664 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
2665 } else if (in.IsStackSlot()) {
2666 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002667 } else {
2668 DCHECK(in.GetConstant()->IsIntConstant());
2669 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002670 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
2671 }
2672 break;
2673 case DataType::Type::kInt64:
2674 if (in.IsRegisterPair()) {
2675 __ movzxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2676 } else if (in.IsDoubleStackSlot()) {
2677 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2678 } else {
2679 DCHECK(in.GetConstant()->IsLongConstant());
2680 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2681 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002682 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002683 break;
2684
2685 default:
2686 LOG(FATAL) << "Unexpected type conversion from " << input_type
2687 << " to " << result_type;
2688 }
2689 break;
2690
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002691 case DataType::Type::kInt16:
Roland Levillain01a8d712014-11-14 16:27:39 +00002692 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002693 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002694 case DataType::Type::kInt32:
Roland Levillain01a8d712014-11-14 16:27:39 +00002695 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002696 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002697 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002698 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002699 } else {
2700 DCHECK(in.GetConstant()->IsIntConstant());
2701 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002702 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002703 }
2704 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002705 case DataType::Type::kInt64:
2706 if (in.IsRegisterPair()) {
2707 __ movsxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2708 } else if (in.IsDoubleStackSlot()) {
2709 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2710 } else {
2711 DCHECK(in.GetConstant()->IsLongConstant());
2712 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2713 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
2714 }
2715 break;
Roland Levillain01a8d712014-11-14 16:27:39 +00002716
2717 default:
2718 LOG(FATAL) << "Unexpected type conversion from " << input_type
2719 << " to " << result_type;
2720 }
2721 break;
2722
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002723 case DataType::Type::kInt32:
Roland Levillain946e1432014-11-11 17:35:19 +00002724 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002725 case DataType::Type::kInt64:
Roland Levillain946e1432014-11-11 17:35:19 +00002726 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002727 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002728 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002729 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002730 } else {
2731 DCHECK(in.IsConstant());
2732 DCHECK(in.GetConstant()->IsLongConstant());
2733 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002734 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002735 }
2736 break;
2737
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002738 case DataType::Type::kFloat32: {
Roland Levillain3f8f9362014-12-02 17:45:01 +00002739 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2740 Register output = out.AsRegister<Register>();
2741 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002742 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002743
2744 __ movl(output, Immediate(kPrimIntMax));
2745 // temp = int-to-float(output)
2746 __ cvtsi2ss(temp, output);
2747 // if input >= temp goto done
2748 __ comiss(input, temp);
2749 __ j(kAboveEqual, &done);
2750 // if input == NaN goto nan
2751 __ j(kUnordered, &nan);
2752 // output = float-to-int-truncate(input)
2753 __ cvttss2si(output, input);
2754 __ jmp(&done);
2755 __ Bind(&nan);
2756 // output = 0
2757 __ xorl(output, output);
2758 __ Bind(&done);
2759 break;
2760 }
2761
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002762 case DataType::Type::kFloat64: {
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002763 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2764 Register output = out.AsRegister<Register>();
2765 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002766 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002767
2768 __ movl(output, Immediate(kPrimIntMax));
2769 // temp = int-to-double(output)
2770 __ cvtsi2sd(temp, output);
2771 // if input >= temp goto done
2772 __ comisd(input, temp);
2773 __ j(kAboveEqual, &done);
2774 // if input == NaN goto nan
2775 __ j(kUnordered, &nan);
2776 // output = double-to-int-truncate(input)
2777 __ cvttsd2si(output, input);
2778 __ jmp(&done);
2779 __ Bind(&nan);
2780 // output = 0
2781 __ xorl(output, output);
2782 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002783 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002784 }
Roland Levillain946e1432014-11-11 17:35:19 +00002785
2786 default:
2787 LOG(FATAL) << "Unexpected type conversion from " << input_type
2788 << " to " << result_type;
2789 }
2790 break;
2791
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002792 case DataType::Type::kInt64:
Roland Levillaindff1f282014-11-05 14:15:05 +00002793 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002794 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002795 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002796 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002797 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002798 case DataType::Type::kInt16:
2799 case DataType::Type::kInt32:
Roland Levillaindff1f282014-11-05 14:15:05 +00002800 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2801 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002802 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002803 __ cdq();
2804 break;
2805
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002806 case DataType::Type::kFloat32:
Serban Constantinescuba45db02016-07-12 22:53:02 +01002807 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002808 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002809 break;
2810
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002811 case DataType::Type::kFloat64:
Serban Constantinescuba45db02016-07-12 22:53:02 +01002812 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002813 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002814 break;
2815
2816 default:
2817 LOG(FATAL) << "Unexpected type conversion from " << input_type
2818 << " to " << result_type;
2819 }
2820 break;
2821
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002822 case DataType::Type::kFloat32:
Roland Levillaincff13742014-11-17 14:32:17 +00002823 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002824 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002825 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002826 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002827 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002828 case DataType::Type::kInt16:
2829 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002830 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002831 break;
2832
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002833 case DataType::Type::kInt64: {
Roland Levillain232ade02015-04-20 15:14:36 +01002834 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002835
Roland Levillain232ade02015-04-20 15:14:36 +01002836 // Create stack space for the call to
2837 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2838 // TODO: enhance register allocator to ask for stack temporaries.
2839 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002840 adjustment = DataType::Size(DataType::Type::kInt64);
Roland Levillain232ade02015-04-20 15:14:36 +01002841 __ subl(ESP, Immediate(adjustment));
2842 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002843
Roland Levillain232ade02015-04-20 15:14:36 +01002844 // Load the value to the FP stack, using temporaries if needed.
2845 PushOntoFPStack(in, 0, adjustment, false, true);
2846
2847 if (out.IsStackSlot()) {
2848 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2849 } else {
2850 __ fstps(Address(ESP, 0));
2851 Location stack_temp = Location::StackSlot(0);
2852 codegen_->Move32(out, stack_temp);
2853 }
2854
2855 // Remove the temporary stack space we allocated.
2856 if (adjustment != 0) {
2857 __ addl(ESP, Immediate(adjustment));
2858 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002859 break;
2860 }
2861
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002862 case DataType::Type::kFloat64:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002863 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002864 break;
2865
2866 default:
2867 LOG(FATAL) << "Unexpected type conversion from " << input_type
2868 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08002869 }
Roland Levillaincff13742014-11-17 14:32:17 +00002870 break;
2871
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002872 case DataType::Type::kFloat64:
Roland Levillaincff13742014-11-17 14:32:17 +00002873 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002874 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002875 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002876 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002877 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002878 case DataType::Type::kInt16:
2879 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002880 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002881 break;
2882
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002883 case DataType::Type::kInt64: {
Roland Levillain232ade02015-04-20 15:14:36 +01002884 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002885
Roland Levillain232ade02015-04-20 15:14:36 +01002886 // Create stack space for the call to
2887 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2888 // TODO: enhance register allocator to ask for stack temporaries.
2889 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002890 adjustment = DataType::Size(DataType::Type::kInt64);
Roland Levillain232ade02015-04-20 15:14:36 +01002891 __ subl(ESP, Immediate(adjustment));
2892 }
2893
2894 // Load the value to the FP stack, using temporaries if needed.
2895 PushOntoFPStack(in, 0, adjustment, false, true);
2896
2897 if (out.IsDoubleStackSlot()) {
2898 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2899 } else {
2900 __ fstpl(Address(ESP, 0));
2901 Location stack_temp = Location::DoubleStackSlot(0);
2902 codegen_->Move64(out, stack_temp);
2903 }
2904
2905 // Remove the temporary stack space we allocated.
2906 if (adjustment != 0) {
2907 __ addl(ESP, Immediate(adjustment));
2908 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002909 break;
2910 }
2911
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002912 case DataType::Type::kFloat32:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002913 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002914 break;
2915
2916 default:
2917 LOG(FATAL) << "Unexpected type conversion from " << input_type
2918 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08002919 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002920 break;
2921
2922 default:
2923 LOG(FATAL) << "Unexpected type conversion from " << input_type
2924 << " to " << result_type;
2925 }
2926}
2927
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002928void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002929 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002930 new (GetGraph()->GetAllocator()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002931 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002932 case DataType::Type::kInt32: {
Mark Mendell09b84632015-02-13 17:48:38 -05002933 locations->SetInAt(0, Location::RequiresRegister());
2934 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2935 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2936 break;
2937 }
2938
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002939 case DataType::Type::kInt64: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002940 locations->SetInAt(0, Location::RequiresRegister());
2941 locations->SetInAt(1, Location::Any());
2942 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002943 break;
2944 }
2945
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002946 case DataType::Type::kFloat32:
2947 case DataType::Type::kFloat64: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002948 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002949 if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2950 DCHECK(add->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002951 } else if (add->InputAt(1)->IsConstant()) {
2952 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002953 } else {
2954 locations->SetInAt(1, Location::Any());
2955 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002956 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002957 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002958 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002959
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002960 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002961 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2962 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002963 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002964}
2965
2966void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2967 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002968 Location first = locations->InAt(0);
2969 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002970 Location out = locations->Out();
2971
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002972 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002973 case DataType::Type::kInt32: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002974 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002975 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2976 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002977 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2978 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05002979 } else {
2980 __ leal(out.AsRegister<Register>(), Address(
2981 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
2982 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002983 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002984 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
2985 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2986 __ addl(out.AsRegister<Register>(), Immediate(value));
2987 } else {
2988 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
2989 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002990 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05002991 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002992 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002993 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002994 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002995 }
2996
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002997 case DataType::Type::kInt64: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002998 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002999 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3000 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003001 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003002 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3003 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003004 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003005 } else {
3006 DCHECK(second.IsConstant()) << second;
3007 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3008 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3009 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003010 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003011 break;
3012 }
3013
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003014 case DataType::Type::kFloat32: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003015 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003016 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04003017 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3018 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003019 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003020 __ addss(first.AsFpuRegister<XmmRegister>(),
3021 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003022 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3023 const_area->GetBaseMethodAddress(),
3024 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003025 } else {
3026 DCHECK(second.IsStackSlot());
3027 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003028 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003029 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003030 }
3031
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003032 case DataType::Type::kFloat64: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003033 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003034 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04003035 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3036 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003037 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003038 __ addsd(first.AsFpuRegister<XmmRegister>(),
3039 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003040 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3041 const_area->GetBaseMethodAddress(),
3042 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003043 } else {
3044 DCHECK(second.IsDoubleStackSlot());
3045 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003046 }
3047 break;
3048 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003049
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003050 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003051 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003052 }
3053}
3054
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003055void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003056 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003057 new (GetGraph()->GetAllocator()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003058 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003059 case DataType::Type::kInt32:
3060 case DataType::Type::kInt64: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003061 locations->SetInAt(0, Location::RequiresRegister());
3062 locations->SetInAt(1, Location::Any());
3063 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003064 break;
3065 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003066 case DataType::Type::kFloat32:
3067 case DataType::Type::kFloat64: {
Calin Juravle11351682014-10-23 15:38:15 +01003068 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003069 if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3070 DCHECK(sub->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003071 } else if (sub->InputAt(1)->IsConstant()) {
3072 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003073 } else {
3074 locations->SetInAt(1, Location::Any());
3075 }
Calin Juravle11351682014-10-23 15:38:15 +01003076 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003077 break;
Calin Juravle11351682014-10-23 15:38:15 +01003078 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003079
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003080 default:
Calin Juravle11351682014-10-23 15:38:15 +01003081 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003082 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003083}
3084
3085void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
3086 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003087 Location first = locations->InAt(0);
3088 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01003089 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003090 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003091 case DataType::Type::kInt32: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003092 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003093 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003094 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003095 __ subl(first.AsRegister<Register>(),
3096 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003097 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003098 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003099 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003100 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003101 }
3102
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003103 case DataType::Type::kInt64: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003104 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01003105 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3106 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003107 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01003108 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003109 __ sbbl(first.AsRegisterPairHigh<Register>(),
3110 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003111 } else {
3112 DCHECK(second.IsConstant()) << second;
3113 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3114 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3115 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003116 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003117 break;
3118 }
3119
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003120 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003121 if (second.IsFpuRegister()) {
3122 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3123 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3124 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003125 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003126 __ subss(first.AsFpuRegister<XmmRegister>(),
3127 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003128 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3129 const_area->GetBaseMethodAddress(),
3130 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003131 } else {
3132 DCHECK(second.IsStackSlot());
3133 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3134 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003135 break;
Calin Juravle11351682014-10-23 15:38:15 +01003136 }
3137
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003138 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003139 if (second.IsFpuRegister()) {
3140 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3141 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3142 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003143 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003144 __ subsd(first.AsFpuRegister<XmmRegister>(),
3145 codegen_->LiteralDoubleAddress(
3146 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003147 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003148 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3149 } else {
3150 DCHECK(second.IsDoubleStackSlot());
3151 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3152 }
Calin Juravle11351682014-10-23 15:38:15 +01003153 break;
3154 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003155
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003156 default:
Calin Juravle11351682014-10-23 15:38:15 +01003157 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003158 }
3159}
3160
Calin Juravle34bacdf2014-10-07 20:23:36 +01003161void LocationsBuilderX86::VisitMul(HMul* mul) {
3162 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003163 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Calin Juravle34bacdf2014-10-07 20:23:36 +01003164 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003165 case DataType::Type::kInt32:
Calin Juravle34bacdf2014-10-07 20:23:36 +01003166 locations->SetInAt(0, Location::RequiresRegister());
3167 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003168 if (mul->InputAt(1)->IsIntConstant()) {
3169 // Can use 3 operand multiply.
3170 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3171 } else {
3172 locations->SetOut(Location::SameAsFirstInput());
3173 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003174 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003175 case DataType::Type::kInt64: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003176 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003177 locations->SetInAt(1, Location::Any());
3178 locations->SetOut(Location::SameAsFirstInput());
3179 // Needed for imul on 32bits with 64bits output.
3180 locations->AddTemp(Location::RegisterLocation(EAX));
3181 locations->AddTemp(Location::RegisterLocation(EDX));
3182 break;
3183 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003184 case DataType::Type::kFloat32:
3185 case DataType::Type::kFloat64: {
Calin Juravleb5bfa962014-10-21 18:02:24 +01003186 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003187 if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3188 DCHECK(mul->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003189 } else if (mul->InputAt(1)->IsConstant()) {
3190 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003191 } else {
3192 locations->SetInAt(1, Location::Any());
3193 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003194 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003195 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003196 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003197
3198 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003199 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003200 }
3201}
3202
3203void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
3204 LocationSummary* locations = mul->GetLocations();
3205 Location first = locations->InAt(0);
3206 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003207 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003208
3209 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003210 case DataType::Type::kInt32:
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003211 // The constant may have ended up in a register, so test explicitly to avoid
3212 // problems where the output may not be the same as the first operand.
3213 if (mul->InputAt(1)->IsIntConstant()) {
3214 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3215 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
3216 } else if (second.IsRegister()) {
3217 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003218 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003219 } else {
3220 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003221 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003222 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003223 }
3224 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003225
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003226 case DataType::Type::kInt64: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003227 Register in1_hi = first.AsRegisterPairHigh<Register>();
3228 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003229 Register eax = locations->GetTemp(0).AsRegister<Register>();
3230 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003231
3232 DCHECK_EQ(EAX, eax);
3233 DCHECK_EQ(EDX, edx);
3234
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003235 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01003236 // output: in1
3237 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3238 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3239 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003240 if (second.IsConstant()) {
3241 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003242
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003243 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3244 int32_t low_value = Low32Bits(value);
3245 int32_t high_value = High32Bits(value);
3246 Immediate low(low_value);
3247 Immediate high(high_value);
3248
3249 __ movl(eax, high);
3250 // eax <- in1.lo * in2.hi
3251 __ imull(eax, in1_lo);
3252 // in1.hi <- in1.hi * in2.lo
3253 __ imull(in1_hi, low);
3254 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3255 __ addl(in1_hi, eax);
3256 // move in2_lo to eax to prepare for double precision
3257 __ movl(eax, low);
3258 // edx:eax <- in1.lo * in2.lo
3259 __ mull(in1_lo);
3260 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3261 __ addl(in1_hi, edx);
3262 // in1.lo <- (in1.lo * in2.lo)[31:0];
3263 __ movl(in1_lo, eax);
3264 } else if (second.IsRegisterPair()) {
3265 Register in2_hi = second.AsRegisterPairHigh<Register>();
3266 Register in2_lo = second.AsRegisterPairLow<Register>();
3267
3268 __ movl(eax, in2_hi);
3269 // eax <- in1.lo * in2.hi
3270 __ imull(eax, in1_lo);
3271 // in1.hi <- in1.hi * in2.lo
3272 __ imull(in1_hi, in2_lo);
3273 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3274 __ addl(in1_hi, eax);
3275 // move in1_lo to eax to prepare for double precision
3276 __ movl(eax, in1_lo);
3277 // edx:eax <- in1.lo * in2.lo
3278 __ mull(in2_lo);
3279 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3280 __ addl(in1_hi, edx);
3281 // in1.lo <- (in1.lo * in2.lo)[31:0];
3282 __ movl(in1_lo, eax);
3283 } else {
3284 DCHECK(second.IsDoubleStackSlot()) << second;
3285 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
3286 Address in2_lo(ESP, second.GetStackIndex());
3287
3288 __ movl(eax, in2_hi);
3289 // eax <- in1.lo * in2.hi
3290 __ imull(eax, in1_lo);
3291 // in1.hi <- in1.hi * in2.lo
3292 __ imull(in1_hi, in2_lo);
3293 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3294 __ addl(in1_hi, eax);
3295 // move in1_lo to eax to prepare for double precision
3296 __ movl(eax, in1_lo);
3297 // edx:eax <- in1.lo * in2.lo
3298 __ mull(in2_lo);
3299 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3300 __ addl(in1_hi, edx);
3301 // in1.lo <- (in1.lo * in2.lo)[31:0];
3302 __ movl(in1_lo, eax);
3303 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003304
3305 break;
3306 }
3307
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003308 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003309 DCHECK(first.Equals(locations->Out()));
3310 if (second.IsFpuRegister()) {
3311 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3312 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3313 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003314 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003315 __ mulss(first.AsFpuRegister<XmmRegister>(),
3316 codegen_->LiteralFloatAddress(
3317 const_area->GetConstant()->AsFloatConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003318 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003319 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3320 } else {
3321 DCHECK(second.IsStackSlot());
3322 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3323 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003324 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003325 }
3326
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003327 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003328 DCHECK(first.Equals(locations->Out()));
3329 if (second.IsFpuRegister()) {
3330 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3331 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3332 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003333 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003334 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3335 codegen_->LiteralDoubleAddress(
3336 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003337 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003338 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3339 } else {
3340 DCHECK(second.IsDoubleStackSlot());
3341 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3342 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003343 break;
3344 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003345
3346 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003347 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003348 }
3349}
3350
Roland Levillain232ade02015-04-20 15:14:36 +01003351void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3352 uint32_t temp_offset,
3353 uint32_t stack_adjustment,
3354 bool is_fp,
3355 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003356 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003357 DCHECK(!is_wide);
3358 if (is_fp) {
3359 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3360 } else {
3361 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3362 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003363 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003364 DCHECK(is_wide);
3365 if (is_fp) {
3366 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3367 } else {
3368 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3369 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003370 } else {
3371 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003372 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003373 Location stack_temp = Location::StackSlot(temp_offset);
3374 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003375 if (is_fp) {
3376 __ flds(Address(ESP, temp_offset));
3377 } else {
3378 __ filds(Address(ESP, temp_offset));
3379 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003380 } else {
3381 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3382 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003383 if (is_fp) {
3384 __ fldl(Address(ESP, temp_offset));
3385 } else {
3386 __ fildl(Address(ESP, temp_offset));
3387 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003388 }
3389 }
3390}
3391
3392void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003393 DataType::Type type = rem->GetResultType();
3394 bool is_float = type == DataType::Type::kFloat32;
3395 size_t elem_size = DataType::Size(type);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003396 LocationSummary* locations = rem->GetLocations();
3397 Location first = locations->InAt(0);
3398 Location second = locations->InAt(1);
3399 Location out = locations->Out();
3400
3401 // Create stack space for 2 elements.
3402 // TODO: enhance register allocator to ask for stack temporaries.
3403 __ subl(ESP, Immediate(2 * elem_size));
3404
3405 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003406 const bool is_wide = !is_float;
3407 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
3408 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003409
3410 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003411 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003412 __ Bind(&retry);
3413 __ fprem();
3414
3415 // Move FP status to AX.
3416 __ fstsw();
3417
3418 // And see if the argument reduction is complete. This is signaled by the
3419 // C2 FPU flag bit set to 0.
3420 __ andl(EAX, Immediate(kC2ConditionMask));
3421 __ j(kNotEqual, &retry);
3422
3423 // We have settled on the final value. Retrieve it into an XMM register.
3424 // Store FP top of stack to real stack.
3425 if (is_float) {
3426 __ fsts(Address(ESP, 0));
3427 } else {
3428 __ fstl(Address(ESP, 0));
3429 }
3430
3431 // Pop the 2 items from the FP stack.
3432 __ fucompp();
3433
3434 // Load the value from the stack into an XMM register.
3435 DCHECK(out.IsFpuRegister()) << out;
3436 if (is_float) {
3437 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3438 } else {
3439 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3440 }
3441
3442 // And remove the temporary stack space we allocated.
3443 __ addl(ESP, Immediate(2 * elem_size));
3444}
3445
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003446
3447void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3448 DCHECK(instruction->IsDiv() || instruction->IsRem());
3449
3450 LocationSummary* locations = instruction->GetLocations();
3451 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003452 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003453
3454 Register out_register = locations->Out().AsRegister<Register>();
3455 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003456 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003457
3458 DCHECK(imm == 1 || imm == -1);
3459
3460 if (instruction->IsRem()) {
3461 __ xorl(out_register, out_register);
3462 } else {
3463 __ movl(out_register, input_register);
3464 if (imm == -1) {
3465 __ negl(out_register);
3466 }
3467 }
3468}
3469
3470
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003471void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003472 LocationSummary* locations = instruction->GetLocations();
3473
3474 Register out_register = locations->Out().AsRegister<Register>();
3475 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003476 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003477 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3478 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003479
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003480 Register num = locations->GetTemp(0).AsRegister<Register>();
3481
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003482 __ leal(num, Address(input_register, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003483 __ testl(input_register, input_register);
3484 __ cmovl(kGreaterEqual, num, input_register);
3485 int shift = CTZ(imm);
3486 __ sarl(num, Immediate(shift));
3487
3488 if (imm < 0) {
3489 __ negl(num);
3490 }
3491
3492 __ movl(out_register, num);
3493}
3494
3495void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3496 DCHECK(instruction->IsDiv() || instruction->IsRem());
3497
3498 LocationSummary* locations = instruction->GetLocations();
3499 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3500
3501 Register eax = locations->InAt(0).AsRegister<Register>();
3502 Register out = locations->Out().AsRegister<Register>();
3503 Register num;
3504 Register edx;
3505
3506 if (instruction->IsDiv()) {
3507 edx = locations->GetTemp(0).AsRegister<Register>();
3508 num = locations->GetTemp(1).AsRegister<Register>();
3509 } else {
3510 edx = locations->Out().AsRegister<Register>();
3511 num = locations->GetTemp(0).AsRegister<Register>();
3512 }
3513
3514 DCHECK_EQ(EAX, eax);
3515 DCHECK_EQ(EDX, edx);
3516 if (instruction->IsDiv()) {
3517 DCHECK_EQ(EAX, out);
3518 } else {
3519 DCHECK_EQ(EDX, out);
3520 }
3521
3522 int64_t magic;
3523 int shift;
3524 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3525
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003526 // Save the numerator.
3527 __ movl(num, eax);
3528
3529 // EAX = magic
3530 __ movl(eax, Immediate(magic));
3531
3532 // EDX:EAX = magic * numerator
3533 __ imull(num);
3534
3535 if (imm > 0 && magic < 0) {
3536 // EDX += num
3537 __ addl(edx, num);
3538 } else if (imm < 0 && magic > 0) {
3539 __ subl(edx, num);
3540 }
3541
3542 // Shift if needed.
3543 if (shift != 0) {
3544 __ sarl(edx, Immediate(shift));
3545 }
3546
3547 // EDX += 1 if EDX < 0
3548 __ movl(eax, edx);
3549 __ shrl(edx, Immediate(31));
3550 __ addl(edx, eax);
3551
3552 if (instruction->IsRem()) {
3553 __ movl(eax, num);
3554 __ imull(edx, Immediate(imm));
3555 __ subl(eax, edx);
3556 __ movl(edx, eax);
3557 } else {
3558 __ movl(eax, edx);
3559 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003560}
3561
Calin Juravlebacfec32014-11-14 15:54:36 +00003562void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3563 DCHECK(instruction->IsDiv() || instruction->IsRem());
3564
3565 LocationSummary* locations = instruction->GetLocations();
3566 Location out = locations->Out();
3567 Location first = locations->InAt(0);
3568 Location second = locations->InAt(1);
3569 bool is_div = instruction->IsDiv();
3570
3571 switch (instruction->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003572 case DataType::Type::kInt32: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003573 DCHECK_EQ(EAX, first.AsRegister<Register>());
3574 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003575
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003576 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003577 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003578
3579 if (imm == 0) {
3580 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3581 } else if (imm == 1 || imm == -1) {
3582 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003583 } else if (is_div && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003584 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003585 } else {
3586 DCHECK(imm <= -2 || imm >= 2);
3587 GenerateDivRemWithAnyConstant(instruction);
3588 }
3589 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01003590 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) DivRemMinusOneSlowPathX86(
David Srbecky9cd6d372016-02-09 15:24:47 +00003591 instruction, out.AsRegister<Register>(), is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003592 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003593
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003594 Register second_reg = second.AsRegister<Register>();
3595 // 0x80000000/-1 triggers an arithmetic exception!
3596 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3597 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003598
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003599 __ cmpl(second_reg, Immediate(-1));
3600 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003601
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003602 // edx:eax <- sign-extended of eax
3603 __ cdq();
3604 // eax = quotient, edx = remainder
3605 __ idivl(second_reg);
3606 __ Bind(slow_path->GetExitLabel());
3607 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003608 break;
3609 }
3610
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003611 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003612 InvokeRuntimeCallingConvention calling_convention;
3613 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3614 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3615 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3616 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3617 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3618 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3619
3620 if (is_div) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003621 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003622 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003623 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003624 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003625 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003626 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003627 break;
3628 }
3629
3630 default:
3631 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3632 }
3633}
3634
Calin Juravle7c4954d2014-10-28 16:57:40 +00003635void LocationsBuilderX86::VisitDiv(HDiv* div) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003636 LocationSummary::CallKind call_kind = (div->GetResultType() == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003637 ? LocationSummary::kCallOnMainOnly
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003638 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01003639 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(div, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003640
Calin Juravle7c4954d2014-10-28 16:57:40 +00003641 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003642 case DataType::Type::kInt32: {
Calin Juravled0d48522014-11-04 16:40:20 +00003643 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003644 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003645 locations->SetOut(Location::SameAsFirstInput());
3646 // Intel uses edx:eax as the dividend.
3647 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003648 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3649 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3650 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003651 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003652 locations->AddTemp(Location::RequiresRegister());
3653 }
Calin Juravled0d48522014-11-04 16:40:20 +00003654 break;
3655 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003656 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003657 InvokeRuntimeCallingConvention calling_convention;
3658 locations->SetInAt(0, Location::RegisterPairLocation(
3659 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3660 locations->SetInAt(1, Location::RegisterPairLocation(
3661 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3662 // Runtime helper puts the result in EAX, EDX.
3663 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003664 break;
3665 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003666 case DataType::Type::kFloat32:
3667 case DataType::Type::kFloat64: {
Calin Juravle7c4954d2014-10-28 16:57:40 +00003668 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003669 if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3670 DCHECK(div->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003671 } else if (div->InputAt(1)->IsConstant()) {
3672 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003673 } else {
3674 locations->SetInAt(1, Location::Any());
3675 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003676 locations->SetOut(Location::SameAsFirstInput());
3677 break;
3678 }
3679
3680 default:
3681 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3682 }
3683}
3684
3685void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3686 LocationSummary* locations = div->GetLocations();
3687 Location first = locations->InAt(0);
3688 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003689
3690 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003691 case DataType::Type::kInt32:
3692 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003693 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003694 break;
3695 }
3696
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003697 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003698 if (second.IsFpuRegister()) {
3699 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3700 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3701 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003702 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003703 __ divss(first.AsFpuRegister<XmmRegister>(),
3704 codegen_->LiteralFloatAddress(
3705 const_area->GetConstant()->AsFloatConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003706 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003707 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3708 } else {
3709 DCHECK(second.IsStackSlot());
3710 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3711 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003712 break;
3713 }
3714
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003715 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003716 if (second.IsFpuRegister()) {
3717 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3718 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3719 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003720 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003721 __ divsd(first.AsFpuRegister<XmmRegister>(),
3722 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003723 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3724 const_area->GetBaseMethodAddress(),
3725 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003726 } else {
3727 DCHECK(second.IsDoubleStackSlot());
3728 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3729 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003730 break;
3731 }
3732
3733 default:
3734 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3735 }
3736}
3737
Calin Juravlebacfec32014-11-14 15:54:36 +00003738void LocationsBuilderX86::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003739 DataType::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003740
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003741 LocationSummary::CallKind call_kind = (rem->GetResultType() == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003742 ? LocationSummary::kCallOnMainOnly
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003743 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01003744 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003745
Calin Juravled2ec87d2014-12-08 14:24:46 +00003746 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003747 case DataType::Type::kInt32: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003748 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003749 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003750 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003751 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3752 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3753 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003754 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003755 locations->AddTemp(Location::RequiresRegister());
3756 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003757 break;
3758 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003759 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003760 InvokeRuntimeCallingConvention calling_convention;
3761 locations->SetInAt(0, Location::RegisterPairLocation(
3762 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3763 locations->SetInAt(1, Location::RegisterPairLocation(
3764 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3765 // Runtime helper puts the result in EAX, EDX.
3766 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3767 break;
3768 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003769 case DataType::Type::kFloat64:
3770 case DataType::Type::kFloat32: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003771 locations->SetInAt(0, Location::Any());
3772 locations->SetInAt(1, Location::Any());
3773 locations->SetOut(Location::RequiresFpuRegister());
3774 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003775 break;
3776 }
3777
3778 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003779 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003780 }
3781}
3782
3783void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003784 DataType::Type type = rem->GetResultType();
Calin Juravlebacfec32014-11-14 15:54:36 +00003785 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003786 case DataType::Type::kInt32:
3787 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003788 GenerateDivRemIntegral(rem);
3789 break;
3790 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003791 case DataType::Type::kFloat32:
3792 case DataType::Type::kFloat64: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003793 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003794 break;
3795 }
3796 default:
3797 LOG(FATAL) << "Unexpected rem type " << type;
3798 }
3799}
3800
Calin Juravled0d48522014-11-04 16:40:20 +00003801void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003802 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003803 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003804 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003805 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003806 case DataType::Type::kInt8:
3807 case DataType::Type::kUint16:
3808 case DataType::Type::kInt16:
3809 case DataType::Type::kInt32: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003810 locations->SetInAt(0, Location::Any());
3811 break;
3812 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003813 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003814 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
3815 if (!instruction->IsConstant()) {
3816 locations->AddTemp(Location::RequiresRegister());
3817 }
3818 break;
3819 }
3820 default:
3821 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3822 }
Calin Juravled0d48522014-11-04 16:40:20 +00003823}
3824
3825void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01003826 SlowPathCode* slow_path =
3827 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003828 codegen_->AddSlowPath(slow_path);
3829
3830 LocationSummary* locations = instruction->GetLocations();
3831 Location value = locations->InAt(0);
3832
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003833 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003834 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003835 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003836 case DataType::Type::kInt8:
3837 case DataType::Type::kUint16:
3838 case DataType::Type::kInt16:
3839 case DataType::Type::kInt32: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003840 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003841 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003842 __ j(kEqual, slow_path->GetEntryLabel());
3843 } else if (value.IsStackSlot()) {
3844 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
3845 __ j(kEqual, slow_path->GetEntryLabel());
3846 } else {
3847 DCHECK(value.IsConstant()) << value;
3848 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01003849 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003850 }
3851 }
3852 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003853 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003854 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003855 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003856 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003857 __ movl(temp, value.AsRegisterPairLow<Register>());
3858 __ orl(temp, value.AsRegisterPairHigh<Register>());
3859 __ j(kEqual, slow_path->GetEntryLabel());
3860 } else {
3861 DCHECK(value.IsConstant()) << value;
3862 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3863 __ jmp(slow_path->GetEntryLabel());
3864 }
3865 }
3866 break;
3867 }
3868 default:
3869 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003870 }
Calin Juravled0d48522014-11-04 16:40:20 +00003871}
3872
Calin Juravle9aec02f2014-11-18 23:06:35 +00003873void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3874 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3875
3876 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003877 new (GetGraph()->GetAllocator()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003878
3879 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003880 case DataType::Type::kInt32:
3881 case DataType::Type::kInt64: {
Mark P Mendell73945692015-04-29 14:56:17 +00003882 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003883 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003884 // The shift count needs to be in CL or a constant.
3885 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003886 locations->SetOut(Location::SameAsFirstInput());
3887 break;
3888 }
3889 default:
3890 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3891 }
3892}
3893
3894void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3895 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3896
3897 LocationSummary* locations = op->GetLocations();
3898 Location first = locations->InAt(0);
3899 Location second = locations->InAt(1);
3900 DCHECK(first.Equals(locations->Out()));
3901
3902 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003903 case DataType::Type::kInt32: {
Mark P Mendell73945692015-04-29 14:56:17 +00003904 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003905 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003906 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003907 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003908 DCHECK_EQ(ECX, second_reg);
3909 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003910 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003911 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003912 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003913 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003914 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003915 }
3916 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003917 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003918 if (shift == 0) {
3919 return;
3920 }
3921 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003922 if (op->IsShl()) {
3923 __ shll(first_reg, imm);
3924 } else if (op->IsShr()) {
3925 __ sarl(first_reg, imm);
3926 } else {
3927 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003928 }
3929 }
3930 break;
3931 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003932 case DataType::Type::kInt64: {
Mark P Mendell73945692015-04-29 14:56:17 +00003933 if (second.IsRegister()) {
3934 Register second_reg = second.AsRegister<Register>();
3935 DCHECK_EQ(ECX, second_reg);
3936 if (op->IsShl()) {
3937 GenerateShlLong(first, second_reg);
3938 } else if (op->IsShr()) {
3939 GenerateShrLong(first, second_reg);
3940 } else {
3941 GenerateUShrLong(first, second_reg);
3942 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003943 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003944 // Shift by a constant.
Roland Levillain5b5b9312016-03-22 14:57:31 +00003945 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003946 // Nothing to do if the shift is 0, as the input is already the output.
3947 if (shift != 0) {
3948 if (op->IsShl()) {
3949 GenerateShlLong(first, shift);
3950 } else if (op->IsShr()) {
3951 GenerateShrLong(first, shift);
3952 } else {
3953 GenerateUShrLong(first, shift);
3954 }
3955 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003956 }
3957 break;
3958 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003959 default:
3960 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3961 }
3962}
3963
Mark P Mendell73945692015-04-29 14:56:17 +00003964void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3965 Register low = loc.AsRegisterPairLow<Register>();
3966 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003967 if (shift == 1) {
3968 // This is just an addition.
3969 __ addl(low, low);
3970 __ adcl(high, high);
3971 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003972 // Shift by 32 is easy. High gets low, and low gets 0.
3973 codegen_->EmitParallelMoves(
3974 loc.ToLow(),
3975 loc.ToHigh(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003976 DataType::Type::kInt32,
Mark P Mendell73945692015-04-29 14:56:17 +00003977 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3978 loc.ToLow(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003979 DataType::Type::kInt32);
Mark P Mendell73945692015-04-29 14:56:17 +00003980 } else if (shift > 32) {
3981 // Low part becomes 0. High part is low part << (shift-32).
3982 __ movl(high, low);
3983 __ shll(high, Immediate(shift - 32));
3984 __ xorl(low, low);
3985 } else {
3986 // Between 1 and 31.
3987 __ shld(high, low, Immediate(shift));
3988 __ shll(low, Immediate(shift));
3989 }
3990}
3991
Calin Juravle9aec02f2014-11-18 23:06:35 +00003992void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003993 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003994 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
3995 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
3996 __ testl(shifter, Immediate(32));
3997 __ j(kEqual, &done);
3998 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
3999 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
4000 __ Bind(&done);
4001}
4002
Mark P Mendell73945692015-04-29 14:56:17 +00004003void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
4004 Register low = loc.AsRegisterPairLow<Register>();
4005 Register high = loc.AsRegisterPairHigh<Register>();
4006 if (shift == 32) {
4007 // Need to copy the sign.
4008 DCHECK_NE(low, high);
4009 __ movl(low, high);
4010 __ sarl(high, Immediate(31));
4011 } else if (shift > 32) {
4012 DCHECK_NE(low, high);
4013 // High part becomes sign. Low part is shifted by shift - 32.
4014 __ movl(low, high);
4015 __ sarl(high, Immediate(31));
4016 __ sarl(low, Immediate(shift - 32));
4017 } else {
4018 // Between 1 and 31.
4019 __ shrd(low, high, Immediate(shift));
4020 __ sarl(high, Immediate(shift));
4021 }
4022}
4023
Calin Juravle9aec02f2014-11-18 23:06:35 +00004024void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004025 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004026 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4027 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
4028 __ testl(shifter, Immediate(32));
4029 __ j(kEqual, &done);
4030 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4031 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
4032 __ Bind(&done);
4033}
4034
Mark P Mendell73945692015-04-29 14:56:17 +00004035void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
4036 Register low = loc.AsRegisterPairLow<Register>();
4037 Register high = loc.AsRegisterPairHigh<Register>();
4038 if (shift == 32) {
4039 // Shift by 32 is easy. Low gets high, and high gets 0.
4040 codegen_->EmitParallelMoves(
4041 loc.ToHigh(),
4042 loc.ToLow(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004043 DataType::Type::kInt32,
Mark P Mendell73945692015-04-29 14:56:17 +00004044 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
4045 loc.ToHigh(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004046 DataType::Type::kInt32);
Mark P Mendell73945692015-04-29 14:56:17 +00004047 } else if (shift > 32) {
4048 // Low part is high >> (shift - 32). High part becomes 0.
4049 __ movl(low, high);
4050 __ shrl(low, Immediate(shift - 32));
4051 __ xorl(high, high);
4052 } else {
4053 // Between 1 and 31.
4054 __ shrd(low, high, Immediate(shift));
4055 __ shrl(high, Immediate(shift));
4056 }
4057}
4058
Calin Juravle9aec02f2014-11-18 23:06:35 +00004059void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004060 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004061 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4062 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
4063 __ testl(shifter, Immediate(32));
4064 __ j(kEqual, &done);
4065 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4066 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
4067 __ Bind(&done);
4068}
4069
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004070void LocationsBuilderX86::VisitRor(HRor* ror) {
4071 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004072 new (GetGraph()->GetAllocator()) LocationSummary(ror, LocationSummary::kNoCall);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004073
4074 switch (ror->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004075 case DataType::Type::kInt64:
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004076 // Add the temporary needed.
4077 locations->AddTemp(Location::RequiresRegister());
4078 FALLTHROUGH_INTENDED;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004079 case DataType::Type::kInt32:
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004080 locations->SetInAt(0, Location::RequiresRegister());
4081 // The shift count needs to be in CL (unless it is a constant).
4082 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
4083 locations->SetOut(Location::SameAsFirstInput());
4084 break;
4085 default:
4086 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4087 UNREACHABLE();
4088 }
4089}
4090
4091void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
4092 LocationSummary* locations = ror->GetLocations();
4093 Location first = locations->InAt(0);
4094 Location second = locations->InAt(1);
4095
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004096 if (ror->GetResultType() == DataType::Type::kInt32) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004097 Register first_reg = first.AsRegister<Register>();
4098 if (second.IsRegister()) {
4099 Register second_reg = second.AsRegister<Register>();
4100 __ rorl(first_reg, second_reg);
4101 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004102 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004103 __ rorl(first_reg, imm);
4104 }
4105 return;
4106 }
4107
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004108 DCHECK_EQ(ror->GetResultType(), DataType::Type::kInt64);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004109 Register first_reg_lo = first.AsRegisterPairLow<Register>();
4110 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
4111 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
4112 if (second.IsRegister()) {
4113 Register second_reg = second.AsRegister<Register>();
4114 DCHECK_EQ(second_reg, ECX);
4115 __ movl(temp_reg, first_reg_hi);
4116 __ shrd(first_reg_hi, first_reg_lo, second_reg);
4117 __ shrd(first_reg_lo, temp_reg, second_reg);
4118 __ movl(temp_reg, first_reg_hi);
4119 __ testl(second_reg, Immediate(32));
4120 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
4121 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
4122 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004123 int32_t shift_amt = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004124 if (shift_amt == 0) {
4125 // Already fine.
4126 return;
4127 }
4128 if (shift_amt == 32) {
4129 // Just swap.
4130 __ movl(temp_reg, first_reg_lo);
4131 __ movl(first_reg_lo, first_reg_hi);
4132 __ movl(first_reg_hi, temp_reg);
4133 return;
4134 }
4135
4136 Immediate imm(shift_amt);
4137 // Save the constents of the low value.
4138 __ movl(temp_reg, first_reg_lo);
4139
4140 // Shift right into low, feeding bits from high.
4141 __ shrd(first_reg_lo, first_reg_hi, imm);
4142
4143 // Shift right into high, feeding bits from the original low.
4144 __ shrd(first_reg_hi, temp_reg, imm);
4145
4146 // Swap if needed.
4147 if (shift_amt > 32) {
4148 __ movl(temp_reg, first_reg_lo);
4149 __ movl(first_reg_lo, first_reg_hi);
4150 __ movl(first_reg_hi, temp_reg);
4151 }
4152 }
4153}
4154
Calin Juravle9aec02f2014-11-18 23:06:35 +00004155void LocationsBuilderX86::VisitShl(HShl* shl) {
4156 HandleShift(shl);
4157}
4158
4159void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
4160 HandleShift(shl);
4161}
4162
4163void LocationsBuilderX86::VisitShr(HShr* shr) {
4164 HandleShift(shr);
4165}
4166
4167void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
4168 HandleShift(shr);
4169}
4170
4171void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
4172 HandleShift(ushr);
4173}
4174
4175void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
4176 HandleShift(ushr);
4177}
4178
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004179void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004180 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
4181 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004182 locations->SetOut(Location::RegisterLocation(EAX));
David Brazdil6de19382016-01-08 17:37:10 +00004183 if (instruction->IsStringAlloc()) {
4184 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4185 } else {
4186 InvokeRuntimeCallingConvention calling_convention;
4187 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00004188 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004189}
4190
4191void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004192 // Note: if heap poisoning is enabled, the entry point takes cares
4193 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004194 if (instruction->IsStringAlloc()) {
4195 // String is allocated through StringFactory. Call NewEmptyString entry point.
4196 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07004197 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004198 __ fs()->movl(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString)));
4199 __ call(Address(temp, code_offset.Int32Value()));
4200 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4201 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01004202 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00004203 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00004204 DCHECK(!codegen_->IsLeafMethod());
4205 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004206}
4207
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004208void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004209 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
4210 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004211 locations->SetOut(Location::RegisterLocation(EAX));
4212 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004213 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4214 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004215}
4216
4217void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004218 // Note: if heap poisoning is enabled, the entry point takes cares
4219 // of poisoning the reference.
Nicolas Geoffrayd0958442017-01-30 14:57:16 +00004220 QuickEntrypointEnum entrypoint =
4221 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
4222 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004223 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004224 DCHECK(!codegen_->IsLeafMethod());
4225}
4226
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004227void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004228 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004229 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004230 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4231 if (location.IsStackSlot()) {
4232 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4233 } else if (location.IsDoubleStackSlot()) {
4234 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004235 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004236 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004237}
4238
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004239void InstructionCodeGeneratorX86::VisitParameterValue(
4240 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4241}
4242
4243void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
4244 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004245 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004246 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4247}
4248
4249void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004250}
4251
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004252void LocationsBuilderX86::VisitClassTableGet(HClassTableGet* instruction) {
4253 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004254 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004255 locations->SetInAt(0, Location::RequiresRegister());
4256 locations->SetOut(Location::RequiresRegister());
4257}
4258
4259void InstructionCodeGeneratorX86::VisitClassTableGet(HClassTableGet* instruction) {
4260 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004261 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004262 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004263 instruction->GetIndex(), kX86PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004264 __ movl(locations->Out().AsRegister<Register>(),
4265 Address(locations->InAt(0).AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004266 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004267 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004268 instruction->GetIndex(), kX86PointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004269 __ movl(locations->Out().AsRegister<Register>(),
4270 Address(locations->InAt(0).AsRegister<Register>(),
4271 mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
4272 // temp = temp->GetImtEntryAt(method_offset);
4273 __ movl(locations->Out().AsRegister<Register>(),
4274 Address(locations->Out().AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004275 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004276}
4277
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004278void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004279 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004280 new (GetGraph()->GetAllocator()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004281 locations->SetInAt(0, Location::RequiresRegister());
4282 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004283}
4284
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004285void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
4286 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01004287 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004288 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01004289 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004290 switch (not_->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004291 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004292 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004293 break;
4294
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004295 case DataType::Type::kInt64:
Roland Levillain70566432014-10-24 16:20:17 +01004296 __ notl(out.AsRegisterPairLow<Register>());
4297 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004298 break;
4299
4300 default:
4301 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4302 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004303}
4304
David Brazdil66d126e2015-04-03 16:02:44 +01004305void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
4306 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004307 new (GetGraph()->GetAllocator()) LocationSummary(bool_not, LocationSummary::kNoCall);
David Brazdil66d126e2015-04-03 16:02:44 +01004308 locations->SetInAt(0, Location::RequiresRegister());
4309 locations->SetOut(Location::SameAsFirstInput());
4310}
4311
4312void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004313 LocationSummary* locations = bool_not->GetLocations();
4314 Location in = locations->InAt(0);
4315 Location out = locations->Out();
4316 DCHECK(in.Equals(out));
4317 __ xorl(out.AsRegister<Register>(), Immediate(1));
4318}
4319
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004320void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004321 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004322 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004323 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004324 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004325 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004326 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004327 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004328 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004329 case DataType::Type::kInt32:
4330 case DataType::Type::kInt64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00004331 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00004332 locations->SetInAt(1, Location::Any());
4333 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4334 break;
4335 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004336 case DataType::Type::kFloat32:
4337 case DataType::Type::kFloat64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00004338 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004339 if (compare->InputAt(1)->IsX86LoadFromConstantTable()) {
4340 DCHECK(compare->InputAt(1)->IsEmittedAtUseSite());
4341 } else if (compare->InputAt(1)->IsConstant()) {
4342 locations->SetInAt(1, Location::RequiresFpuRegister());
4343 } else {
4344 locations->SetInAt(1, Location::Any());
4345 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004346 locations->SetOut(Location::RequiresRegister());
4347 break;
4348 }
4349 default:
4350 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4351 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004352}
4353
4354void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004355 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004356 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004357 Location left = locations->InAt(0);
4358 Location right = locations->InAt(1);
4359
Mark Mendell0c9497d2015-08-21 09:30:05 -04004360 NearLabel less, greater, done;
Aart Bika19616e2016-02-01 18:57:58 -08004361 Condition less_cond = kLess;
4362
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004363 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004364 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004365 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004366 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004367 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004368 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004369 case DataType::Type::kInt32: {
Roland Levillain0b671c02016-08-19 12:02:34 +01004370 codegen_->GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08004371 break;
4372 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004373 case DataType::Type::kInt64: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004374 Register left_low = left.AsRegisterPairLow<Register>();
4375 Register left_high = left.AsRegisterPairHigh<Register>();
4376 int32_t val_low = 0;
4377 int32_t val_high = 0;
4378 bool right_is_const = false;
4379
4380 if (right.IsConstant()) {
4381 DCHECK(right.GetConstant()->IsLongConstant());
4382 right_is_const = true;
4383 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
4384 val_low = Low32Bits(val);
4385 val_high = High32Bits(val);
4386 }
4387
Calin Juravleddb7df22014-11-25 20:56:51 +00004388 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004389 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004390 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004391 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004392 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004393 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004394 codegen_->Compare32BitValue(left_high, val_high);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004395 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004396 __ j(kLess, &less); // Signed compare.
4397 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004398 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004399 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004400 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004401 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004402 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004403 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004404 codegen_->Compare32BitValue(left_low, val_low);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004405 }
Aart Bika19616e2016-02-01 18:57:58 -08004406 less_cond = kBelow; // for CF (unsigned).
Calin Juravleddb7df22014-11-25 20:56:51 +00004407 break;
4408 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004409 case DataType::Type::kFloat32: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004410 GenerateFPCompare(left, right, compare, false);
Calin Juravleddb7df22014-11-25 20:56:51 +00004411 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004412 less_cond = kBelow; // for CF (floats).
Calin Juravleddb7df22014-11-25 20:56:51 +00004413 break;
4414 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004415 case DataType::Type::kFloat64: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004416 GenerateFPCompare(left, right, compare, true);
Calin Juravleddb7df22014-11-25 20:56:51 +00004417 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004418 less_cond = kBelow; // for CF (floats).
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004419 break;
4420 }
4421 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004422 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004423 }
Aart Bika19616e2016-02-01 18:57:58 -08004424
Calin Juravleddb7df22014-11-25 20:56:51 +00004425 __ movl(out, Immediate(0));
4426 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08004427 __ j(less_cond, &less);
Calin Juravleddb7df22014-11-25 20:56:51 +00004428
4429 __ Bind(&greater);
4430 __ movl(out, Immediate(1));
4431 __ jmp(&done);
4432
4433 __ Bind(&less);
4434 __ movl(out, Immediate(-1));
4435
4436 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004437}
4438
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004439void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004440 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004441 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004442 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004443 locations->SetInAt(i, Location::Any());
4444 }
4445 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004446}
4447
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004448void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004449 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004450}
4451
Roland Levillain7c1559a2015-12-15 10:55:36 +00004452void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004453 /*
4454 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
4455 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
4456 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4457 */
4458 switch (kind) {
4459 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004460 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004461 break;
4462 }
4463 case MemBarrierKind::kAnyStore:
4464 case MemBarrierKind::kLoadAny:
4465 case MemBarrierKind::kStoreStore: {
4466 // nop
4467 break;
4468 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004469 case MemBarrierKind::kNTStoreStore:
4470 // Non-Temporal Store/Store needs an explicit fence.
4471 MemoryFence(/* non-temporal */ true);
4472 break;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004473 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004474}
4475
Vladimir Markodc151b22015-10-15 18:02:30 +01004476HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
4477 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004478 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004479 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004480}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004481
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004482Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4483 Register temp) {
4484 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Markoc53c0792015-11-19 15:48:33 +00004485 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004486 if (!invoke->GetLocations()->Intrinsified()) {
4487 return location.AsRegister<Register>();
4488 }
4489 // For intrinsics we allow any location, so it may be on the stack.
4490 if (!location.IsRegister()) {
4491 __ movl(temp, Address(ESP, location.GetStackIndex()));
4492 return temp;
4493 }
4494 // For register locations, check if the register was saved. If so, get it from the stack.
4495 // Note: There is a chance that the register was saved but not overwritten, so we could
4496 // save one load. However, since this is just an intrinsic slow path we prefer this
4497 // simple and more robust approach rather that trying to determine if that's the case.
4498 SlowPathCode* slow_path = GetCurrentSlowPath();
Vladimir Marko4ee8e292017-06-02 15:39:30 +00004499 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
4500 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4501 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4502 __ movl(temp, Address(ESP, stack_offset));
4503 return temp;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004504 }
4505 return location.AsRegister<Register>();
4506}
4507
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004508void CodeGeneratorX86::GenerateStaticOrDirectCall(
4509 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Vladimir Marko58155012015-08-19 12:49:41 +00004510 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4511 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004512 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00004513 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004514 uint32_t offset =
4515 GetThreadOffset<kX86PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
4516 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004517 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004518 }
Vladimir Marko58155012015-08-19 12:49:41 +00004519 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004520 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004521 break;
Vladimir Marko65979462017-05-19 17:25:12 +01004522 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
4523 DCHECK(GetCompilerOptions().IsBootImage());
4524 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4525 temp.AsRegister<Register>());
4526 __ leal(temp.AsRegister<Register>(), Address(base_reg, CodeGeneratorX86::kDummy32BitOffset));
4527 RecordBootMethodPatch(invoke);
4528 break;
4529 }
Vladimir Marko58155012015-08-19 12:49:41 +00004530 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4531 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
4532 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004533 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004534 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4535 temp.AsRegister<Register>());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004536 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004537 // Bind a new fixup label at the end of the "movl" insn.
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004538 __ Bind(NewMethodBssEntryPatch(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004539 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress(),
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004540 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex())));
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004541 break;
4542 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004543 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
4544 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
4545 return; // No code pointer retrieval; the runtime performs the call directly.
Vladimir Marko9b688a02015-05-06 14:12:42 +01004546 }
Vladimir Marko58155012015-08-19 12:49:41 +00004547 }
4548
4549 switch (invoke->GetCodePtrLocation()) {
4550 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4551 __ call(GetFrameEntryLabel());
4552 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004553 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4554 // (callee_method + offset_of_quick_compiled_code)()
4555 __ call(Address(callee_method.AsRegister<Register>(),
4556 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07004557 kX86PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004558 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04004559 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004560 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Mark Mendell09ed1a32015-03-25 08:30:06 -04004561
4562 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04004563}
4564
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004565void CodeGeneratorX86::GenerateVirtualCall(
4566 HInvokeVirtual* invoke, Location temp_in, SlowPathCode* slow_path) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004567 Register temp = temp_in.AsRegister<Register>();
4568 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4569 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004570
4571 // Use the calling convention instead of the location of the receiver, as
4572 // intrinsics may have put the receiver in a different register. In the intrinsics
4573 // slow path, the arguments have been moved to the right place, so here we are
4574 // guaranteed that the receiver is the first register of the calling convention.
4575 InvokeDexCallingConvention calling_convention;
4576 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004577 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004578 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004579 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004580 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004581 // Instead of simply (possibly) unpoisoning `temp` here, we should
4582 // emit a read barrier for the previous class reference load.
4583 // However this is not required in practice, as this is an
4584 // intermediate/temporary reference and because the current
4585 // concurrent copying collector keeps the from-space memory
4586 // intact/accessible until the end of the marking phase (the
4587 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004588 __ MaybeUnpoisonHeapReference(temp);
4589 // temp = temp->GetMethodAt(method_offset);
4590 __ movl(temp, Address(temp, method_offset));
4591 // call temp->GetEntryPoint();
4592 __ call(Address(
Andreas Gampe542451c2016-07-26 09:02:02 -07004593 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004594 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004595}
4596
Vladimir Marko65979462017-05-19 17:25:12 +01004597void CodeGeneratorX86::RecordBootMethodPatch(HInvokeStaticOrDirect* invoke) {
4598 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
4599 HX86ComputeBaseMethodAddress* address =
4600 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
4601 boot_image_method_patches_.emplace_back(address,
4602 *invoke->GetTargetMethod().dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07004603 invoke->GetTargetMethod().index);
Vladimir Marko65979462017-05-19 17:25:12 +01004604 __ Bind(&boot_image_method_patches_.back().label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004605}
4606
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004607Label* CodeGeneratorX86::NewMethodBssEntryPatch(
4608 HX86ComputeBaseMethodAddress* method_address,
4609 MethodReference target_method) {
4610 // Add the patch entry and bind its label at the end of the instruction.
4611 method_bss_entry_patches_.emplace_back(method_address,
4612 *target_method.dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07004613 target_method.index);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004614 return &method_bss_entry_patches_.back().label;
4615}
4616
Vladimir Marko1998cd02017-01-13 13:02:58 +00004617void CodeGeneratorX86::RecordBootTypePatch(HLoadClass* load_class) {
Vladimir Marko00916b92017-05-17 17:45:45 +01004618 HX86ComputeBaseMethodAddress* address = load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004619 boot_image_type_patches_.emplace_back(address,
4620 load_class->GetDexFile(),
Vladimir Marko1998cd02017-01-13 13:02:58 +00004621 load_class->GetTypeIndex().index_);
4622 __ Bind(&boot_image_type_patches_.back().label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004623}
4624
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004625Label* CodeGeneratorX86::NewTypeBssEntryPatch(HLoadClass* load_class) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004626 HX86ComputeBaseMethodAddress* address =
4627 load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
4628 type_bss_entry_patches_.emplace_back(
4629 address, load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00004630 return &type_bss_entry_patches_.back().label;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004631}
4632
Vladimir Marko65979462017-05-19 17:25:12 +01004633void CodeGeneratorX86::RecordBootStringPatch(HLoadString* load_string) {
Vladimir Marko65979462017-05-19 17:25:12 +01004634 HX86ComputeBaseMethodAddress* address = load_string->InputAt(0)->AsX86ComputeBaseMethodAddress();
4635 string_patches_.emplace_back(address,
4636 load_string->GetDexFile(),
4637 load_string->GetStringIndex().index_);
4638 __ Bind(&string_patches_.back().label);
4639}
4640
Vladimir Markoaad75c62016-10-03 08:46:48 +00004641Label* CodeGeneratorX86::NewStringBssEntryPatch(HLoadString* load_string) {
4642 DCHECK(!GetCompilerOptions().IsBootImage());
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004643 HX86ComputeBaseMethodAddress* address =
4644 load_string->InputAt(0)->AsX86ComputeBaseMethodAddress();
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004645 string_bss_entry_patches_.emplace_back(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004646 address, load_string->GetDexFile(), load_string->GetStringIndex().index_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004647 return &string_bss_entry_patches_.back().label;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004648}
4649
Vladimir Markoaad75c62016-10-03 08:46:48 +00004650// The label points to the end of the "movl" or another instruction but the literal offset
4651// for method patch needs to point to the embedded constant which occupies the last 4 bytes.
4652constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
4653
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004654template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +00004655inline void CodeGeneratorX86::EmitPcRelativeLinkerPatches(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004656 const ArenaDeque<X86PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004657 ArenaVector<linker::LinkerPatch>* linker_patches) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004658 for (const X86PcRelativePatchInfo& info : infos) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00004659 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004660 linker_patches->push_back(Factory(
4661 literal_offset, &info.dex_file, GetMethodAddressOffset(info.method_address), info.index));
Vladimir Markoaad75c62016-10-03 08:46:48 +00004662 }
4663}
4664
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004665void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Marko58155012015-08-19 12:49:41 +00004666 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004667 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01004668 boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004669 method_bss_entry_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +00004670 boot_image_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01004671 type_bss_entry_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004672 string_patches_.size() +
4673 string_bss_entry_patches_.size();
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004674 linker_patches->reserve(size);
Vladimir Marko764d4542017-05-16 10:31:41 +01004675 if (GetCompilerOptions().IsBootImage()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004676 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
4677 boot_image_method_patches_, linker_patches);
4678 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
4679 boot_image_type_patches_, linker_patches);
4680 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
4681 string_patches_, linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004682 } else {
Vladimir Marko65979462017-05-19 17:25:12 +01004683 DCHECK(boot_image_method_patches_.empty());
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004684 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeClassTablePatch>(
4685 boot_image_type_patches_, linker_patches);
4686 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringInternTablePatch>(
4687 string_patches_, linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004688 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004689 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
4690 method_bss_entry_patches_, linker_patches);
4691 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
4692 type_bss_entry_patches_, linker_patches);
4693 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
4694 string_bss_entry_patches_, linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00004695 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00004696}
4697
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004698void CodeGeneratorX86::MarkGCCard(Register temp,
4699 Register card,
4700 Register object,
4701 Register value,
4702 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004703 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004704 if (value_can_be_null) {
4705 __ testl(value, value);
4706 __ j(kEqual, &is_null);
4707 }
Andreas Gampe542451c2016-07-26 09:02:02 -07004708 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86PointerSize>().Int32Value()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004709 __ movl(temp, object);
4710 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004711 __ movb(Address(temp, card, TIMES_1, 0),
4712 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004713 if (value_can_be_null) {
4714 __ Bind(&is_null);
4715 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004716}
4717
Calin Juravle52c48962014-12-16 17:02:57 +00004718void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4719 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00004720
4721 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004722 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004723 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004724 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
4725 kEmitCompilerReadBarrier
4726 ? LocationSummary::kCallOnSlowPath
4727 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004728 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004729 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004730 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004731 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004732
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004733 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004734 locations->SetOut(Location::RequiresFpuRegister());
4735 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004736 // The output overlaps in case of long: we don't want the low move
4737 // to overwrite the object's location. Likewise, in the case of
4738 // an object field get with read barriers enabled, we do not want
4739 // the move to overwrite the object's location, as we need it to emit
4740 // the read barrier.
4741 locations->SetOut(
4742 Location::RequiresRegister(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004743 (object_field_get_with_read_barrier || instruction->GetType() == DataType::Type::kInt64) ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00004744 Location::kOutputOverlap :
4745 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004746 }
Calin Juravle52c48962014-12-16 17:02:57 +00004747
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004748 if (field_info.IsVolatile() && (field_info.GetFieldType() == DataType::Type::kInt64)) {
Calin Juravle52c48962014-12-16 17:02:57 +00004749 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00004750 // So we use an XMM register as a temp to achieve atomicity (first
4751 // load the temp into the XMM and then copy the XMM into the
4752 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00004753 locations->AddTemp(Location::RequiresFpuRegister());
4754 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004755}
4756
Calin Juravle52c48962014-12-16 17:02:57 +00004757void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
4758 const FieldInfo& field_info) {
4759 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004760
Calin Juravle52c48962014-12-16 17:02:57 +00004761 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004762 Location base_loc = locations->InAt(0);
4763 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004764 Location out = locations->Out();
4765 bool is_volatile = field_info.IsVolatile();
Vladimir Marko61b92282017-10-11 13:23:17 +01004766 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
4767 DataType::Type load_type = instruction->GetType();
Calin Juravle52c48962014-12-16 17:02:57 +00004768 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4769
Vladimir Marko61b92282017-10-11 13:23:17 +01004770 switch (load_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004771 case DataType::Type::kBool:
4772 case DataType::Type::kUint8: {
Calin Juravle52c48962014-12-16 17:02:57 +00004773 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004774 break;
4775 }
4776
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004777 case DataType::Type::kInt8: {
Calin Juravle52c48962014-12-16 17:02:57 +00004778 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004779 break;
4780 }
4781
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004782 case DataType::Type::kUint16: {
4783 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004784 break;
4785 }
4786
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004787 case DataType::Type::kInt16: {
4788 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004789 break;
4790 }
4791
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004792 case DataType::Type::kInt32:
Calin Juravle52c48962014-12-16 17:02:57 +00004793 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004794 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00004795
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004796 case DataType::Type::kReference: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004797 // /* HeapReference<Object> */ out = *(base + offset)
4798 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004799 // Note that a potential implicit null check is handled in this
4800 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4801 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00004802 instruction, out, base, offset, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00004803 if (is_volatile) {
4804 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4805 }
4806 } else {
4807 __ movl(out.AsRegister<Register>(), Address(base, offset));
4808 codegen_->MaybeRecordImplicitNullCheck(instruction);
4809 if (is_volatile) {
4810 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4811 }
4812 // If read barriers are enabled, emit read barriers other than
4813 // Baker's using a slow path (and also unpoison the loaded
4814 // reference, if heap poisoning is enabled).
4815 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4816 }
4817 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004818 }
4819
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004820 case DataType::Type::kInt64: {
Calin Juravle52c48962014-12-16 17:02:57 +00004821 if (is_volatile) {
4822 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4823 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004824 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004825 __ movd(out.AsRegisterPairLow<Register>(), temp);
4826 __ psrlq(temp, Immediate(32));
4827 __ movd(out.AsRegisterPairHigh<Register>(), temp);
4828 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004829 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00004830 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004831 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004832 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
4833 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004834 break;
4835 }
4836
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004837 case DataType::Type::kFloat32: {
Calin Juravle52c48962014-12-16 17:02:57 +00004838 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004839 break;
4840 }
4841
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004842 case DataType::Type::kFloat64: {
Calin Juravle52c48962014-12-16 17:02:57 +00004843 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004844 break;
4845 }
4846
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004847 case DataType::Type::kVoid:
Vladimir Marko61b92282017-10-11 13:23:17 +01004848 LOG(FATAL) << "Unreachable type " << load_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004849 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004850 }
Calin Juravle52c48962014-12-16 17:02:57 +00004851
Vladimir Marko61b92282017-10-11 13:23:17 +01004852 if (load_type == DataType::Type::kReference || load_type == DataType::Type::kInt64) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004853 // Potential implicit null checks, in the case of reference or
4854 // long fields, are handled in the previous switch statement.
4855 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004856 codegen_->MaybeRecordImplicitNullCheck(instruction);
4857 }
4858
Calin Juravle52c48962014-12-16 17:02:57 +00004859 if (is_volatile) {
Vladimir Marko61b92282017-10-11 13:23:17 +01004860 if (load_type == DataType::Type::kReference) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004861 // Memory barriers, in the case of references, are also handled
4862 // in the previous switch statement.
4863 } else {
4864 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4865 }
Roland Levillain4d027112015-07-01 15:41:14 +01004866 }
Calin Juravle52c48962014-12-16 17:02:57 +00004867}
4868
4869void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4870 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4871
4872 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004873 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00004874 locations->SetInAt(0, Location::RequiresRegister());
4875 bool is_volatile = field_info.IsVolatile();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004876 DataType::Type field_type = field_info.GetFieldType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004877 bool is_byte_type = DataType::Size(field_type) == 1u;
Calin Juravle52c48962014-12-16 17:02:57 +00004878
4879 // The register allocator does not support multiple
4880 // inputs that die at entry with one in a specific register.
4881 if (is_byte_type) {
4882 // Ensure the value is in a byte register.
4883 locations->SetInAt(1, Location::RegisterLocation(EAX));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004884 } else if (DataType::IsFloatingPointType(field_type)) {
4885 if (is_volatile && field_type == DataType::Type::kFloat64) {
Mark Mendell81489372015-11-04 11:30:41 -05004886 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4887 locations->SetInAt(1, Location::RequiresFpuRegister());
4888 } else {
4889 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4890 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004891 } else if (is_volatile && field_type == DataType::Type::kInt64) {
Mark Mendell81489372015-11-04 11:30:41 -05004892 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00004893 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004894
Calin Juravle52c48962014-12-16 17:02:57 +00004895 // 64bits value can be atomically written to an address with movsd and an XMM register.
4896 // We need two XMM registers because there's no easier way to (bit) copy a register pair
4897 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
4898 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
4899 // isolated cases when we need this it isn't worth adding the extra complexity.
4900 locations->AddTemp(Location::RequiresFpuRegister());
4901 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004902 } else {
4903 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4904
4905 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4906 // Temporary registers for the write barrier.
4907 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
4908 // Ensure the card is in a byte register.
4909 locations->AddTemp(Location::RegisterLocation(ECX));
4910 }
Calin Juravle52c48962014-12-16 17:02:57 +00004911 }
4912}
4913
4914void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004915 const FieldInfo& field_info,
4916 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004917 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4918
4919 LocationSummary* locations = instruction->GetLocations();
4920 Register base = locations->InAt(0).AsRegister<Register>();
4921 Location value = locations->InAt(1);
4922 bool is_volatile = field_info.IsVolatile();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004923 DataType::Type field_type = field_info.GetFieldType();
Calin Juravle52c48962014-12-16 17:02:57 +00004924 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004925 bool needs_write_barrier =
4926 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004927
4928 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004929 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004930 }
4931
Mark Mendell81489372015-11-04 11:30:41 -05004932 bool maybe_record_implicit_null_check_done = false;
4933
Calin Juravle52c48962014-12-16 17:02:57 +00004934 switch (field_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004935 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004936 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004937 case DataType::Type::kInt8: {
Calin Juravle52c48962014-12-16 17:02:57 +00004938 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
4939 break;
4940 }
4941
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004942 case DataType::Type::kUint16:
4943 case DataType::Type::kInt16: {
Mark Mendell81489372015-11-04 11:30:41 -05004944 if (value.IsConstant()) {
Nicolas Geoffray78612082017-07-24 14:18:53 +01004945 __ movw(Address(base, offset),
4946 Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Mark Mendell81489372015-11-04 11:30:41 -05004947 } else {
4948 __ movw(Address(base, offset), value.AsRegister<Register>());
4949 }
Calin Juravle52c48962014-12-16 17:02:57 +00004950 break;
4951 }
4952
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004953 case DataType::Type::kInt32:
4954 case DataType::Type::kReference: {
Roland Levillain4d027112015-07-01 15:41:14 +01004955 if (kPoisonHeapReferences && needs_write_barrier) {
4956 // Note that in the case where `value` is a null reference,
4957 // we do not enter this block, as the reference does not
4958 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004959 DCHECK_EQ(field_type, DataType::Type::kReference);
Roland Levillain4d027112015-07-01 15:41:14 +01004960 Register temp = locations->GetTemp(0).AsRegister<Register>();
4961 __ movl(temp, value.AsRegister<Register>());
4962 __ PoisonHeapReference(temp);
4963 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05004964 } else if (value.IsConstant()) {
4965 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4966 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01004967 } else {
Nicolas Geoffray03971632016-03-17 10:44:24 +00004968 DCHECK(value.IsRegister()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01004969 __ movl(Address(base, offset), value.AsRegister<Register>());
4970 }
Calin Juravle52c48962014-12-16 17:02:57 +00004971 break;
4972 }
4973
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004974 case DataType::Type::kInt64: {
Calin Juravle52c48962014-12-16 17:02:57 +00004975 if (is_volatile) {
4976 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4977 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
4978 __ movd(temp1, value.AsRegisterPairLow<Register>());
4979 __ movd(temp2, value.AsRegisterPairHigh<Register>());
4980 __ punpckldq(temp1, temp2);
4981 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00004982 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05004983 } else if (value.IsConstant()) {
4984 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4985 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4986 codegen_->MaybeRecordImplicitNullCheck(instruction);
4987 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00004988 } else {
4989 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004990 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004991 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
4992 }
Mark Mendell81489372015-11-04 11:30:41 -05004993 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00004994 break;
4995 }
4996
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004997 case DataType::Type::kFloat32: {
Mark Mendell81489372015-11-04 11:30:41 -05004998 if (value.IsConstant()) {
4999 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5000 __ movl(Address(base, offset), Immediate(v));
5001 } else {
5002 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
5003 }
Calin Juravle52c48962014-12-16 17:02:57 +00005004 break;
5005 }
5006
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005007 case DataType::Type::kFloat64: {
Mark Mendell81489372015-11-04 11:30:41 -05005008 if (value.IsConstant()) {
5009 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
5010 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
5011 codegen_->MaybeRecordImplicitNullCheck(instruction);
5012 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
5013 maybe_record_implicit_null_check_done = true;
5014 } else {
5015 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
5016 }
Calin Juravle52c48962014-12-16 17:02:57 +00005017 break;
5018 }
5019
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005020 case DataType::Type::kVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00005021 LOG(FATAL) << "Unreachable type " << field_type;
5022 UNREACHABLE();
5023 }
5024
Mark Mendell81489372015-11-04 11:30:41 -05005025 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005026 codegen_->MaybeRecordImplicitNullCheck(instruction);
5027 }
5028
Roland Levillain4d027112015-07-01 15:41:14 +01005029 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005030 Register temp = locations->GetTemp(0).AsRegister<Register>();
5031 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005032 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00005033 }
5034
Calin Juravle52c48962014-12-16 17:02:57 +00005035 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005036 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00005037 }
5038}
5039
5040void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5041 HandleFieldGet(instruction, instruction->GetFieldInfo());
5042}
5043
5044void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5045 HandleFieldGet(instruction, instruction->GetFieldInfo());
5046}
5047
5048void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5049 HandleFieldSet(instruction, instruction->GetFieldInfo());
5050}
5051
5052void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005053 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005054}
5055
5056void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5057 HandleFieldSet(instruction, instruction->GetFieldInfo());
5058}
5059
5060void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005061 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005062}
5063
5064void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5065 HandleFieldGet(instruction, instruction->GetFieldInfo());
5066}
5067
5068void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5069 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005070}
5071
Calin Juravlee460d1d2015-09-29 04:52:17 +01005072void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
5073 HUnresolvedInstanceFieldGet* instruction) {
5074 FieldAccessCallingConventionX86 calling_convention;
5075 codegen_->CreateUnresolvedFieldLocationSummary(
5076 instruction, instruction->GetFieldType(), calling_convention);
5077}
5078
5079void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
5080 HUnresolvedInstanceFieldGet* instruction) {
5081 FieldAccessCallingConventionX86 calling_convention;
5082 codegen_->GenerateUnresolvedFieldAccess(instruction,
5083 instruction->GetFieldType(),
5084 instruction->GetFieldIndex(),
5085 instruction->GetDexPc(),
5086 calling_convention);
5087}
5088
5089void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
5090 HUnresolvedInstanceFieldSet* instruction) {
5091 FieldAccessCallingConventionX86 calling_convention;
5092 codegen_->CreateUnresolvedFieldLocationSummary(
5093 instruction, instruction->GetFieldType(), calling_convention);
5094}
5095
5096void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
5097 HUnresolvedInstanceFieldSet* instruction) {
5098 FieldAccessCallingConventionX86 calling_convention;
5099 codegen_->GenerateUnresolvedFieldAccess(instruction,
5100 instruction->GetFieldType(),
5101 instruction->GetFieldIndex(),
5102 instruction->GetDexPc(),
5103 calling_convention);
5104}
5105
5106void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
5107 HUnresolvedStaticFieldGet* instruction) {
5108 FieldAccessCallingConventionX86 calling_convention;
5109 codegen_->CreateUnresolvedFieldLocationSummary(
5110 instruction, instruction->GetFieldType(), calling_convention);
5111}
5112
5113void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
5114 HUnresolvedStaticFieldGet* instruction) {
5115 FieldAccessCallingConventionX86 calling_convention;
5116 codegen_->GenerateUnresolvedFieldAccess(instruction,
5117 instruction->GetFieldType(),
5118 instruction->GetFieldIndex(),
5119 instruction->GetDexPc(),
5120 calling_convention);
5121}
5122
5123void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
5124 HUnresolvedStaticFieldSet* instruction) {
5125 FieldAccessCallingConventionX86 calling_convention;
5126 codegen_->CreateUnresolvedFieldLocationSummary(
5127 instruction, instruction->GetFieldType(), calling_convention);
5128}
5129
5130void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
5131 HUnresolvedStaticFieldSet* instruction) {
5132 FieldAccessCallingConventionX86 calling_convention;
5133 codegen_->GenerateUnresolvedFieldAccess(instruction,
5134 instruction->GetFieldType(),
5135 instruction->GetFieldIndex(),
5136 instruction->GetDexPc(),
5137 calling_convention);
5138}
5139
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005140void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005141 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5142 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
5143 ? Location::RequiresRegister()
5144 : Location::Any();
5145 locations->SetInAt(0, loc);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005146}
5147
Calin Juravle2ae48182016-03-16 14:05:09 +00005148void CodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
5149 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005150 return;
5151 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005152 LocationSummary* locations = instruction->GetLocations();
5153 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005154
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005155 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00005156 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005157}
5158
Calin Juravle2ae48182016-03-16 14:05:09 +00005159void CodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005160 SlowPathCode* slow_path = new (GetScopedAllocator()) NullCheckSlowPathX86(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005161 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005162
5163 LocationSummary* locations = instruction->GetLocations();
5164 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005165
5166 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04005167 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005168 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005169 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005170 } else {
5171 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00005172 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005173 __ jmp(slow_path->GetEntryLabel());
5174 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005175 }
5176 __ j(kEqual, slow_path->GetEntryLabel());
5177}
5178
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005179void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005180 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005181}
5182
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005183void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005184 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005185 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005186 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005187 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
5188 object_array_get_with_read_barrier
5189 ? LocationSummary::kCallOnSlowPath
5190 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005191 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005192 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005193 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005194 locations->SetInAt(0, Location::RequiresRegister());
5195 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005196 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005197 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5198 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005199 // The output overlaps in case of long: we don't want the low move
5200 // to overwrite the array's location. Likewise, in the case of an
5201 // object array get with read barriers enabled, we do not want the
5202 // move to overwrite the array's location, as we need it to emit
5203 // the read barrier.
5204 locations->SetOut(
5205 Location::RequiresRegister(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005206 (instruction->GetType() == DataType::Type::kInt64 || object_array_get_with_read_barrier)
5207 ? Location::kOutputOverlap
5208 : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005209 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005210}
5211
5212void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
5213 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005214 Location obj_loc = locations->InAt(0);
5215 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005216 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005217 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005218 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005219
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005220 DataType::Type type = instruction->GetType();
Calin Juravle77520bc2015-01-12 18:45:46 +00005221 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005222 case DataType::Type::kBool:
5223 case DataType::Type::kUint8: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005224 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005225 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005226 break;
5227 }
5228
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005229 case DataType::Type::kInt8: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005230 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005231 __ movsxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005232 break;
5233 }
5234
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005235 case DataType::Type::kUint16: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005236 Register out = out_loc.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07005237 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5238 // Branch cases into compressed and uncompressed for each index's type.
5239 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
5240 NearLabel done, not_compressed;
Vladimir Marko3c89d422017-02-17 11:30:23 +00005241 __ testb(Address(obj, count_offset), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005242 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005243 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
5244 "Expecting 0=compressed, 1=uncompressed");
5245 __ j(kNotZero, &not_compressed);
jessicahandojo4877b792016-09-08 19:49:13 -07005246 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
5247 __ jmp(&done);
5248 __ Bind(&not_compressed);
5249 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5250 __ Bind(&done);
5251 } else {
5252 // Common case for charAt of array of char or when string compression's
5253 // feature is turned off.
5254 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5255 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005256 break;
5257 }
5258
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005259 case DataType::Type::kInt16: {
5260 Register out = out_loc.AsRegister<Register>();
5261 __ movsxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5262 break;
5263 }
5264
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005265 case DataType::Type::kInt32: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005266 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005267 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005268 break;
5269 }
5270
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005271 case DataType::Type::kReference: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005272 static_assert(
5273 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5274 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00005275 // /* HeapReference<Object> */ out =
5276 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5277 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005278 // Note that a potential implicit null check is handled in this
5279 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
5280 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00005281 instruction, out_loc, obj, data_offset, index, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005282 } else {
5283 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005284 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
5285 codegen_->MaybeRecordImplicitNullCheck(instruction);
5286 // If read barriers are enabled, emit read barriers other than
5287 // Baker's using a slow path (and also unpoison the loaded
5288 // reference, if heap poisoning is enabled).
Roland Levillain7c1559a2015-12-15 10:55:36 +00005289 if (index.IsConstant()) {
5290 uint32_t offset =
5291 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005292 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5293 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005294 codegen_->MaybeGenerateReadBarrierSlow(
5295 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5296 }
5297 }
5298 break;
5299 }
5300
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005301 case DataType::Type::kInt64: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005302 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005303 __ movl(out_loc.AsRegisterPairLow<Register>(),
5304 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
5305 codegen_->MaybeRecordImplicitNullCheck(instruction);
5306 __ movl(out_loc.AsRegisterPairHigh<Register>(),
5307 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005308 break;
5309 }
5310
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005311 case DataType::Type::kFloat32: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005312 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005313 __ movss(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005314 break;
5315 }
5316
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005317 case DataType::Type::kFloat64: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005318 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005319 __ movsd(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005320 break;
5321 }
5322
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005323 case DataType::Type::kVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00005324 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005325 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005326 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005327
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005328 if (type == DataType::Type::kReference || type == DataType::Type::kInt64) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005329 // Potential implicit null checks, in the case of reference or
5330 // long arrays, are handled in the previous switch statement.
5331 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005332 codegen_->MaybeRecordImplicitNullCheck(instruction);
5333 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005334}
5335
5336void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005337 DataType::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005338
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005339 bool needs_write_barrier =
5340 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005341 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005342
Vladimir Markoca6fff82017-10-03 14:49:14 +01005343 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Nicolas Geoffray39468442014-09-02 15:17:15 +01005344 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01005345 may_need_runtime_call_for_type_check ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00005346 LocationSummary::kCallOnSlowPath :
5347 LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005348
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005349 bool is_byte_type = DataType::Size(value_type) == 1u;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005350 // We need the inputs to be different than the output in case of long operation.
5351 // In case of a byte operation, the register allocator does not support multiple
5352 // inputs that die at entry with one in a specific register.
5353 locations->SetInAt(0, Location::RequiresRegister());
5354 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5355 if (is_byte_type) {
5356 // Ensure the value is in a byte register.
5357 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005358 } else if (DataType::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05005359 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005360 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005361 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
5362 }
5363 if (needs_write_barrier) {
5364 // Temporary registers for the write barrier.
5365 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5366 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005367 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005368 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005369}
5370
5371void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
5372 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005373 Location array_loc = locations->InAt(0);
5374 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005375 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005376 Location value = locations->InAt(2);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005377 DataType::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005378 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5379 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5380 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005381 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005382 bool needs_write_barrier =
5383 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005384
5385 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005386 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005387 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005388 case DataType::Type::kInt8: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005389 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005390 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_1, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005391 if (value.IsRegister()) {
5392 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005393 } else {
Nicolas Geoffray78612082017-07-24 14:18:53 +01005394 __ movb(address, Immediate(CodeGenerator::GetInt8ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005395 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005396 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005397 break;
5398 }
5399
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005400 case DataType::Type::kUint16:
5401 case DataType::Type::kInt16: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005402 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005403 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_2, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005404 if (value.IsRegister()) {
5405 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005406 } else {
Nicolas Geoffray78612082017-07-24 14:18:53 +01005407 __ movw(address, Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005408 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005409 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005410 break;
5411 }
5412
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005413 case DataType::Type::kReference: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005414 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005415 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005416
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005417 if (!value.IsRegister()) {
5418 // Just setting null.
5419 DCHECK(instruction->InputAt(2)->IsNullConstant());
5420 DCHECK(value.IsConstant()) << value;
5421 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00005422 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005423 DCHECK(!needs_write_barrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005424 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005425 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005426 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005427
5428 DCHECK(needs_write_barrier);
5429 Register register_value = value.AsRegister<Register>();
Roland Levillain16d9f942016-08-25 17:27:56 +01005430 // We cannot use a NearLabel for `done`, as its range may be too
5431 // short when Baker read barriers are enabled.
5432 Label done;
5433 NearLabel not_null, do_put;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005434 SlowPathCode* slow_path = nullptr;
Roland Levillain16d9f942016-08-25 17:27:56 +01005435 Location temp_loc = locations->GetTemp(0);
5436 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005437 if (may_need_runtime_call_for_type_check) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005438 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathX86(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005439 codegen_->AddSlowPath(slow_path);
5440 if (instruction->GetValueCanBeNull()) {
5441 __ testl(register_value, register_value);
5442 __ j(kNotEqual, &not_null);
5443 __ movl(address, Immediate(0));
5444 codegen_->MaybeRecordImplicitNullCheck(instruction);
5445 __ jmp(&done);
5446 __ Bind(&not_null);
5447 }
5448
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005449 // Note that when Baker read barriers are enabled, the type
5450 // checks are performed without read barriers. This is fine,
5451 // even in the case where a class object is in the from-space
5452 // after the flip, as a comparison involving such a type would
5453 // not produce a false positive; it may of course produce a
5454 // false negative, in which case we would take the ArraySet
5455 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01005456
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005457 // /* HeapReference<Class> */ temp = array->klass_
5458 __ movl(temp, Address(array, class_offset));
5459 codegen_->MaybeRecordImplicitNullCheck(instruction);
5460 __ MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01005461
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005462 // /* HeapReference<Class> */ temp = temp->component_type_
5463 __ movl(temp, Address(temp, component_offset));
5464 // If heap poisoning is enabled, no need to unpoison `temp`
5465 // nor the object reference in `register_value->klass`, as
5466 // we are comparing two poisoned references.
5467 __ cmpl(temp, Address(register_value, class_offset));
Roland Levillain16d9f942016-08-25 17:27:56 +01005468
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005469 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5470 __ j(kEqual, &do_put);
5471 // If heap poisoning is enabled, the `temp` reference has
5472 // not been unpoisoned yet; unpoison it now.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005473 __ MaybeUnpoisonHeapReference(temp);
5474
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005475 // If heap poisoning is enabled, no need to unpoison the
5476 // heap reference loaded below, as it is only used for a
5477 // comparison with null.
5478 __ cmpl(Address(temp, super_offset), Immediate(0));
5479 __ j(kNotEqual, slow_path->GetEntryLabel());
5480 __ Bind(&do_put);
5481 } else {
5482 __ j(kNotEqual, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005483 }
5484 }
5485
5486 if (kPoisonHeapReferences) {
5487 __ movl(temp, register_value);
5488 __ PoisonHeapReference(temp);
5489 __ movl(address, temp);
5490 } else {
5491 __ movl(address, register_value);
5492 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005493 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005494 codegen_->MaybeRecordImplicitNullCheck(instruction);
5495 }
5496
5497 Register card = locations->GetTemp(1).AsRegister<Register>();
5498 codegen_->MarkGCCard(
5499 temp, card, array, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
5500 __ Bind(&done);
5501
5502 if (slow_path != nullptr) {
5503 __ Bind(slow_path->GetExitLabel());
5504 }
5505
5506 break;
5507 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005508
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005509 case DataType::Type::kInt32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005510 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005511 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005512 if (value.IsRegister()) {
5513 __ movl(address, value.AsRegister<Register>());
5514 } else {
5515 DCHECK(value.IsConstant()) << value;
5516 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5517 __ movl(address, Immediate(v));
5518 }
5519 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005520 break;
5521 }
5522
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005523 case DataType::Type::kInt64: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005524 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005525 if (value.IsRegisterPair()) {
5526 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
5527 value.AsRegisterPairLow<Register>());
5528 codegen_->MaybeRecordImplicitNullCheck(instruction);
5529 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
5530 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005531 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005532 DCHECK(value.IsConstant());
5533 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
5534 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
5535 Immediate(Low32Bits(val)));
5536 codegen_->MaybeRecordImplicitNullCheck(instruction);
5537 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
5538 Immediate(High32Bits(val)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005539 }
5540 break;
5541 }
5542
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005543 case DataType::Type::kFloat32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005544 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005545 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005546 if (value.IsFpuRegister()) {
5547 __ movss(address, value.AsFpuRegister<XmmRegister>());
5548 } else {
5549 DCHECK(value.IsConstant());
5550 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
5551 __ movl(address, Immediate(v));
5552 }
5553 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005554 break;
5555 }
5556
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005557 case DataType::Type::kFloat64: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005558 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005559 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005560 if (value.IsFpuRegister()) {
5561 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5562 } else {
5563 DCHECK(value.IsConstant());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005564 Address address_hi =
5565 CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset + kX86WordSize);
Mark Mendell81489372015-11-04 11:30:41 -05005566 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5567 __ movl(address, Immediate(Low32Bits(v)));
5568 codegen_->MaybeRecordImplicitNullCheck(instruction);
5569 __ movl(address_hi, Immediate(High32Bits(v)));
5570 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005571 break;
5572 }
5573
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005574 case DataType::Type::kVoid:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005575 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005576 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005577 }
5578}
5579
5580void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005581 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005582 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04005583 if (!instruction->IsEmittedAtUseSite()) {
5584 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5585 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005586}
5587
5588void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04005589 if (instruction->IsEmittedAtUseSite()) {
5590 return;
5591 }
5592
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005593 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005594 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005595 Register obj = locations->InAt(0).AsRegister<Register>();
5596 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005597 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005598 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo4877b792016-09-08 19:49:13 -07005599 // Mask out most significant bit in case the array is String's array of char.
5600 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005601 __ shrl(out, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005602 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005603}
5604
5605void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005606 RegisterSet caller_saves = RegisterSet::Empty();
5607 InvokeRuntimeCallingConvention calling_convention;
5608 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5609 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5610 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005611 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04005612 HInstruction* length = instruction->InputAt(1);
5613 if (!length->IsEmittedAtUseSite()) {
5614 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5615 }
jessicahandojo4877b792016-09-08 19:49:13 -07005616 // Need register to see array's length.
5617 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5618 locations->AddTemp(Location::RequiresRegister());
5619 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005620}
5621
5622void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
jessicahandojo4877b792016-09-08 19:49:13 -07005623 const bool is_string_compressed_char_at =
5624 mirror::kUseStringCompression && instruction->IsStringCharAt();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005625 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005626 Location index_loc = locations->InAt(0);
5627 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07005628 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01005629 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005630
Mark Mendell99dbd682015-04-22 16:18:52 -04005631 if (length_loc.IsConstant()) {
5632 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5633 if (index_loc.IsConstant()) {
5634 // BCE will remove the bounds check if we are guarenteed to pass.
5635 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5636 if (index < 0 || index >= length) {
5637 codegen_->AddSlowPath(slow_path);
5638 __ jmp(slow_path->GetEntryLabel());
5639 } else {
5640 // Some optimization after BCE may have generated this, and we should not
5641 // generate a bounds check if it is a valid range.
5642 }
5643 return;
5644 }
5645
5646 // We have to reverse the jump condition because the length is the constant.
5647 Register index_reg = index_loc.AsRegister<Register>();
5648 __ cmpl(index_reg, Immediate(length));
5649 codegen_->AddSlowPath(slow_path);
5650 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005651 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04005652 HInstruction* array_length = instruction->InputAt(1);
5653 if (array_length->IsEmittedAtUseSite()) {
5654 // Address the length field in the array.
5655 DCHECK(array_length->IsArrayLength());
5656 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
5657 Location array_loc = array_length->GetLocations()->InAt(0);
5658 Address array_len(array_loc.AsRegister<Register>(), len_offset);
jessicahandojo4877b792016-09-08 19:49:13 -07005659 if (is_string_compressed_char_at) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005660 // TODO: if index_loc.IsConstant(), compare twice the index (to compensate for
5661 // the string compression flag) with the in-memory length and avoid the temporary.
jessicahandojo4877b792016-09-08 19:49:13 -07005662 Register length_reg = locations->GetTemp(0).AsRegister<Register>();
5663 __ movl(length_reg, array_len);
5664 codegen_->MaybeRecordImplicitNullCheck(array_length);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005665 __ shrl(length_reg, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005666 codegen_->GenerateIntCompare(length_reg, index_loc);
Mark Mendellee8d9712016-07-12 11:13:15 -04005667 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07005668 // Checking bounds for general case:
5669 // Array of char or string's array with feature compression off.
5670 if (index_loc.IsConstant()) {
5671 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5672 __ cmpl(array_len, Immediate(value));
5673 } else {
5674 __ cmpl(array_len, index_loc.AsRegister<Register>());
5675 }
5676 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendellee8d9712016-07-12 11:13:15 -04005677 }
Mark Mendell99dbd682015-04-22 16:18:52 -04005678 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005679 codegen_->GenerateIntCompare(length_loc, index_loc);
Mark Mendell99dbd682015-04-22 16:18:52 -04005680 }
5681 codegen_->AddSlowPath(slow_path);
5682 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005683 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005684}
5685
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005686void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005687 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005688}
5689
5690void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01005691 if (instruction->GetNext()->IsSuspendCheck() &&
5692 instruction->GetBlock()->GetLoopInformation() != nullptr) {
5693 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
5694 // The back edge will generate the suspend check.
5695 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
5696 }
5697
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005698 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5699}
5700
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005701void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005702 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5703 instruction, LocationSummary::kCallOnSlowPath);
Aart Bikb13c65b2017-03-21 20:14:07 -07005704 // In suspend check slow path, usually there are no caller-save registers at all.
5705 // If SIMD instructions are present, however, we force spilling all live SIMD
5706 // registers in full width (since the runtime only saves/restores lower part).
Aart Bik5576f372017-03-23 16:17:37 -07005707 locations->SetCustomSlowPathCallerSaves(
5708 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005709}
5710
5711void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005712 HBasicBlock* block = instruction->GetBlock();
5713 if (block->GetLoopInformation() != nullptr) {
5714 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5715 // The back edge will generate the suspend check.
5716 return;
5717 }
5718 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5719 // The goto will generate the suspend check.
5720 return;
5721 }
5722 GenerateSuspendCheck(instruction, nullptr);
5723}
5724
5725void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
5726 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005727 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005728 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
5729 if (slow_path == nullptr) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005730 slow_path =
5731 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005732 instruction->SetSlowPath(slow_path);
5733 codegen_->AddSlowPath(slow_path);
5734 if (successor != nullptr) {
5735 DCHECK(successor->IsLoopHeader());
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005736 }
5737 } else {
5738 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5739 }
5740
Andreas Gampe542451c2016-07-26 09:02:02 -07005741 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86PointerSize>().Int32Value()),
Roland Levillain7c1559a2015-12-15 10:55:36 +00005742 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005743 if (successor == nullptr) {
5744 __ j(kNotEqual, slow_path->GetEntryLabel());
5745 __ Bind(slow_path->GetReturnLabel());
5746 } else {
5747 __ j(kEqual, codegen_->GetLabelOf(successor));
5748 __ jmp(slow_path->GetEntryLabel());
5749 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005750}
5751
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005752X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
5753 return codegen_->GetAssembler();
5754}
5755
Aart Bikcfe50bb2017-12-12 14:54:12 -08005756void ParallelMoveResolverX86::MoveMemoryToMemory(int dst, int src, int number_of_words) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005757 ScratchRegisterScope ensure_scratch(
5758 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5759 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5760 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
Mark Mendell7c8d0092015-01-26 11:21:33 -05005761
Aart Bikcfe50bb2017-12-12 14:54:12 -08005762 // Now that temp register is available (possibly spilled), move blocks of memory.
5763 for (int i = 0; i < number_of_words; i++) {
5764 __ movl(temp_reg, Address(ESP, src + stack_offset));
5765 __ movl(Address(ESP, dst + stack_offset), temp_reg);
5766 stack_offset += kX86WordSize;
5767 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005768}
5769
5770void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005771 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005772 Location source = move->GetSource();
5773 Location destination = move->GetDestination();
5774
5775 if (source.IsRegister()) {
5776 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005777 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005778 } else if (destination.IsFpuRegister()) {
5779 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005780 } else {
5781 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005782 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005783 }
David Brazdil74eb1b22015-12-14 11:44:01 +00005784 } else if (source.IsRegisterPair()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005785 size_t elem_size = DataType::Size(DataType::Type::kInt32);
David Brazdil74eb1b22015-12-14 11:44:01 +00005786 // Create stack space for 2 elements.
5787 __ subl(ESP, Immediate(2 * elem_size));
5788 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
5789 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
5790 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
5791 // And remove the temporary stack space we allocated.
5792 __ addl(ESP, Immediate(2 * elem_size));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005793 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005794 if (destination.IsRegister()) {
5795 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
5796 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005797 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005798 } else if (destination.IsRegisterPair()) {
5799 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
5800 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
5801 __ psrlq(src_reg, Immediate(32));
5802 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005803 } else if (destination.IsStackSlot()) {
5804 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07005805 } else if (destination.IsDoubleStackSlot()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005806 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07005807 } else {
5808 DCHECK(destination.IsSIMDStackSlot());
5809 __ movups(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005810 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005811 } else if (source.IsStackSlot()) {
5812 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005813 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005814 } else if (destination.IsFpuRegister()) {
5815 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005816 } else {
5817 DCHECK(destination.IsStackSlot());
Aart Bikcfe50bb2017-12-12 14:54:12 -08005818 MoveMemoryToMemory(destination.GetStackIndex(), source.GetStackIndex(), 1);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005819 }
5820 } else if (source.IsDoubleStackSlot()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005821 if (destination.IsRegisterPair()) {
5822 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
5823 __ movl(destination.AsRegisterPairHigh<Register>(),
5824 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
5825 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005826 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
5827 } else {
5828 DCHECK(destination.IsDoubleStackSlot()) << destination;
Aart Bikcfe50bb2017-12-12 14:54:12 -08005829 MoveMemoryToMemory(destination.GetStackIndex(), source.GetStackIndex(), 2);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005830 }
Aart Bik5576f372017-03-23 16:17:37 -07005831 } else if (source.IsSIMDStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08005832 if (destination.IsFpuRegister()) {
5833 __ movups(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
5834 } else {
5835 DCHECK(destination.IsSIMDStackSlot());
5836 MoveMemoryToMemory(destination.GetStackIndex(), source.GetStackIndex(), 4);
5837 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005838 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005839 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005840 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005841 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005842 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005843 if (value == 0) {
5844 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
5845 } else {
5846 __ movl(destination.AsRegister<Register>(), Immediate(value));
5847 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005848 } else {
5849 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05005850 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005851 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005852 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005853 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005854 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005855 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005856 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005857 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5858 if (value == 0) {
5859 // Easy handling of 0.0.
5860 __ xorps(dest, dest);
5861 } else {
5862 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005863 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5864 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
5865 __ movl(temp, Immediate(value));
5866 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005867 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005868 } else {
5869 DCHECK(destination.IsStackSlot()) << destination;
5870 __ movl(Address(ESP, destination.GetStackIndex()), imm);
5871 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005872 } else if (constant->IsLongConstant()) {
5873 int64_t value = constant->AsLongConstant()->GetValue();
5874 int32_t low_value = Low32Bits(value);
5875 int32_t high_value = High32Bits(value);
5876 Immediate low(low_value);
5877 Immediate high(high_value);
5878 if (destination.IsDoubleStackSlot()) {
5879 __ movl(Address(ESP, destination.GetStackIndex()), low);
5880 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5881 } else {
5882 __ movl(destination.AsRegisterPairLow<Register>(), low);
5883 __ movl(destination.AsRegisterPairHigh<Register>(), high);
5884 }
5885 } else {
5886 DCHECK(constant->IsDoubleConstant());
5887 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005888 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005889 int32_t low_value = Low32Bits(value);
5890 int32_t high_value = High32Bits(value);
5891 Immediate low(low_value);
5892 Immediate high(high_value);
5893 if (destination.IsFpuRegister()) {
5894 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5895 if (value == 0) {
5896 // Easy handling of 0.0.
5897 __ xorpd(dest, dest);
5898 } else {
5899 __ pushl(high);
5900 __ pushl(low);
5901 __ movsd(dest, Address(ESP, 0));
5902 __ addl(ESP, Immediate(8));
5903 }
5904 } else {
5905 DCHECK(destination.IsDoubleStackSlot()) << destination;
5906 __ movl(Address(ESP, destination.GetStackIndex()), low);
5907 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5908 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005909 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005910 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005911 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005912 }
5913}
5914
Mark Mendella5c19ce2015-04-01 12:51:05 -04005915void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005916 Register suggested_scratch = reg == EAX ? EBX : EAX;
5917 ScratchRegisterScope ensure_scratch(
5918 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
5919
5920 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5921 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
5922 __ movl(Address(ESP, mem + stack_offset), reg);
5923 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005924}
5925
Mark Mendell7c8d0092015-01-26 11:21:33 -05005926void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005927 ScratchRegisterScope ensure_scratch(
5928 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5929
5930 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5931 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5932 __ movl(temp_reg, Address(ESP, mem + stack_offset));
5933 __ movss(Address(ESP, mem + stack_offset), reg);
5934 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005935}
5936
Aart Bikcfe50bb2017-12-12 14:54:12 -08005937void ParallelMoveResolverX86::Exchange128(XmmRegister reg, int mem) {
5938 size_t extra_slot = 4 * kX86WordSize;
5939 __ subl(ESP, Immediate(extra_slot));
5940 __ movups(Address(ESP, 0), XmmRegister(reg));
5941 ExchangeMemory(0, mem + extra_slot, 4);
5942 __ movups(XmmRegister(reg), Address(ESP, 0));
5943 __ addl(ESP, Immediate(extra_slot));
5944}
5945
5946void ParallelMoveResolverX86::ExchangeMemory(int mem1, int mem2, int number_of_words) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005947 ScratchRegisterScope ensure_scratch1(
5948 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005949
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005950 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
5951 ScratchRegisterScope ensure_scratch2(
5952 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005953
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005954 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
5955 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
Aart Bikcfe50bb2017-12-12 14:54:12 -08005956
5957 // Now that temp registers are available (possibly spilled), exchange blocks of memory.
5958 for (int i = 0; i < number_of_words; i++) {
5959 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
5960 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
5961 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
5962 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
5963 stack_offset += kX86WordSize;
5964 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005965}
5966
5967void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005968 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005969 Location source = move->GetSource();
5970 Location destination = move->GetDestination();
5971
5972 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04005973 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
5974 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
5975 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
5976 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
5977 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005978 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005979 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005980 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005981 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005982 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08005983 ExchangeMemory(destination.GetStackIndex(), source.GetStackIndex(), 1);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005984 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
5985 // Use XOR Swap algorithm to avoid a temporary.
5986 DCHECK_NE(source.reg(), destination.reg());
5987 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5988 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5989 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5990 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
5991 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
5992 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
5993 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005994 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
5995 // Take advantage of the 16 bytes in the XMM register.
5996 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
5997 Address stack(ESP, destination.GetStackIndex());
5998 // Load the double into the high doubleword.
5999 __ movhpd(reg, stack);
6000
6001 // Store the low double into the destination.
6002 __ movsd(stack, reg);
6003
6004 // Move the high double to the low double.
6005 __ psrldq(reg, Immediate(8));
6006 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
6007 // Take advantage of the 16 bytes in the XMM register.
6008 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
6009 Address stack(ESP, source.GetStackIndex());
6010 // Load the double into the high doubleword.
6011 __ movhpd(reg, stack);
6012
6013 // Store the low double into the destination.
6014 __ movsd(stack, reg);
6015
6016 // Move the high double to the low double.
6017 __ psrldq(reg, Immediate(8));
6018 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08006019 ExchangeMemory(destination.GetStackIndex(), source.GetStackIndex(), 2);
6020 } else if (source.IsSIMDStackSlot() && destination.IsSIMDStackSlot()) {
6021 ExchangeMemory(destination.GetStackIndex(), source.GetStackIndex(), 4);
6022 } else if (source.IsFpuRegister() && destination.IsSIMDStackSlot()) {
6023 Exchange128(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
6024 } else if (destination.IsFpuRegister() && source.IsSIMDStackSlot()) {
6025 Exchange128(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006026 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006027 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006028 }
6029}
6030
6031void ParallelMoveResolverX86::SpillScratch(int reg) {
6032 __ pushl(static_cast<Register>(reg));
6033}
6034
6035void ParallelMoveResolverX86::RestoreScratch(int reg) {
6036 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006037}
6038
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006039HLoadClass::LoadKind CodeGeneratorX86::GetSupportedLoadClassKind(
6040 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006041 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006042 case HLoadClass::LoadKind::kInvalid:
6043 LOG(FATAL) << "UNREACHABLE";
6044 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006045 case HLoadClass::LoadKind::kReferrersClass:
6046 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006047 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006048 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006049 case HLoadClass::LoadKind::kBssEntry:
Vladimir Marko764d4542017-05-16 10:31:41 +01006050 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006051 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006052 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006053 DCHECK(Runtime::Current()->UseJitCompilation());
6054 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01006055 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006056 case HLoadClass::LoadKind::kRuntimeCall:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006057 break;
6058 }
6059 return desired_class_load_kind;
6060}
6061
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006062void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00006063 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006064 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006065 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00006066 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006067 cls,
6068 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00006069 Location::RegisterLocation(EAX));
Vladimir Markoea4c1262017-02-06 19:59:33 +00006070 DCHECK_EQ(calling_convention.GetRegisterAt(0), EAX);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006071 return;
6072 }
Vladimir Marko41559982017-01-06 14:04:23 +00006073 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006074
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006075 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
6076 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006077 ? LocationSummary::kCallOnSlowPath
6078 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01006079 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006080 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006081 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006082 }
6083
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006084 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006085 load_kind == HLoadClass::LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006086 load_kind == HLoadClass::LoadKind::kBootImageClassTable ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006087 load_kind == HLoadClass::LoadKind::kBssEntry) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006088 locations->SetInAt(0, Location::RequiresRegister());
6089 }
6090 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006091 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
6092 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6093 // Rely on the type resolution and/or initialization to save everything.
6094 RegisterSet caller_saves = RegisterSet::Empty();
6095 InvokeRuntimeCallingConvention calling_convention;
6096 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6097 locations->SetCustomSlowPathCallerSaves(caller_saves);
6098 } else {
6099 // For non-Baker read barrier we have a temp-clobbering call.
6100 }
6101 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006102}
6103
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006104Label* CodeGeneratorX86::NewJitRootClassPatch(const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01006105 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006106 Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006107 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006108 // Add a patch entry and return the label.
Vladimir Marko174b2e22017-10-12 13:34:49 +01006109 jit_class_patches_.emplace_back(dex_file, type_index.index_);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006110 PatchInfo<Label>* info = &jit_class_patches_.back();
6111 return &info->label;
6112}
6113
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006114// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6115// move.
6116void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00006117 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006118 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00006119 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01006120 return;
6121 }
Vladimir Marko41559982017-01-06 14:04:23 +00006122 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01006123
Vladimir Marko41559982017-01-06 14:04:23 +00006124 LocationSummary* locations = cls->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006125 Location out_loc = locations->Out();
6126 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006127
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006128 bool generate_null_check = false;
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006129 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
6130 ? kWithoutReadBarrier
6131 : kCompilerReadBarrierOption;
Vladimir Marko41559982017-01-06 14:04:23 +00006132 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006133 case HLoadClass::LoadKind::kReferrersClass: {
6134 DCHECK(!cls->CanCallRuntime());
6135 DCHECK(!cls->MustGenerateClinitCheck());
6136 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6137 Register current_method = locations->InAt(0).AsRegister<Register>();
6138 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006139 cls,
6140 out_loc,
6141 Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()),
Roland Levillain00468f32016-10-27 18:02:48 +01006142 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006143 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006144 break;
6145 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006146 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006147 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006148 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006149 Register method_address = locations->InAt(0).AsRegister<Register>();
6150 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Marko1998cd02017-01-13 13:02:58 +00006151 codegen_->RecordBootTypePatch(cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006152 break;
6153 }
6154 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006155 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006156 uint32_t address = dchecked_integral_cast<uint32_t>(
6157 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
6158 DCHECK_NE(address, 0u);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006159 __ movl(out, Immediate(address));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006160 break;
6161 }
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006162 case HLoadClass::LoadKind::kBootImageClassTable: {
6163 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6164 Register method_address = locations->InAt(0).AsRegister<Register>();
6165 __ movl(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
6166 codegen_->RecordBootTypePatch(cls);
6167 // Extract the reference from the slot data, i.e. clear the hash bits.
6168 int32_t masked_hash = ClassTable::TableSlot::MaskHash(
6169 ComputeModifiedUtf8Hash(cls->GetDexFile().StringByTypeIdx(cls->GetTypeIndex())));
6170 if (masked_hash != 0) {
6171 __ subl(out, Immediate(masked_hash));
6172 }
6173 break;
6174 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006175 case HLoadClass::LoadKind::kBssEntry: {
6176 Register method_address = locations->InAt(0).AsRegister<Register>();
6177 Address address(method_address, CodeGeneratorX86::kDummy32BitOffset);
6178 Label* fixup_label = codegen_->NewTypeBssEntryPatch(cls);
6179 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
6180 generate_null_check = true;
6181 break;
6182 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006183 case HLoadClass::LoadKind::kJitTableAddress: {
6184 Address address = Address::Absolute(CodeGeneratorX86::kDummy32BitOffset);
6185 Label* fixup_label = codegen_->NewJitRootClassPatch(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006186 cls->GetDexFile(), cls->GetTypeIndex(), cls->GetClass());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006187 // /* GcRoot<mirror::Class> */ out = *address
Vladimir Markoea4c1262017-02-06 19:59:33 +00006188 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006189 break;
6190 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006191 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006192 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00006193 LOG(FATAL) << "UNREACHABLE";
6194 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006195 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006196
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006197 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6198 DCHECK(cls->CanCallRuntime());
Vladimir Marko174b2e22017-10-12 13:34:49 +01006199 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathX86(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006200 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
6201 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006202
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006203 if (generate_null_check) {
6204 __ testl(out, out);
6205 __ j(kEqual, slow_path->GetEntryLabel());
6206 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006207
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006208 if (cls->MustGenerateClinitCheck()) {
6209 GenerateClassInitializationCheck(slow_path, out);
6210 } else {
6211 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006212 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006213 }
6214}
6215
6216void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
6217 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006218 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006219 locations->SetInAt(0, Location::RequiresRegister());
6220 if (check->HasUses()) {
6221 locations->SetOut(Location::SameAsFirstInput());
6222 }
6223}
6224
6225void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006226 // We assume the class to not be null.
Vladimir Marko174b2e22017-10-12 13:34:49 +01006227 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006228 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006229 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00006230 GenerateClassInitializationCheck(slow_path,
6231 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006232}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006233
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006234void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07006235 SlowPathCode* slow_path, Register class_reg) {
Vladimir Markodc682aa2018-01-04 18:42:57 +00006236 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
6237 const size_t status_byte_offset =
6238 mirror::Class::StatusOffset().SizeValue() + (status_lsb_position / kBitsPerByte);
6239 constexpr uint32_t shifted_initialized_value =
6240 enum_cast<uint32_t>(ClassStatus::kInitialized) << (status_lsb_position % kBitsPerByte);
6241
6242 __ cmpb(Address(class_reg, status_byte_offset), Immediate(shifted_initialized_value));
Vladimir Marko2c64a832018-01-04 11:31:56 +00006243 __ j(kBelow, slow_path->GetEntryLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006244 __ Bind(slow_path->GetExitLabel());
6245 // No need for memory fence, thanks to the X86 memory model.
6246}
6247
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006248HLoadString::LoadKind CodeGeneratorX86::GetSupportedLoadStringKind(
6249 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006250 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006251 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006252 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00006253 case HLoadString::LoadKind::kBssEntry:
Vladimir Marko764d4542017-05-16 10:31:41 +01006254 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006255 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006256 case HLoadString::LoadKind::kJitTableAddress:
6257 DCHECK(Runtime::Current()->UseJitCompilation());
6258 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01006259 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006260 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006261 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006262 }
6263 return desired_string_load_kind;
6264}
6265
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006266void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006267 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01006268 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006269 HLoadString::LoadKind load_kind = load->GetLoadKind();
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006270 if (load_kind == HLoadString::LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006271 load_kind == HLoadString::LoadKind::kBootImageInternTable ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00006272 load_kind == HLoadString::LoadKind::kBssEntry) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006273 locations->SetInAt(0, Location::RequiresRegister());
6274 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006275 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006276 locations->SetOut(Location::RegisterLocation(EAX));
6277 } else {
6278 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006279 if (load_kind == HLoadString::LoadKind::kBssEntry) {
6280 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00006281 // Rely on the pResolveString to save everything.
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006282 RegisterSet caller_saves = RegisterSet::Empty();
6283 InvokeRuntimeCallingConvention calling_convention;
6284 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6285 locations->SetCustomSlowPathCallerSaves(caller_saves);
6286 } else {
6287 // For non-Baker read barrier we have a temp-clobbering call.
6288 }
6289 }
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006290 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006291}
6292
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006293Label* CodeGeneratorX86::NewJitRootStringPatch(const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01006294 dex::StringIndex string_index,
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006295 Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006296 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006297 // Add a patch entry and return the label.
Vladimir Marko174b2e22017-10-12 13:34:49 +01006298 jit_string_patches_.emplace_back(dex_file, string_index.index_);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006299 PatchInfo<Label>* info = &jit_string_patches_.back();
6300 return &info->label;
6301}
6302
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006303// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6304// move.
6305void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006306 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006307 Location out_loc = locations->Out();
6308 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006309
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006310 switch (load->GetLoadKind()) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006311 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006312 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006313 Register method_address = locations->InAt(0).AsRegister<Register>();
6314 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Markoaad75c62016-10-03 08:46:48 +00006315 codegen_->RecordBootStringPatch(load);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006316 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006317 }
6318 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006319 uint32_t address = dchecked_integral_cast<uint32_t>(
6320 reinterpret_cast<uintptr_t>(load->GetString().Get()));
6321 DCHECK_NE(address, 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006322 __ movl(out, Immediate(address));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006323 return;
6324 }
6325 case HLoadString::LoadKind::kBootImageInternTable: {
6326 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6327 Register method_address = locations->InAt(0).AsRegister<Register>();
6328 __ movl(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
6329 codegen_->RecordBootStringPatch(load);
6330 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006331 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00006332 case HLoadString::LoadKind::kBssEntry: {
6333 Register method_address = locations->InAt(0).AsRegister<Register>();
6334 Address address = Address(method_address, CodeGeneratorX86::kDummy32BitOffset);
6335 Label* fixup_label = codegen_->NewStringBssEntryPatch(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006336 // /* GcRoot<mirror::String> */ out = *address /* PC-relative */
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006337 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
Vladimir Marko174b2e22017-10-12 13:34:49 +01006338 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadStringSlowPathX86(load);
Vladimir Markoaad75c62016-10-03 08:46:48 +00006339 codegen_->AddSlowPath(slow_path);
6340 __ testl(out, out);
6341 __ j(kEqual, slow_path->GetEntryLabel());
6342 __ Bind(slow_path->GetExitLabel());
6343 return;
6344 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006345 case HLoadString::LoadKind::kJitTableAddress: {
6346 Address address = Address::Absolute(CodeGeneratorX86::kDummy32BitOffset);
6347 Label* fixup_label = codegen_->NewJitRootStringPatch(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006348 load->GetDexFile(), load->GetStringIndex(), load->GetString());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006349 // /* GcRoot<mirror::String> */ out = *address
6350 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
6351 return;
6352 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006353 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006354 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006355 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006356
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006357 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006358 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006359 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006360 __ movl(calling_convention.GetRegisterAt(0), Immediate(load->GetStringIndex().index_));
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006361 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6362 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006363}
6364
David Brazdilcb1c0552015-08-04 16:22:25 +01006365static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006366 return Address::Absolute(Thread::ExceptionOffset<kX86PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01006367}
6368
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006369void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
6370 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006371 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006372 locations->SetOut(Location::RequiresRegister());
6373}
6374
6375void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01006376 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
6377}
6378
6379void LocationsBuilderX86::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006380 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
David Brazdilcb1c0552015-08-04 16:22:25 +01006381}
6382
6383void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6384 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006385}
6386
6387void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006388 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6389 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006390 InvokeRuntimeCallingConvention calling_convention;
6391 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6392}
6393
6394void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006395 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006396 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006397}
6398
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006399// Temp is used for read barrier.
6400static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
6401 if (kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00006402 !kUseBakerReadBarrier &&
6403 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain7c1559a2015-12-15 10:55:36 +00006404 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006405 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
6406 return 1;
6407 }
6408 return 0;
6409}
6410
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006411// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006412// interface pointer, one for loading the current interface.
6413// The other checks have one temp for loading the object's class.
6414static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
Vladimir Markoe619f6c2017-12-12 16:00:01 +00006415 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006416 return 2;
6417 }
6418 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006419}
6420
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006421void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006422 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006423 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01006424 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006425 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006426 case TypeCheckKind::kExactCheck:
6427 case TypeCheckKind::kAbstractClassCheck:
6428 case TypeCheckKind::kClassHierarchyCheck:
Vladimir Marko87584542017-12-12 17:47:52 +00006429 case TypeCheckKind::kArrayObjectCheck: {
6430 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
6431 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
6432 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006433 break;
Vladimir Marko87584542017-12-12 17:47:52 +00006434 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006435 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006436 case TypeCheckKind::kUnresolvedCheck:
6437 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006438 call_kind = LocationSummary::kCallOnSlowPath;
6439 break;
6440 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006441
Vladimir Markoca6fff82017-10-03 14:49:14 +01006442 LocationSummary* locations =
6443 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01006444 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006445 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006446 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006447 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00006448 locations->SetInAt(1, Location::Any());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006449 // Note that TypeCheckSlowPathX86 uses this "out" register too.
6450 locations->SetOut(Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006451 // When read barriers are enabled, we need a temporary register for some cases.
6452 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006453}
6454
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006455void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006456 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006457 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006458 Location obj_loc = locations->InAt(0);
6459 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006460 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006461 Location out_loc = locations->Out();
6462 Register out = out_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006463 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
6464 DCHECK_LE(num_temps, 1u);
6465 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006466 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006467 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6468 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6469 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07006470 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006471 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006472
6473 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006474 // Avoid null check if we know obj is not null.
6475 if (instruction->MustDoNullCheck()) {
6476 __ testl(obj, obj);
6477 __ j(kEqual, &zero);
6478 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006479
Roland Levillain7c1559a2015-12-15 10:55:36 +00006480 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006481 case TypeCheckKind::kExactCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00006482 ReadBarrierOption read_barrier_option =
6483 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006484 // /* HeapReference<Class> */ out = obj->klass_
6485 GenerateReferenceLoadTwoRegisters(instruction,
6486 out_loc,
6487 obj_loc,
6488 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00006489 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006490 if (cls.IsRegister()) {
6491 __ cmpl(out, cls.AsRegister<Register>());
6492 } else {
6493 DCHECK(cls.IsStackSlot()) << cls;
6494 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6495 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006496
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006497 // Classes must be equal for the instanceof to succeed.
6498 __ j(kNotEqual, &zero);
6499 __ movl(out, Immediate(1));
6500 __ jmp(&done);
6501 break;
6502 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006503
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006504 case TypeCheckKind::kAbstractClassCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00006505 ReadBarrierOption read_barrier_option =
6506 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006507 // /* HeapReference<Class> */ out = obj->klass_
6508 GenerateReferenceLoadTwoRegisters(instruction,
6509 out_loc,
6510 obj_loc,
6511 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00006512 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006513 // If the class is abstract, we eagerly fetch the super class of the
6514 // object to avoid doing a comparison we know will fail.
6515 NearLabel loop;
6516 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006517 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006518 GenerateReferenceLoadOneRegister(instruction,
6519 out_loc,
6520 super_offset,
6521 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00006522 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006523 __ testl(out, out);
6524 // If `out` is null, we use it for the result, and jump to `done`.
6525 __ j(kEqual, &done);
6526 if (cls.IsRegister()) {
6527 __ cmpl(out, cls.AsRegister<Register>());
6528 } else {
6529 DCHECK(cls.IsStackSlot()) << cls;
6530 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6531 }
6532 __ j(kNotEqual, &loop);
6533 __ movl(out, Immediate(1));
6534 if (zero.IsLinked()) {
6535 __ jmp(&done);
6536 }
6537 break;
6538 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006539
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006540 case TypeCheckKind::kClassHierarchyCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00006541 ReadBarrierOption read_barrier_option =
6542 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006543 // /* HeapReference<Class> */ out = obj->klass_
6544 GenerateReferenceLoadTwoRegisters(instruction,
6545 out_loc,
6546 obj_loc,
6547 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00006548 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006549 // Walk over the class hierarchy to find a match.
6550 NearLabel loop, success;
6551 __ Bind(&loop);
6552 if (cls.IsRegister()) {
6553 __ cmpl(out, cls.AsRegister<Register>());
6554 } else {
6555 DCHECK(cls.IsStackSlot()) << cls;
6556 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6557 }
6558 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006559 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006560 GenerateReferenceLoadOneRegister(instruction,
6561 out_loc,
6562 super_offset,
6563 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00006564 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006565 __ testl(out, out);
6566 __ j(kNotEqual, &loop);
6567 // If `out` is null, we use it for the result, and jump to `done`.
6568 __ jmp(&done);
6569 __ Bind(&success);
6570 __ movl(out, Immediate(1));
6571 if (zero.IsLinked()) {
6572 __ jmp(&done);
6573 }
6574 break;
6575 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006576
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006577 case TypeCheckKind::kArrayObjectCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00006578 ReadBarrierOption read_barrier_option =
6579 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006580 // /* HeapReference<Class> */ out = obj->klass_
6581 GenerateReferenceLoadTwoRegisters(instruction,
6582 out_loc,
6583 obj_loc,
6584 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00006585 read_barrier_option);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006586 // Do an exact check.
6587 NearLabel exact_check;
6588 if (cls.IsRegister()) {
6589 __ cmpl(out, cls.AsRegister<Register>());
6590 } else {
6591 DCHECK(cls.IsStackSlot()) << cls;
6592 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6593 }
6594 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006595 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006596 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006597 GenerateReferenceLoadOneRegister(instruction,
6598 out_loc,
6599 component_offset,
6600 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00006601 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006602 __ testl(out, out);
6603 // If `out` is null, we use it for the result, and jump to `done`.
6604 __ j(kEqual, &done);
6605 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
6606 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006607 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006608 __ movl(out, Immediate(1));
6609 __ jmp(&done);
6610 break;
6611 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006612
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006613 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006614 // No read barrier since the slow path will retry upon failure.
6615 // /* HeapReference<Class> */ out = obj->klass_
6616 GenerateReferenceLoadTwoRegisters(instruction,
6617 out_loc,
6618 obj_loc,
6619 class_offset,
6620 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006621 if (cls.IsRegister()) {
6622 __ cmpl(out, cls.AsRegister<Register>());
6623 } else {
6624 DCHECK(cls.IsStackSlot()) << cls;
6625 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6626 }
6627 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01006628 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
6629 instruction, /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006630 codegen_->AddSlowPath(slow_path);
6631 __ j(kNotEqual, slow_path->GetEntryLabel());
6632 __ movl(out, Immediate(1));
6633 if (zero.IsLinked()) {
6634 __ jmp(&done);
6635 }
6636 break;
6637 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006638
Calin Juravle98893e12015-10-02 21:05:03 +01006639 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006640 case TypeCheckKind::kInterfaceCheck: {
6641 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006642 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00006643 // cases.
6644 //
6645 // We cannot directly call the InstanceofNonTrivial runtime
6646 // entry point without resorting to a type checking slow path
6647 // here (i.e. by calling InvokeRuntime directly), as it would
6648 // require to assign fixed registers for the inputs of this
6649 // HInstanceOf instruction (following the runtime calling
6650 // convention), which might be cluttered by the potential first
6651 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006652 //
6653 // TODO: Introduce a new runtime entry point taking the object
6654 // to test (instead of its class) as argument, and let it deal
6655 // with the read barrier issues. This will let us refactor this
6656 // case of the `switch` code as it was previously (with a direct
6657 // call to the runtime not using a type checking slow path).
6658 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006659 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01006660 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
6661 instruction, /* is_fatal */ false);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006662 codegen_->AddSlowPath(slow_path);
6663 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006664 if (zero.IsLinked()) {
6665 __ jmp(&done);
6666 }
6667 break;
6668 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006669 }
6670
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006671 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006672 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006673 __ xorl(out, out);
6674 }
6675
6676 if (done.IsLinked()) {
6677 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006678 }
6679
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006680 if (slow_path != nullptr) {
6681 __ Bind(slow_path->GetExitLabel());
6682 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006683}
6684
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006685void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006686 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko87584542017-12-12 17:47:52 +00006687 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01006688 LocationSummary* locations =
6689 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006690 locations->SetInAt(0, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006691 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
6692 // Require a register for the interface check since there is a loop that compares the class to
6693 // a memory address.
6694 locations->SetInAt(1, Location::RequiresRegister());
6695 } else {
6696 locations->SetInAt(1, Location::Any());
6697 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006698 // Note that TypeCheckSlowPathX86 uses this "temp" register too.
6699 locations->AddTemp(Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006700 // When read barriers are enabled, we need an additional temporary register for some cases.
6701 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
6702}
6703
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006704void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006705 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006706 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006707 Location obj_loc = locations->InAt(0);
6708 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006709 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006710 Location temp_loc = locations->GetTemp(0);
6711 Register temp = temp_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006712 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
6713 DCHECK_GE(num_temps, 1u);
6714 DCHECK_LE(num_temps, 2u);
6715 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
6716 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6717 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6718 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6719 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
6720 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
6721 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
6722 const uint32_t object_array_data_offset =
6723 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006724
Vladimir Marko87584542017-12-12 17:47:52 +00006725 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006726 SlowPathCode* type_check_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006727 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
6728 instruction, is_type_check_slow_path_fatal);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006729 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006730
Roland Levillain0d5a2812015-11-13 10:07:31 +00006731 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006732 // Avoid null check if we know obj is not null.
6733 if (instruction->MustDoNullCheck()) {
6734 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006735 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006736 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006737
Roland Levillain0d5a2812015-11-13 10:07:31 +00006738 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006739 case TypeCheckKind::kExactCheck:
6740 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006741 // /* HeapReference<Class> */ temp = obj->klass_
6742 GenerateReferenceLoadTwoRegisters(instruction,
6743 temp_loc,
6744 obj_loc,
6745 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006746 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006747
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006748 if (cls.IsRegister()) {
6749 __ cmpl(temp, cls.AsRegister<Register>());
6750 } else {
6751 DCHECK(cls.IsStackSlot()) << cls;
6752 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6753 }
6754 // Jump to slow path for throwing the exception or doing a
6755 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006756 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006757 break;
6758 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006759
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006760 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006761 // /* HeapReference<Class> */ temp = obj->klass_
6762 GenerateReferenceLoadTwoRegisters(instruction,
6763 temp_loc,
6764 obj_loc,
6765 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006766 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006767
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006768 // If the class is abstract, we eagerly fetch the super class of the
6769 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006770 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006771 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006772 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006773 GenerateReferenceLoadOneRegister(instruction,
6774 temp_loc,
6775 super_offset,
6776 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006777 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006778
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006779 // If the class reference currently in `temp` is null, jump to the slow path to throw the
6780 // exception.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006781 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006782 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006783
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006784 // Otherwise, compare the classes
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006785 if (cls.IsRegister()) {
6786 __ cmpl(temp, cls.AsRegister<Register>());
6787 } else {
6788 DCHECK(cls.IsStackSlot()) << cls;
6789 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6790 }
6791 __ j(kNotEqual, &loop);
6792 break;
6793 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006794
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006795 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006796 // /* HeapReference<Class> */ temp = obj->klass_
6797 GenerateReferenceLoadTwoRegisters(instruction,
6798 temp_loc,
6799 obj_loc,
6800 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006801 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006802
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006803 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006804 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006805 __ Bind(&loop);
6806 if (cls.IsRegister()) {
6807 __ cmpl(temp, cls.AsRegister<Register>());
6808 } else {
6809 DCHECK(cls.IsStackSlot()) << cls;
6810 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6811 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006812 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006813
Roland Levillain0d5a2812015-11-13 10:07:31 +00006814 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006815 GenerateReferenceLoadOneRegister(instruction,
6816 temp_loc,
6817 super_offset,
6818 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006819 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006820
6821 // If the class reference currently in `temp` is not null, jump
6822 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006823 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006824 __ j(kNotZero, &loop);
6825 // Otherwise, jump to the slow path to throw the exception.;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006826 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006827 break;
6828 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006829
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006830 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006831 // /* HeapReference<Class> */ temp = obj->klass_
6832 GenerateReferenceLoadTwoRegisters(instruction,
6833 temp_loc,
6834 obj_loc,
6835 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006836 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006837
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006838 // Do an exact check.
6839 if (cls.IsRegister()) {
6840 __ cmpl(temp, cls.AsRegister<Register>());
6841 } else {
6842 DCHECK(cls.IsStackSlot()) << cls;
6843 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6844 }
6845 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006846
6847 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006848 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006849 GenerateReferenceLoadOneRegister(instruction,
6850 temp_loc,
6851 component_offset,
6852 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006853 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006854
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006855 // If the component type is null (i.e. the object not an array), jump to the slow path to
6856 // throw the exception. Otherwise proceed with the check.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006857 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006858 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006859
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006860 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006861 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006862 break;
6863 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006864
Calin Juravle98893e12015-10-02 21:05:03 +01006865 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006866 // We always go into the type check slow path for the unresolved check case.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006867 // We cannot directly call the CheckCast runtime entry point
6868 // without resorting to a type checking slow path here (i.e. by
6869 // calling InvokeRuntime directly), as it would require to
6870 // assign fixed registers for the inputs of this HInstanceOf
6871 // instruction (following the runtime calling convention), which
6872 // might be cluttered by the potential first read barrier
6873 // emission at the beginning of this method.
6874 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006875 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006876
6877 case TypeCheckKind::kInterfaceCheck: {
Vladimir Markoe619f6c2017-12-12 16:00:01 +00006878 // Fast path for the interface check. Try to avoid read barriers to improve the fast path.
6879 // We can not get false positives by doing this.
6880 // /* HeapReference<Class> */ temp = obj->klass_
6881 GenerateReferenceLoadTwoRegisters(instruction,
6882 temp_loc,
6883 obj_loc,
6884 class_offset,
6885 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006886
Vladimir Markoe619f6c2017-12-12 16:00:01 +00006887 // /* HeapReference<Class> */ temp = temp->iftable_
6888 GenerateReferenceLoadTwoRegisters(instruction,
6889 temp_loc,
6890 temp_loc,
6891 iftable_offset,
6892 kWithoutReadBarrier);
6893 // Iftable is never null.
6894 __ movl(maybe_temp2_loc.AsRegister<Register>(), Address(temp, array_length_offset));
6895 // Maybe poison the `cls` for direct comparison with memory.
6896 __ MaybePoisonHeapReference(cls.AsRegister<Register>());
6897 // Loop through the iftable and check if any class matches.
6898 NearLabel start_loop;
6899 __ Bind(&start_loop);
6900 // Need to subtract first to handle the empty array case.
6901 __ subl(maybe_temp2_loc.AsRegister<Register>(), Immediate(2));
6902 __ j(kNegative, type_check_slow_path->GetEntryLabel());
6903 // Go to next interface if the classes do not match.
6904 __ cmpl(cls.AsRegister<Register>(),
6905 CodeGeneratorX86::ArrayAddress(temp,
6906 maybe_temp2_loc,
6907 TIMES_4,
6908 object_array_data_offset));
6909 __ j(kNotEqual, &start_loop);
6910 // If `cls` was poisoned above, unpoison it.
6911 __ MaybeUnpoisonHeapReference(cls.AsRegister<Register>());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006912 break;
6913 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006914 }
6915 __ Bind(&done);
6916
Roland Levillain0d5a2812015-11-13 10:07:31 +00006917 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006918}
6919
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006920void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006921 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6922 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006923 InvokeRuntimeCallingConvention calling_convention;
6924 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6925}
6926
6927void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006928 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject
6929 : kQuickUnlockObject,
Alexandre Rames8158f282015-08-07 10:26:17 +01006930 instruction,
Serban Constantinescuba45db02016-07-12 22:53:02 +01006931 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006932 if (instruction->IsEnter()) {
6933 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6934 } else {
6935 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6936 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006937}
6938
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006939void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6940void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6941void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6942
6943void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6944 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006945 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006946 DCHECK(instruction->GetResultType() == DataType::Type::kInt32
6947 || instruction->GetResultType() == DataType::Type::kInt64);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006948 locations->SetInAt(0, Location::RequiresRegister());
6949 locations->SetInAt(1, Location::Any());
6950 locations->SetOut(Location::SameAsFirstInput());
6951}
6952
6953void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
6954 HandleBitwiseOperation(instruction);
6955}
6956
6957void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
6958 HandleBitwiseOperation(instruction);
6959}
6960
6961void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
6962 HandleBitwiseOperation(instruction);
6963}
6964
6965void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6966 LocationSummary* locations = instruction->GetLocations();
6967 Location first = locations->InAt(0);
6968 Location second = locations->InAt(1);
6969 DCHECK(first.Equals(locations->Out()));
6970
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006971 if (instruction->GetResultType() == DataType::Type::kInt32) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006972 if (second.IsRegister()) {
6973 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006974 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006975 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006976 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006977 } else {
6978 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006979 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006980 }
6981 } else if (second.IsConstant()) {
6982 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006983 __ andl(first.AsRegister<Register>(),
6984 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006985 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006986 __ orl(first.AsRegister<Register>(),
6987 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006988 } else {
6989 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00006990 __ xorl(first.AsRegister<Register>(),
6991 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006992 }
6993 } else {
6994 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006995 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006996 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006997 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006998 } else {
6999 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00007000 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007001 }
7002 }
7003 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007004 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007005 if (second.IsRegisterPair()) {
7006 if (instruction->IsAnd()) {
7007 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7008 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7009 } else if (instruction->IsOr()) {
7010 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7011 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7012 } else {
7013 DCHECK(instruction->IsXor());
7014 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7015 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7016 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007017 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007018 if (instruction->IsAnd()) {
7019 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7020 __ andl(first.AsRegisterPairHigh<Register>(),
7021 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7022 } else if (instruction->IsOr()) {
7023 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7024 __ orl(first.AsRegisterPairHigh<Register>(),
7025 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7026 } else {
7027 DCHECK(instruction->IsXor());
7028 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7029 __ xorl(first.AsRegisterPairHigh<Register>(),
7030 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7031 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007032 } else {
7033 DCHECK(second.IsConstant()) << second;
7034 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007035 int32_t low_value = Low32Bits(value);
7036 int32_t high_value = High32Bits(value);
7037 Immediate low(low_value);
7038 Immediate high(high_value);
7039 Register first_low = first.AsRegisterPairLow<Register>();
7040 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007041 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007042 if (low_value == 0) {
7043 __ xorl(first_low, first_low);
7044 } else if (low_value != -1) {
7045 __ andl(first_low, low);
7046 }
7047 if (high_value == 0) {
7048 __ xorl(first_high, first_high);
7049 } else if (high_value != -1) {
7050 __ andl(first_high, high);
7051 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007052 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007053 if (low_value != 0) {
7054 __ orl(first_low, low);
7055 }
7056 if (high_value != 0) {
7057 __ orl(first_high, high);
7058 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007059 } else {
7060 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007061 if (low_value != 0) {
7062 __ xorl(first_low, low);
7063 }
7064 if (high_value != 0) {
7065 __ xorl(first_high, high);
7066 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007067 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007068 }
7069 }
7070}
7071
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007072void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(
7073 HInstruction* instruction,
7074 Location out,
7075 uint32_t offset,
7076 Location maybe_temp,
7077 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007078 Register out_reg = out.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007079 if (read_barrier_option == kWithReadBarrier) {
7080 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007081 if (kUseBakerReadBarrier) {
7082 // Load with fast path based Baker's read barrier.
7083 // /* HeapReference<Object> */ out = *(out + offset)
7084 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00007085 instruction, out, out_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007086 } else {
7087 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007088 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain7c1559a2015-12-15 10:55:36 +00007089 // in the following move operation, as we will need it for the
7090 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00007091 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007092 __ movl(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007093 // /* HeapReference<Object> */ out = *(out + offset)
7094 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007095 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007096 }
7097 } else {
7098 // Plain load with no read barrier.
7099 // /* HeapReference<Object> */ out = *(out + offset)
7100 __ movl(out_reg, Address(out_reg, offset));
7101 __ MaybeUnpoisonHeapReference(out_reg);
7102 }
7103}
7104
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007105void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(
7106 HInstruction* instruction,
7107 Location out,
7108 Location obj,
7109 uint32_t offset,
7110 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007111 Register out_reg = out.AsRegister<Register>();
7112 Register obj_reg = obj.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007113 if (read_barrier_option == kWithReadBarrier) {
7114 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007115 if (kUseBakerReadBarrier) {
7116 // Load with fast path based Baker's read barrier.
7117 // /* HeapReference<Object> */ out = *(obj + offset)
7118 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00007119 instruction, out, obj_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007120 } else {
7121 // Load with slow path based read barrier.
7122 // /* HeapReference<Object> */ out = *(obj + offset)
7123 __ movl(out_reg, Address(obj_reg, offset));
7124 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
7125 }
7126 } else {
7127 // Plain load with no read barrier.
7128 // /* HeapReference<Object> */ out = *(obj + offset)
7129 __ movl(out_reg, Address(obj_reg, offset));
7130 __ MaybeUnpoisonHeapReference(out_reg);
7131 }
7132}
7133
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007134void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(
7135 HInstruction* instruction,
7136 Location root,
7137 const Address& address,
7138 Label* fixup_label,
7139 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007140 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007141 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07007142 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007143 if (kUseBakerReadBarrier) {
7144 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
7145 // Baker's read barrier are used:
7146 //
Roland Levillaind966ce72017-02-09 16:20:14 +00007147 // root = obj.field;
7148 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
7149 // if (temp != null) {
7150 // root = temp(root)
Roland Levillain7c1559a2015-12-15 10:55:36 +00007151 // }
7152
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007153 // /* GcRoot<mirror::Object> */ root = *address
7154 __ movl(root_reg, address);
7155 if (fixup_label != nullptr) {
7156 __ Bind(fixup_label);
7157 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007158 static_assert(
7159 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
7160 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
7161 "have different sizes.");
7162 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
7163 "art::mirror::CompressedReference<mirror::Object> and int32_t "
7164 "have different sizes.");
7165
Vladimir Marko953437b2016-08-24 08:30:46 +00007166 // Slow path marking the GC root `root`.
Vladimir Marko174b2e22017-10-12 13:34:49 +01007167 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathX86(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007168 instruction, root, /* unpoison_ref_before_marking */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007169 codegen_->AddSlowPath(slow_path);
7170
Roland Levillaind966ce72017-02-09 16:20:14 +00007171 // Test the entrypoint (`Thread::Current()->pReadBarrierMarkReg ## root.reg()`).
7172 const int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +01007173 Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(root.reg());
Roland Levillaind966ce72017-02-09 16:20:14 +00007174 __ fs()->cmpl(Address::Absolute(entry_point_offset), Immediate(0));
7175 // The entrypoint is null when the GC is not marking.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007176 __ j(kNotEqual, slow_path->GetEntryLabel());
7177 __ Bind(slow_path->GetExitLabel());
7178 } else {
7179 // GC root loaded through a slow path for read barriers other
7180 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007181 // /* GcRoot<mirror::Object>* */ root = address
7182 __ leal(root_reg, address);
7183 if (fixup_label != nullptr) {
7184 __ Bind(fixup_label);
7185 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007186 // /* mirror::Object* */ root = root->Read()
7187 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
7188 }
7189 } else {
7190 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007191 // /* GcRoot<mirror::Object> */ root = *address
7192 __ movl(root_reg, address);
7193 if (fixup_label != nullptr) {
7194 __ Bind(fixup_label);
7195 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007196 // Note that GC roots are not affected by heap poisoning, thus we
7197 // do not have to unpoison `root_reg` here.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007198 }
7199}
7200
7201void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
7202 Location ref,
7203 Register obj,
7204 uint32_t offset,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007205 bool needs_null_check) {
7206 DCHECK(kEmitCompilerReadBarrier);
7207 DCHECK(kUseBakerReadBarrier);
7208
7209 // /* HeapReference<Object> */ ref = *(obj + offset)
7210 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007211 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007212}
7213
7214void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
7215 Location ref,
7216 Register obj,
7217 uint32_t data_offset,
7218 Location index,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007219 bool needs_null_check) {
7220 DCHECK(kEmitCompilerReadBarrier);
7221 DCHECK(kUseBakerReadBarrier);
7222
Roland Levillain3d312422016-06-23 13:53:42 +01007223 static_assert(
7224 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
7225 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00007226 // /* HeapReference<Object> */ ref =
7227 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007228 Address src = CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007229 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007230}
7231
7232void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
7233 Location ref,
7234 Register obj,
7235 const Address& src,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007236 bool needs_null_check,
7237 bool always_update_field,
7238 Register* temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007239 DCHECK(kEmitCompilerReadBarrier);
7240 DCHECK(kUseBakerReadBarrier);
7241
7242 // In slow path based read barriers, the read barrier call is
7243 // inserted after the original load. However, in fast path based
7244 // Baker's read barriers, we need to perform the load of
7245 // mirror::Object::monitor_ *before* the original reference load.
7246 // This load-load ordering is required by the read barrier.
7247 // The fast path/slow path (for Baker's algorithm) should look like:
7248 //
7249 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7250 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7251 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007252 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain7c1559a2015-12-15 10:55:36 +00007253 // if (is_gray) {
7254 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7255 // }
7256 //
7257 // Note: the original implementation in ReadBarrier::Barrier is
7258 // slightly more complex as:
7259 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007260 // the high-bits of rb_state, which are expected to be all zeroes
7261 // (we use CodeGeneratorX86::GenerateMemoryBarrier instead here,
7262 // which is a no-op thanks to the x86 memory model);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007263 // - it performs additional checks that we do not do here for
7264 // performance reasons.
7265
7266 Register ref_reg = ref.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +00007267 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7268
Vladimir Marko953437b2016-08-24 08:30:46 +00007269 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007270 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
7271 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko953437b2016-08-24 08:30:46 +00007272 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
7273 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
7274 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
7275
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007276 // if (rb_state == ReadBarrier::GrayState())
Vladimir Marko953437b2016-08-24 08:30:46 +00007277 // ref = ReadBarrier::Mark(ref);
7278 // At this point, just do the "if" and make sure that flags are preserved until the branch.
7279 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain7c1559a2015-12-15 10:55:36 +00007280 if (needs_null_check) {
7281 MaybeRecordImplicitNullCheck(instruction);
7282 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007283
7284 // Load fence to prevent load-load reordering.
7285 // Note that this is a no-op, thanks to the x86 memory model.
7286 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
7287
7288 // The actual reference load.
7289 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00007290 __ movl(ref_reg, src); // Flags are unaffected.
7291
7292 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
7293 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007294 SlowPathCode* slow_path;
7295 if (always_update_field) {
7296 DCHECK(temp != nullptr);
Vladimir Marko174b2e22017-10-12 13:34:49 +01007297 slow_path = new (GetScopedAllocator()) ReadBarrierMarkAndUpdateFieldSlowPathX86(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007298 instruction, ref, obj, src, /* unpoison_ref_before_marking */ true, *temp);
7299 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01007300 slow_path = new (GetScopedAllocator()) ReadBarrierMarkSlowPathX86(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007301 instruction, ref, /* unpoison_ref_before_marking */ true);
7302 }
Vladimir Marko953437b2016-08-24 08:30:46 +00007303 AddSlowPath(slow_path);
7304
7305 // We have done the "if" of the gray bit check above, now branch based on the flags.
7306 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain7c1559a2015-12-15 10:55:36 +00007307
7308 // Object* ref = ref_addr->AsMirrorPtr()
7309 __ MaybeUnpoisonHeapReference(ref_reg);
7310
Roland Levillain7c1559a2015-12-15 10:55:36 +00007311 __ Bind(slow_path->GetExitLabel());
7312}
7313
7314void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
7315 Location out,
7316 Location ref,
7317 Location obj,
7318 uint32_t offset,
7319 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007320 DCHECK(kEmitCompilerReadBarrier);
7321
Roland Levillain7c1559a2015-12-15 10:55:36 +00007322 // Insert a slow path based read barrier *after* the reference load.
7323 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007324 // If heap poisoning is enabled, the unpoisoning of the loaded
7325 // reference will be carried out by the runtime within the slow
7326 // path.
7327 //
7328 // Note that `ref` currently does not get unpoisoned (when heap
7329 // poisoning is enabled), which is alright as the `ref` argument is
7330 // not used by the artReadBarrierSlow entry point.
7331 //
7332 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01007333 SlowPathCode* slow_path = new (GetScopedAllocator())
Roland Levillain0d5a2812015-11-13 10:07:31 +00007334 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
7335 AddSlowPath(slow_path);
7336
Roland Levillain0d5a2812015-11-13 10:07:31 +00007337 __ jmp(slow_path->GetEntryLabel());
7338 __ Bind(slow_path->GetExitLabel());
7339}
7340
Roland Levillain7c1559a2015-12-15 10:55:36 +00007341void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7342 Location out,
7343 Location ref,
7344 Location obj,
7345 uint32_t offset,
7346 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007347 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007348 // Baker's read barriers shall be handled by the fast path
7349 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
7350 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007351 // If heap poisoning is enabled, unpoisoning will be taken care of
7352 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007353 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007354 } else if (kPoisonHeapReferences) {
7355 __ UnpoisonHeapReference(out.AsRegister<Register>());
7356 }
7357}
7358
Roland Levillain7c1559a2015-12-15 10:55:36 +00007359void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7360 Location out,
7361 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007362 DCHECK(kEmitCompilerReadBarrier);
7363
Roland Levillain7c1559a2015-12-15 10:55:36 +00007364 // Insert a slow path based read barrier *after* the GC root load.
7365 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007366 // Note that GC roots are not affected by heap poisoning, so we do
7367 // not need to do anything special for this here.
7368 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007369 new (GetScopedAllocator()) ReadBarrierForRootSlowPathX86(instruction, out, root);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007370 AddSlowPath(slow_path);
7371
Roland Levillain0d5a2812015-11-13 10:07:31 +00007372 __ jmp(slow_path->GetEntryLabel());
7373 __ Bind(slow_path->GetExitLabel());
7374}
7375
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007376void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007377 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007378 LOG(FATAL) << "Unreachable";
7379}
7380
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007381void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007382 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007383 LOG(FATAL) << "Unreachable";
7384}
7385
Mark Mendellfe57faa2015-09-18 09:26:15 -04007386// Simple implementation of packed switch - generate cascaded compare/jumps.
7387void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7388 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007389 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendellfe57faa2015-09-18 09:26:15 -04007390 locations->SetInAt(0, Location::RequiresRegister());
7391}
7392
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007393void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
7394 int32_t lower_bound,
7395 uint32_t num_entries,
7396 HBasicBlock* switch_block,
7397 HBasicBlock* default_block) {
7398 // Figure out the correct compare values and jump conditions.
7399 // Handle the first compare/branch as a special case because it might
7400 // jump to the default case.
7401 DCHECK_GT(num_entries, 2u);
7402 Condition first_condition;
7403 uint32_t index;
7404 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
7405 if (lower_bound != 0) {
7406 first_condition = kLess;
7407 __ cmpl(value_reg, Immediate(lower_bound));
7408 __ j(first_condition, codegen_->GetLabelOf(default_block));
7409 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007410
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007411 index = 1;
7412 } else {
7413 // Handle all the compare/jumps below.
7414 first_condition = kBelow;
7415 index = 0;
7416 }
7417
7418 // Handle the rest of the compare/jumps.
7419 for (; index + 1 < num_entries; index += 2) {
7420 int32_t compare_to_value = lower_bound + index + 1;
7421 __ cmpl(value_reg, Immediate(compare_to_value));
7422 // Jump to successors[index] if value < case_value[index].
7423 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
7424 // Jump to successors[index + 1] if value == case_value[index + 1].
7425 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
7426 }
7427
7428 if (index != num_entries) {
7429 // There are an odd number of entries. Handle the last one.
7430 DCHECK_EQ(index + 1, num_entries);
7431 __ cmpl(value_reg, Immediate(lower_bound + index));
7432 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007433 }
7434
7435 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007436 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
7437 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007438 }
7439}
7440
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007441void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7442 int32_t lower_bound = switch_instr->GetStartValue();
7443 uint32_t num_entries = switch_instr->GetNumEntries();
7444 LocationSummary* locations = switch_instr->GetLocations();
7445 Register value_reg = locations->InAt(0).AsRegister<Register>();
7446
7447 GenPackedSwitchWithCompares(value_reg,
7448 lower_bound,
7449 num_entries,
7450 switch_instr->GetBlock(),
7451 switch_instr->GetDefaultBlock());
7452}
7453
Mark Mendell805b3b52015-09-18 14:10:29 -04007454void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7455 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007456 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendell805b3b52015-09-18 14:10:29 -04007457 locations->SetInAt(0, Location::RequiresRegister());
7458
7459 // Constant area pointer.
7460 locations->SetInAt(1, Location::RequiresRegister());
7461
7462 // And the temporary we need.
7463 locations->AddTemp(Location::RequiresRegister());
7464}
7465
7466void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7467 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007468 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04007469 LocationSummary* locations = switch_instr->GetLocations();
7470 Register value_reg = locations->InAt(0).AsRegister<Register>();
7471 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7472
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007473 if (num_entries <= kPackedSwitchJumpTableThreshold) {
7474 GenPackedSwitchWithCompares(value_reg,
7475 lower_bound,
7476 num_entries,
7477 switch_instr->GetBlock(),
7478 default_block);
7479 return;
7480 }
7481
Mark Mendell805b3b52015-09-18 14:10:29 -04007482 // Optimizing has a jump area.
7483 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7484 Register constant_area = locations->InAt(1).AsRegister<Register>();
7485
7486 // Remove the bias, if needed.
7487 if (lower_bound != 0) {
7488 __ leal(temp_reg, Address(value_reg, -lower_bound));
7489 value_reg = temp_reg;
7490 }
7491
7492 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007493 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04007494 __ cmpl(value_reg, Immediate(num_entries - 1));
7495 __ j(kAbove, codegen_->GetLabelOf(default_block));
7496
7497 // We are in the range of the table.
7498 // Load (target-constant_area) from the jump table, indexing by the value.
7499 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
7500
7501 // Compute the actual target address by adding in constant_area.
7502 __ addl(temp_reg, constant_area);
7503
7504 // And jump.
7505 __ jmp(temp_reg);
7506}
7507
Mark Mendell0616ae02015-04-17 12:49:27 -04007508void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
7509 HX86ComputeBaseMethodAddress* insn) {
7510 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007511 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Mark Mendell0616ae02015-04-17 12:49:27 -04007512 locations->SetOut(Location::RequiresRegister());
7513}
7514
7515void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
7516 HX86ComputeBaseMethodAddress* insn) {
7517 LocationSummary* locations = insn->GetLocations();
7518 Register reg = locations->Out().AsRegister<Register>();
7519
7520 // Generate call to next instruction.
7521 Label next_instruction;
7522 __ call(&next_instruction);
7523 __ Bind(&next_instruction);
7524
7525 // Remember this offset for later use with constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007526 codegen_->AddMethodAddressOffset(insn, GetAssembler()->CodeSize());
Mark Mendell0616ae02015-04-17 12:49:27 -04007527
7528 // Grab the return address off the stack.
7529 __ popl(reg);
7530}
7531
7532void LocationsBuilderX86::VisitX86LoadFromConstantTable(
7533 HX86LoadFromConstantTable* insn) {
7534 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007535 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Mark Mendell0616ae02015-04-17 12:49:27 -04007536
7537 locations->SetInAt(0, Location::RequiresRegister());
7538 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
7539
7540 // If we don't need to be materialized, we only need the inputs to be set.
David Brazdilb3e773e2016-01-26 11:28:37 +00007541 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007542 return;
7543 }
7544
7545 switch (insn->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007546 case DataType::Type::kFloat32:
7547 case DataType::Type::kFloat64:
Mark Mendell0616ae02015-04-17 12:49:27 -04007548 locations->SetOut(Location::RequiresFpuRegister());
7549 break;
7550
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007551 case DataType::Type::kInt32:
Mark Mendell0616ae02015-04-17 12:49:27 -04007552 locations->SetOut(Location::RequiresRegister());
7553 break;
7554
7555 default:
7556 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7557 }
7558}
7559
7560void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
David Brazdilb3e773e2016-01-26 11:28:37 +00007561 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007562 return;
7563 }
7564
7565 LocationSummary* locations = insn->GetLocations();
7566 Location out = locations->Out();
7567 Register const_area = locations->InAt(0).AsRegister<Register>();
7568 HConstant *value = insn->GetConstant();
7569
7570 switch (insn->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007571 case DataType::Type::kFloat32:
Mark Mendell0616ae02015-04-17 12:49:27 -04007572 __ movss(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007573 codegen_->LiteralFloatAddress(
7574 value->AsFloatConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04007575 break;
7576
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007577 case DataType::Type::kFloat64:
Mark Mendell0616ae02015-04-17 12:49:27 -04007578 __ movsd(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007579 codegen_->LiteralDoubleAddress(
7580 value->AsDoubleConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04007581 break;
7582
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007583 case DataType::Type::kInt32:
Mark Mendell0616ae02015-04-17 12:49:27 -04007584 __ movl(out.AsRegister<Register>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007585 codegen_->LiteralInt32Address(
7586 value->AsIntConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04007587 break;
7588
7589 default:
7590 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7591 }
7592}
7593
Mark Mendell0616ae02015-04-17 12:49:27 -04007594/**
7595 * Class to handle late fixup of offsets into constant area.
7596 */
Vladimir Marko5233f932015-09-29 19:01:15 +01007597class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04007598 public:
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007599 RIPFixup(CodeGeneratorX86& codegen,
7600 HX86ComputeBaseMethodAddress* base_method_address,
7601 size_t offset)
7602 : codegen_(&codegen),
7603 base_method_address_(base_method_address),
7604 offset_into_constant_area_(offset) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04007605
7606 protected:
7607 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
7608
7609 CodeGeneratorX86* codegen_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007610 HX86ComputeBaseMethodAddress* base_method_address_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007611
7612 private:
7613 void Process(const MemoryRegion& region, int pos) OVERRIDE {
7614 // Patch the correct offset for the instruction. The place to patch is the
7615 // last 4 bytes of the instruction.
7616 // The value to patch is the distance from the offset in the constant area
7617 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04007618 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007619 int32_t relative_position =
7620 constant_offset - codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell0616ae02015-04-17 12:49:27 -04007621
7622 // Patch in the right value.
7623 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
7624 }
7625
Mark Mendell0616ae02015-04-17 12:49:27 -04007626 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04007627 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007628};
7629
Mark Mendell805b3b52015-09-18 14:10:29 -04007630/**
7631 * Class to handle late fixup of offsets to a jump table that will be created in the
7632 * constant area.
7633 */
7634class JumpTableRIPFixup : public RIPFixup {
7635 public:
7636 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007637 : RIPFixup(codegen, switch_instr->GetBaseMethodAddress(), static_cast<size_t>(-1)),
7638 switch_instr_(switch_instr) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04007639
7640 void CreateJumpTable() {
7641 X86Assembler* assembler = codegen_->GetAssembler();
7642
7643 // Ensure that the reference to the jump table has the correct offset.
7644 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
7645 SetOffset(offset_in_constant_table);
7646
7647 // The label values in the jump table are computed relative to the
7648 // instruction addressing the constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007649 const int32_t relative_offset = codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell805b3b52015-09-18 14:10:29 -04007650
7651 // Populate the jump table with the correct values for the jump table.
7652 int32_t num_entries = switch_instr_->GetNumEntries();
7653 HBasicBlock* block = switch_instr_->GetBlock();
7654 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
7655 // The value that we want is the target offset - the position of the table.
7656 for (int32_t i = 0; i < num_entries; i++) {
7657 HBasicBlock* b = successors[i];
7658 Label* l = codegen_->GetLabelOf(b);
7659 DCHECK(l->IsBound());
7660 int32_t offset_to_block = l->Position() - relative_offset;
7661 assembler->AppendInt32(offset_to_block);
7662 }
7663 }
7664
7665 private:
7666 const HX86PackedSwitch* switch_instr_;
7667};
7668
7669void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
7670 // Generate the constant area if needed.
7671 X86Assembler* assembler = GetAssembler();
7672 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
7673 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
7674 // byte values.
7675 assembler->Align(4, 0);
7676 constant_area_start_ = assembler->CodeSize();
7677
7678 // Populate any jump tables.
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007679 for (JumpTableRIPFixup* jump_table : fixups_to_jump_tables_) {
Mark Mendell805b3b52015-09-18 14:10:29 -04007680 jump_table->CreateJumpTable();
7681 }
7682
7683 // And now add the constant area to the generated code.
7684 assembler->AddConstantArea();
7685 }
7686
7687 // And finish up.
7688 CodeGenerator::Finalize(allocator);
7689}
7690
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007691Address CodeGeneratorX86::LiteralDoubleAddress(double v,
7692 HX86ComputeBaseMethodAddress* method_base,
7693 Register reg) {
7694 AssemblerFixup* fixup =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007695 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddDouble(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04007696 return Address(reg, kDummy32BitOffset, fixup);
7697}
7698
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007699Address CodeGeneratorX86::LiteralFloatAddress(float v,
7700 HX86ComputeBaseMethodAddress* method_base,
7701 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007702 AssemblerFixup* fixup =
7703 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddFloat(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04007704 return Address(reg, kDummy32BitOffset, fixup);
7705}
7706
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007707Address CodeGeneratorX86::LiteralInt32Address(int32_t v,
7708 HX86ComputeBaseMethodAddress* method_base,
7709 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007710 AssemblerFixup* fixup =
7711 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddInt32(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04007712 return Address(reg, kDummy32BitOffset, fixup);
7713}
7714
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007715Address CodeGeneratorX86::LiteralInt64Address(int64_t v,
7716 HX86ComputeBaseMethodAddress* method_base,
7717 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007718 AssemblerFixup* fixup =
7719 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddInt64(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04007720 return Address(reg, kDummy32BitOffset, fixup);
7721}
7722
Aart Bika19616e2016-02-01 18:57:58 -08007723void CodeGeneratorX86::Load32BitValue(Register dest, int32_t value) {
7724 if (value == 0) {
7725 __ xorl(dest, dest);
7726 } else {
7727 __ movl(dest, Immediate(value));
7728 }
7729}
7730
7731void CodeGeneratorX86::Compare32BitValue(Register dest, int32_t value) {
7732 if (value == 0) {
7733 __ testl(dest, dest);
7734 } else {
7735 __ cmpl(dest, Immediate(value));
7736 }
7737}
7738
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007739void CodeGeneratorX86::GenerateIntCompare(Location lhs, Location rhs) {
7740 Register lhs_reg = lhs.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07007741 GenerateIntCompare(lhs_reg, rhs);
7742}
7743
7744void CodeGeneratorX86::GenerateIntCompare(Register lhs, Location rhs) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007745 if (rhs.IsConstant()) {
7746 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
jessicahandojo4877b792016-09-08 19:49:13 -07007747 Compare32BitValue(lhs, value);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007748 } else if (rhs.IsStackSlot()) {
jessicahandojo4877b792016-09-08 19:49:13 -07007749 __ cmpl(lhs, Address(ESP, rhs.GetStackIndex()));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007750 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07007751 __ cmpl(lhs, rhs.AsRegister<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007752 }
7753}
7754
7755Address CodeGeneratorX86::ArrayAddress(Register obj,
7756 Location index,
7757 ScaleFactor scale,
7758 uint32_t data_offset) {
7759 return index.IsConstant() ?
7760 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << scale) + data_offset) :
7761 Address(obj, index.AsRegister<Register>(), scale, data_offset);
7762}
7763
Mark Mendell805b3b52015-09-18 14:10:29 -04007764Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
7765 Register reg,
7766 Register value) {
7767 // Create a fixup to be used to create and address the jump table.
7768 JumpTableRIPFixup* table_fixup =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007769 new (GetGraph()->GetAllocator()) JumpTableRIPFixup(*this, switch_instr);
Mark Mendell805b3b52015-09-18 14:10:29 -04007770
7771 // We have to populate the jump tables.
7772 fixups_to_jump_tables_.push_back(table_fixup);
7773
7774 // We want a scaled address, as we are extracting the correct offset from the table.
7775 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
7776}
7777
Andreas Gampe85b62f22015-09-09 13:15:38 -07007778// TODO: target as memory.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007779void CodeGeneratorX86::MoveFromReturnRegister(Location target, DataType::Type type) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07007780 if (!target.IsValid()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007781 DCHECK_EQ(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007782 return;
7783 }
7784
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007785 DCHECK_NE(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007786
7787 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
7788 if (target.Equals(return_loc)) {
7789 return;
7790 }
7791
7792 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7793 // with the else branch.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007794 if (type == DataType::Type::kInt64) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007795 HParallelMove parallel_move(GetGraph()->GetAllocator());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007796 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), DataType::Type::kInt32, nullptr);
7797 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), DataType::Type::kInt32, nullptr);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007798 GetMoveResolver()->EmitNativeCode(&parallel_move);
7799 } else {
7800 // Let the parallel move resolver take care of all of this.
Vladimir Markoca6fff82017-10-03 14:49:14 +01007801 HParallelMove parallel_move(GetGraph()->GetAllocator());
Andreas Gampe85b62f22015-09-09 13:15:38 -07007802 parallel_move.AddMove(return_loc, target, type, nullptr);
7803 GetMoveResolver()->EmitNativeCode(&parallel_move);
7804 }
7805}
7806
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007807void CodeGeneratorX86::PatchJitRootUse(uint8_t* code,
7808 const uint8_t* roots_data,
7809 const PatchInfo<Label>& info,
7810 uint64_t index_in_table) const {
7811 uint32_t code_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
7812 uintptr_t address =
7813 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
7814 typedef __attribute__((__aligned__(1))) uint32_t unaligned_uint32_t;
7815 reinterpret_cast<unaligned_uint32_t*>(code + code_offset)[0] =
7816 dchecked_integral_cast<uint32_t>(address);
7817}
7818
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007819void CodeGeneratorX86::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
7820 for (const PatchInfo<Label>& info : jit_string_patches_) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01007821 StringReference string_reference(&info.dex_file, dex::StringIndex(info.index));
7822 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007823 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007824 }
7825
7826 for (const PatchInfo<Label>& info : jit_class_patches_) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01007827 TypeReference type_reference(&info.dex_file, dex::TypeIndex(info.index));
7828 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007829 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007830 }
7831}
7832
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007833void LocationsBuilderX86::VisitIntermediateAddress(HIntermediateAddress* instruction
7834 ATTRIBUTE_UNUSED) {
7835 LOG(FATAL) << "Unreachable";
7836}
7837
7838void InstructionCodeGeneratorX86::VisitIntermediateAddress(HIntermediateAddress* instruction
7839 ATTRIBUTE_UNUSED) {
7840 LOG(FATAL) << "Unreachable";
7841}
7842
Roland Levillain4d027112015-07-01 15:41:14 +01007843#undef __
7844
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007845} // namespace x86
7846} // namespace art