blob: 2e8170ecc4f85eced66b230b9cfa782f97f827c0 [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
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000320 if (!is_fatal_) {
321 SaveLiveRegisters(codegen, locations);
322 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000323
324 // We're moving two locations to locations that could overlap, so we need a parallel
325 // move resolver.
326 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800327 x86_codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800328 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100329 DataType::Type::kReference,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800330 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800331 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100332 DataType::Type::kReference);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000333 if (instruction_->IsInstanceOf()) {
Serban Constantinescuba45db02016-07-12 22:53:02 +0100334 x86_codegen->InvokeRuntime(kQuickInstanceofNonTrivial,
Alexandre Rames8158f282015-08-07 10:26:17 +0100335 instruction_,
336 instruction_->GetDexPc(),
337 this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800338 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000339 } else {
340 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800341 x86_codegen->InvokeRuntime(kQuickCheckInstanceOf,
342 instruction_,
343 instruction_->GetDexPc(),
344 this);
345 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000346 }
347
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000348 if (!is_fatal_) {
349 if (instruction_->IsInstanceOf()) {
350 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
351 }
352 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray75374372015-09-17 17:12:19 +0000353
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000354 __ jmp(GetExitLabel());
355 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000356 }
357
Alexandre Rames9931f312015-06-19 14:47:01 +0100358 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathX86"; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000359 bool IsFatal() const OVERRIDE { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100360
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000361 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000362 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000363
364 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
365};
366
Andreas Gampe85b62f22015-09-09 13:15:38 -0700367class DeoptimizationSlowPathX86 : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700368 public:
Aart Bik42249c32016-01-07 15:33:50 -0800369 explicit DeoptimizationSlowPathX86(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000370 : SlowPathCode(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700371
372 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +0100373 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700374 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100375 LocationSummary* locations = instruction_->GetLocations();
376 SaveLiveRegisters(codegen, locations);
377 InvokeRuntimeCallingConvention calling_convention;
378 x86_codegen->Load32BitValue(
379 calling_convention.GetRegisterAt(0),
380 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescuba45db02016-07-12 22:53:02 +0100381 x86_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100382 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700383 }
384
Alexandre Rames9931f312015-06-19 14:47:01 +0100385 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86"; }
386
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700387 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700388 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86);
389};
390
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100391class ArraySetSlowPathX86 : public SlowPathCode {
392 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000393 explicit ArraySetSlowPathX86(HInstruction* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100394
395 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
396 LocationSummary* locations = instruction_->GetLocations();
397 __ Bind(GetEntryLabel());
398 SaveLiveRegisters(codegen, locations);
399
400 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100401 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100402 parallel_move.AddMove(
403 locations->InAt(0),
404 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100405 DataType::Type::kReference,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100406 nullptr);
407 parallel_move.AddMove(
408 locations->InAt(1),
409 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100410 DataType::Type::kInt32,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100411 nullptr);
412 parallel_move.AddMove(
413 locations->InAt(2),
414 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100415 DataType::Type::kReference,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100416 nullptr);
417 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
418
419 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100420 x86_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000421 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100422 RestoreLiveRegisters(codegen, locations);
423 __ jmp(GetExitLabel());
424 }
425
426 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathX86"; }
427
428 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100429 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathX86);
430};
431
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100432// Slow path marking an object reference `ref` during a read
433// barrier. The field `obj.field` in the object `obj` holding this
434// reference does not get updated by this slow path after marking (see
435// ReadBarrierMarkAndUpdateFieldSlowPathX86 below for that).
436//
437// This means that after the execution of this slow path, `ref` will
438// always be up-to-date, but `obj.field` may not; i.e., after the
439// flip, `ref` will be a to-space reference, but `obj.field` will
440// probably still be a from-space reference (unless it gets updated by
441// another thread, or if another thread installed another object
442// reference (different from `ref`) in `obj.field`).
Roland Levillain7c1559a2015-12-15 10:55:36 +0000443class ReadBarrierMarkSlowPathX86 : public SlowPathCode {
444 public:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100445 ReadBarrierMarkSlowPathX86(HInstruction* instruction,
446 Location ref,
447 bool unpoison_ref_before_marking)
448 : SlowPathCode(instruction),
449 ref_(ref),
450 unpoison_ref_before_marking_(unpoison_ref_before_marking) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000451 DCHECK(kEmitCompilerReadBarrier);
452 }
453
454 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathX86"; }
455
456 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
457 LocationSummary* locations = instruction_->GetLocations();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100458 Register ref_reg = ref_.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +0000459 DCHECK(locations->CanCall());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100460 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
Roland Levillain7c1559a2015-12-15 10:55:36 +0000461 DCHECK(instruction_->IsInstanceFieldGet() ||
462 instruction_->IsStaticFieldGet() ||
463 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100464 instruction_->IsArraySet() ||
Roland Levillain7c1559a2015-12-15 10:55:36 +0000465 instruction_->IsLoadClass() ||
466 instruction_->IsLoadString() ||
467 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100468 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100469 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
470 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain7c1559a2015-12-15 10:55:36 +0000471 << "Unexpected instruction in read barrier marking slow path: "
472 << instruction_->DebugName();
473
474 __ Bind(GetEntryLabel());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100475 if (unpoison_ref_before_marking_) {
Vladimir Marko953437b2016-08-24 08:30:46 +0000476 // Object* ref = ref_addr->AsMirrorPtr()
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100477 __ MaybeUnpoisonHeapReference(ref_reg);
Vladimir Marko953437b2016-08-24 08:30:46 +0000478 }
Roland Levillain4359e612016-07-20 11:32:19 +0100479 // No need to save live registers; it's taken care of by the
480 // entrypoint. Also, there is no need to update the stack mask,
481 // as this runtime call will not trigger a garbage collection.
Roland Levillain7c1559a2015-12-15 10:55:36 +0000482 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100483 DCHECK_NE(ref_reg, ESP);
484 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCpuRegisters) << ref_reg;
Roland Levillain02b75802016-07-13 11:54:35 +0100485 // "Compact" slow path, saving two moves.
486 //
487 // Instead of using the standard runtime calling convention (input
488 // and output in EAX):
489 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100490 // EAX <- ref
Roland Levillain02b75802016-07-13 11:54:35 +0100491 // EAX <- ReadBarrierMark(EAX)
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100492 // ref <- EAX
Roland Levillain02b75802016-07-13 11:54:35 +0100493 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100494 // we just use rX (the register containing `ref`) as input and output
Roland Levillain02b75802016-07-13 11:54:35 +0100495 // of a dedicated entrypoint:
496 //
497 // rX <- ReadBarrierMarkRegX(rX)
498 //
Roland Levillain97c46462017-05-11 14:04:03 +0100499 int32_t entry_point_offset = Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(ref_reg);
Roland Levillaindec8f632016-07-22 17:10:06 +0100500 // This runtime call does not require a stack map.
501 x86_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillain7c1559a2015-12-15 10:55:36 +0000502 __ jmp(GetExitLabel());
503 }
504
505 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100506 // The location (register) of the marked object reference.
507 const Location ref_;
508 // Should the reference in `ref_` be unpoisoned prior to marking it?
509 const bool unpoison_ref_before_marking_;
Roland Levillain7c1559a2015-12-15 10:55:36 +0000510
511 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86);
512};
513
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100514// Slow path marking an object reference `ref` during a read barrier,
515// and if needed, atomically updating the field `obj.field` in the
516// object `obj` holding this reference after marking (contrary to
517// ReadBarrierMarkSlowPathX86 above, which never tries to update
518// `obj.field`).
519//
520// This means that after the execution of this slow path, both `ref`
521// and `obj.field` will be up-to-date; i.e., after the flip, both will
522// hold the same to-space reference (unless another thread installed
523// another object reference (different from `ref`) in `obj.field`).
524class ReadBarrierMarkAndUpdateFieldSlowPathX86 : public SlowPathCode {
525 public:
526 ReadBarrierMarkAndUpdateFieldSlowPathX86(HInstruction* instruction,
527 Location ref,
528 Register obj,
529 const Address& field_addr,
530 bool unpoison_ref_before_marking,
531 Register temp)
532 : SlowPathCode(instruction),
533 ref_(ref),
534 obj_(obj),
535 field_addr_(field_addr),
536 unpoison_ref_before_marking_(unpoison_ref_before_marking),
537 temp_(temp) {
538 DCHECK(kEmitCompilerReadBarrier);
539 }
540
541 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkAndUpdateFieldSlowPathX86"; }
542
543 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
544 LocationSummary* locations = instruction_->GetLocations();
545 Register ref_reg = ref_.AsRegister<Register>();
546 DCHECK(locations->CanCall());
547 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
548 // This slow path is only used by the UnsafeCASObject intrinsic.
549 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
550 << "Unexpected instruction in read barrier marking and field updating slow path: "
551 << instruction_->DebugName();
552 DCHECK(instruction_->GetLocations()->Intrinsified());
553 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
554
555 __ Bind(GetEntryLabel());
556 if (unpoison_ref_before_marking_) {
557 // Object* ref = ref_addr->AsMirrorPtr()
558 __ MaybeUnpoisonHeapReference(ref_reg);
559 }
560
561 // Save the old (unpoisoned) reference.
562 __ movl(temp_, ref_reg);
563
564 // No need to save live registers; it's taken care of by the
565 // entrypoint. Also, there is no need to update the stack mask,
566 // as this runtime call will not trigger a garbage collection.
567 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
568 DCHECK_NE(ref_reg, ESP);
569 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCpuRegisters) << ref_reg;
570 // "Compact" slow path, saving two moves.
571 //
572 // Instead of using the standard runtime calling convention (input
573 // and output in EAX):
574 //
575 // EAX <- ref
576 // EAX <- ReadBarrierMark(EAX)
577 // ref <- EAX
578 //
579 // we just use rX (the register containing `ref`) as input and output
580 // of a dedicated entrypoint:
581 //
582 // rX <- ReadBarrierMarkRegX(rX)
583 //
Roland Levillain97c46462017-05-11 14:04:03 +0100584 int32_t entry_point_offset = Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(ref_reg);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100585 // This runtime call does not require a stack map.
586 x86_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
587
588 // If the new reference is different from the old reference,
589 // update the field in the holder (`*field_addr`).
590 //
591 // Note that this field could also hold a different object, if
592 // another thread had concurrently changed it. In that case, the
593 // LOCK CMPXCHGL instruction in the compare-and-set (CAS)
594 // operation below would abort the CAS, leaving the field as-is.
595 NearLabel done;
596 __ cmpl(temp_, ref_reg);
597 __ j(kEqual, &done);
598
599 // Update the the holder's field atomically. This may fail if
600 // mutator updates before us, but it's OK. This is achieved
601 // using a strong compare-and-set (CAS) operation with relaxed
602 // memory synchronization ordering, where the expected value is
603 // the old reference and the desired value is the new reference.
604 // This operation is implemented with a 32-bit LOCK CMPXLCHG
605 // instruction, which requires the expected value (the old
606 // reference) to be in EAX. Save EAX beforehand, and move the
607 // expected value (stored in `temp_`) into EAX.
608 __ pushl(EAX);
609 __ movl(EAX, temp_);
610
611 // Convenience aliases.
612 Register base = obj_;
613 Register expected = EAX;
614 Register value = ref_reg;
615
616 bool base_equals_value = (base == value);
617 if (kPoisonHeapReferences) {
618 if (base_equals_value) {
619 // If `base` and `value` are the same register location, move
620 // `value` to a temporary register. This way, poisoning
621 // `value` won't invalidate `base`.
622 value = temp_;
623 __ movl(value, base);
624 }
625
626 // Check that the register allocator did not assign the location
627 // of `expected` (EAX) to `value` nor to `base`, so that heap
628 // poisoning (when enabled) works as intended below.
629 // - If `value` were equal to `expected`, both references would
630 // be poisoned twice, meaning they would not be poisoned at
631 // all, as heap poisoning uses address negation.
632 // - If `base` were equal to `expected`, poisoning `expected`
633 // would invalidate `base`.
634 DCHECK_NE(value, expected);
635 DCHECK_NE(base, expected);
636
637 __ PoisonHeapReference(expected);
638 __ PoisonHeapReference(value);
639 }
640
641 __ LockCmpxchgl(field_addr_, value);
642
643 // If heap poisoning is enabled, we need to unpoison the values
644 // that were poisoned earlier.
645 if (kPoisonHeapReferences) {
646 if (base_equals_value) {
647 // `value` has been moved to a temporary register, no need
648 // to unpoison it.
649 } else {
650 __ UnpoisonHeapReference(value);
651 }
652 // No need to unpoison `expected` (EAX), as it is be overwritten below.
653 }
654
655 // Restore EAX.
656 __ popl(EAX);
657
658 __ Bind(&done);
659 __ jmp(GetExitLabel());
660 }
661
662 private:
663 // The location (register) of the marked object reference.
664 const Location ref_;
665 // The register containing the object holding the marked object reference field.
666 const Register obj_;
667 // The address of the marked reference field. The base of this address must be `obj_`.
668 const Address field_addr_;
669
670 // Should the reference in `ref_` be unpoisoned prior to marking it?
671 const bool unpoison_ref_before_marking_;
672
673 const Register temp_;
674
675 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathX86);
676};
677
Roland Levillain0d5a2812015-11-13 10:07:31 +0000678// Slow path generating a read barrier for a heap reference.
679class ReadBarrierForHeapReferenceSlowPathX86 : public SlowPathCode {
680 public:
681 ReadBarrierForHeapReferenceSlowPathX86(HInstruction* instruction,
682 Location out,
683 Location ref,
684 Location obj,
685 uint32_t offset,
686 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000687 : SlowPathCode(instruction),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000688 out_(out),
689 ref_(ref),
690 obj_(obj),
691 offset_(offset),
692 index_(index) {
693 DCHECK(kEmitCompilerReadBarrier);
694 // If `obj` is equal to `out` or `ref`, it means the initial object
695 // has been overwritten by (or after) the heap object reference load
696 // to be instrumented, e.g.:
697 //
698 // __ movl(out, Address(out, offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000699 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000700 //
701 // In that case, we have lost the information about the original
702 // object, and the emitted read barrier cannot work properly.
703 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
704 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
705 }
706
707 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
708 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
709 LocationSummary* locations = instruction_->GetLocations();
710 Register reg_out = out_.AsRegister<Register>();
711 DCHECK(locations->CanCall());
712 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain3d312422016-06-23 13:53:42 +0100713 DCHECK(instruction_->IsInstanceFieldGet() ||
714 instruction_->IsStaticFieldGet() ||
715 instruction_->IsArrayGet() ||
716 instruction_->IsInstanceOf() ||
717 instruction_->IsCheckCast() ||
Andreas Gamped9911ee2017-03-27 13:27:24 -0700718 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain7c1559a2015-12-15 10:55:36 +0000719 << "Unexpected instruction in read barrier for heap reference slow path: "
720 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000721
722 __ Bind(GetEntryLabel());
723 SaveLiveRegisters(codegen, locations);
724
725 // We may have to change the index's value, but as `index_` is a
726 // constant member (like other "inputs" of this slow path),
727 // introduce a copy of it, `index`.
728 Location index = index_;
729 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100730 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain0d5a2812015-11-13 10:07:31 +0000731 if (instruction_->IsArrayGet()) {
732 // Compute the actual memory offset and store it in `index`.
733 Register index_reg = index_.AsRegister<Register>();
734 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
735 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
736 // We are about to change the value of `index_reg` (see the
737 // calls to art::x86::X86Assembler::shll and
738 // art::x86::X86Assembler::AddImmediate below), but it has
739 // not been saved by the previous call to
740 // art::SlowPathCode::SaveLiveRegisters, as it is a
741 // callee-save register --
742 // art::SlowPathCode::SaveLiveRegisters does not consider
743 // callee-save registers, as it has been designed with the
744 // assumption that callee-save registers are supposed to be
745 // handled by the called function. So, as a callee-save
746 // register, `index_reg` _would_ eventually be saved onto
747 // the stack, but it would be too late: we would have
748 // changed its value earlier. Therefore, we manually save
749 // it here into another freely available register,
750 // `free_reg`, chosen of course among the caller-save
751 // registers (as a callee-save `free_reg` register would
752 // exhibit the same problem).
753 //
754 // Note we could have requested a temporary register from
755 // the register allocator instead; but we prefer not to, as
756 // this is a slow path, and we know we can find a
757 // caller-save register that is available.
758 Register free_reg = FindAvailableCallerSaveRegister(codegen);
759 __ movl(free_reg, index_reg);
760 index_reg = free_reg;
761 index = Location::RegisterLocation(index_reg);
762 } else {
763 // The initial register stored in `index_` has already been
764 // saved in the call to art::SlowPathCode::SaveLiveRegisters
765 // (as it is not a callee-save register), so we can freely
766 // use it.
767 }
768 // Shifting the index value contained in `index_reg` by the scale
769 // factor (2) cannot overflow in practice, as the runtime is
770 // unable to allocate object arrays with a size larger than
771 // 2^26 - 1 (that is, 2^28 - 4 bytes).
772 __ shll(index_reg, Immediate(TIMES_4));
773 static_assert(
774 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
775 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
776 __ AddImmediate(index_reg, Immediate(offset_));
777 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100778 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
779 // intrinsics, `index_` is not shifted by a scale factor of 2
780 // (as in the case of ArrayGet), as it is actually an offset
781 // to an object field within an object.
782 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000783 DCHECK(instruction_->GetLocations()->Intrinsified());
784 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
785 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
786 << instruction_->AsInvoke()->GetIntrinsic();
787 DCHECK_EQ(offset_, 0U);
788 DCHECK(index_.IsRegisterPair());
789 // UnsafeGet's offset location is a register pair, the low
790 // part contains the correct offset.
791 index = index_.ToLow();
792 }
793 }
794
795 // We're moving two or three locations to locations that could
796 // overlap, so we need a parallel move resolver.
797 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100798 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Roland Levillain0d5a2812015-11-13 10:07:31 +0000799 parallel_move.AddMove(ref_,
800 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100801 DataType::Type::kReference,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000802 nullptr);
803 parallel_move.AddMove(obj_,
804 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100805 DataType::Type::kReference,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000806 nullptr);
807 if (index.IsValid()) {
808 parallel_move.AddMove(index,
809 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100810 DataType::Type::kInt32,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000811 nullptr);
812 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
813 } else {
814 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
815 __ movl(calling_convention.GetRegisterAt(2), Immediate(offset_));
816 }
Serban Constantinescuba45db02016-07-12 22:53:02 +0100817 x86_codegen->InvokeRuntime(kQuickReadBarrierSlow, instruction_, instruction_->GetDexPc(), this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000818 CheckEntrypointTypes<
819 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
820 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
821
822 RestoreLiveRegisters(codegen, locations);
823 __ jmp(GetExitLabel());
824 }
825
826 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathX86"; }
827
828 private:
829 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
830 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
831 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
832 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
833 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
834 return static_cast<Register>(i);
835 }
836 }
837 // We shall never fail to find a free caller-save register, as
838 // there are more than two core caller-save registers on x86
839 // (meaning it is possible to find one which is different from
840 // `ref` and `obj`).
841 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
842 LOG(FATAL) << "Could not find a free caller-save register";
843 UNREACHABLE();
844 }
845
Roland Levillain0d5a2812015-11-13 10:07:31 +0000846 const Location out_;
847 const Location ref_;
848 const Location obj_;
849 const uint32_t offset_;
850 // An additional location containing an index to an array.
851 // Only used for HArrayGet and the UnsafeGetObject &
852 // UnsafeGetObjectVolatile intrinsics.
853 const Location index_;
854
855 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86);
856};
857
858// Slow path generating a read barrier for a GC root.
859class ReadBarrierForRootSlowPathX86 : public SlowPathCode {
860 public:
861 ReadBarrierForRootSlowPathX86(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000862 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000863 DCHECK(kEmitCompilerReadBarrier);
864 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000865
866 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
867 LocationSummary* locations = instruction_->GetLocations();
868 Register reg_out = out_.AsRegister<Register>();
869 DCHECK(locations->CanCall());
870 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000871 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
872 << "Unexpected instruction in read barrier for GC root slow path: "
873 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000874
875 __ Bind(GetEntryLabel());
876 SaveLiveRegisters(codegen, locations);
877
878 InvokeRuntimeCallingConvention calling_convention;
879 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
880 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100881 x86_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000882 instruction_,
883 instruction_->GetDexPc(),
884 this);
885 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
886 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
887
888 RestoreLiveRegisters(codegen, locations);
889 __ jmp(GetExitLabel());
890 }
891
892 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathX86"; }
893
894 private:
Roland Levillain0d5a2812015-11-13 10:07:31 +0000895 const Location out_;
896 const Location root_;
897
898 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86);
899};
900
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100901#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100902// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
903#define __ down_cast<X86Assembler*>(GetAssembler())-> // NOLINT
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100904
Aart Bike9f37602015-10-09 11:15:55 -0700905inline Condition X86Condition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700906 switch (cond) {
907 case kCondEQ: return kEqual;
908 case kCondNE: return kNotEqual;
909 case kCondLT: return kLess;
910 case kCondLE: return kLessEqual;
911 case kCondGT: return kGreater;
912 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700913 case kCondB: return kBelow;
914 case kCondBE: return kBelowEqual;
915 case kCondA: return kAbove;
916 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700917 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100918 LOG(FATAL) << "Unreachable";
919 UNREACHABLE();
920}
921
Aart Bike9f37602015-10-09 11:15:55 -0700922// Maps signed condition to unsigned condition and FP condition to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100923inline Condition X86UnsignedOrFPCondition(IfCondition cond) {
924 switch (cond) {
925 case kCondEQ: return kEqual;
926 case kCondNE: return kNotEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700927 // Signed to unsigned, and FP to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100928 case kCondLT: return kBelow;
929 case kCondLE: return kBelowEqual;
930 case kCondGT: return kAbove;
931 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700932 // Unsigned remain unchanged.
933 case kCondB: return kBelow;
934 case kCondBE: return kBelowEqual;
935 case kCondA: return kAbove;
936 case kCondAE: return kAboveEqual;
Roland Levillain4fa13f62015-07-06 18:11:54 +0100937 }
938 LOG(FATAL) << "Unreachable";
939 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700940}
941
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100942void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100943 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100944}
945
946void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100947 stream << XmmRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100948}
949
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100950size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
951 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
952 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100953}
954
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100955size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
956 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
957 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100958}
959
Mark Mendell7c8d0092015-01-26 11:21:33 -0500960size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Aart Bikb13c65b2017-03-21 20:14:07 -0700961 if (GetGraph()->HasSIMD()) {
Aart Bik5576f372017-03-23 16:17:37 -0700962 __ movups(Address(ESP, stack_index), XmmRegister(reg_id));
Aart Bikb13c65b2017-03-21 20:14:07 -0700963 } else {
964 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
965 }
Mark Mendell7c8d0092015-01-26 11:21:33 -0500966 return GetFloatingPointSpillSlotSize();
967}
968
969size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Aart Bikb13c65b2017-03-21 20:14:07 -0700970 if (GetGraph()->HasSIMD()) {
Aart Bik5576f372017-03-23 16:17:37 -0700971 __ movups(XmmRegister(reg_id), Address(ESP, stack_index));
Aart Bikb13c65b2017-03-21 20:14:07 -0700972 } else {
973 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
974 }
Mark Mendell7c8d0092015-01-26 11:21:33 -0500975 return GetFloatingPointSpillSlotSize();
976}
977
Calin Juravle175dc732015-08-25 15:42:32 +0100978void CodeGeneratorX86::InvokeRuntime(QuickEntrypointEnum entrypoint,
979 HInstruction* instruction,
980 uint32_t dex_pc,
981 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +0100982 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100983 GenerateInvokeRuntime(GetThreadOffset<kX86PointerSize>(entrypoint).Int32Value());
984 if (EntrypointRequiresStackMap(entrypoint)) {
985 RecordPcInfo(instruction, dex_pc, slow_path);
986 }
Alexandre Rames8158f282015-08-07 10:26:17 +0100987}
988
Roland Levillaindec8f632016-07-22 17:10:06 +0100989void CodeGeneratorX86::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
990 HInstruction* instruction,
991 SlowPathCode* slow_path) {
992 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100993 GenerateInvokeRuntime(entry_point_offset);
994}
995
996void CodeGeneratorX86::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +0100997 __ fs()->call(Address::Absolute(entry_point_offset));
998}
999
Mark Mendellfb8d2792015-03-31 22:16:59 -04001000CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +00001001 const X86InstructionSetFeatures& isa_features,
1002 const CompilerOptions& compiler_options,
1003 OptimizingCompilerStats* stats)
Mark Mendell5f874182015-03-04 15:42:45 -05001004 : CodeGenerator(graph,
1005 kNumberOfCpuRegisters,
1006 kNumberOfXmmRegisters,
1007 kNumberOfRegisterPairs,
1008 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1009 arraysize(kCoreCalleeSaves))
1010 | (1 << kFakeReturnRegister),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001011 0,
1012 compiler_options,
1013 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +01001014 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001015 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001016 instruction_visitor_(graph, this),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001017 move_resolver_(graph->GetAllocator(), this),
1018 assembler_(graph->GetAllocator()),
Vladimir Marko58155012015-08-19 12:49:41 +00001019 isa_features_(isa_features),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001020 boot_image_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1021 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1022 boot_image_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1023 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1024 string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1025 string_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1026 jit_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1027 jit_class_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko93205e32016-04-13 11:59:46 +01001028 constant_area_start_(-1),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001029 fixups_to_jump_tables_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001030 method_address_offset_(std::less<uint32_t>(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001031 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001032 // Use a fake return address register to mimic Quick.
1033 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001034}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001035
David Brazdil58282f42016-01-14 12:45:10 +00001036void CodeGeneratorX86::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001037 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001038 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001039}
1040
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001041InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001042 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001043 assembler_(codegen->GetAssembler()),
1044 codegen_(codegen) {}
1045
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001046static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001047 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001048}
1049
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001050void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001051 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001052 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +00001053 bool skip_overflow_check =
1054 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001055 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +00001056
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001057 if (!skip_overflow_check) {
Vladimir Marko33bff252017-11-01 14:35:42 +00001058 size_t reserved_bytes = GetStackOverflowReservedBytes(InstructionSet::kX86);
1059 __ testl(EAX, Address(ESP, -static_cast<int32_t>(reserved_bytes)));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001060 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +01001061 }
1062
Mark Mendell5f874182015-03-04 15:42:45 -05001063 if (HasEmptyFrame()) {
1064 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001065 }
Mark Mendell5f874182015-03-04 15:42:45 -05001066
1067 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
1068 Register reg = kCoreCalleeSaves[i];
1069 if (allocated_registers_.ContainsCoreRegister(reg)) {
1070 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001071 __ cfi().AdjustCFAOffset(kX86WordSize);
1072 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -05001073 }
1074 }
1075
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001076 int adjust = GetFrameSize() - FrameEntrySpillSize();
1077 __ subl(ESP, Immediate(adjust));
1078 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001079 // Save the current method if we need it. Note that we do not
1080 // do this in HCurrentMethod, as the instruction might have been removed
1081 // in the SSA graph.
1082 if (RequiresCurrentMethod()) {
1083 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
1084 }
Nicolas Geoffrayf7893532017-06-15 12:34:36 +01001085
1086 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1087 // Initialize should_deoptimize flag to 0.
1088 __ movl(Address(ESP, GetStackOffsetOfShouldDeoptimizeFlag()), Immediate(0));
1089 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001090}
1091
1092void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001093 __ cfi().RememberState();
1094 if (!HasEmptyFrame()) {
1095 int adjust = GetFrameSize() - FrameEntrySpillSize();
1096 __ addl(ESP, Immediate(adjust));
1097 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -05001098
David Srbeckyc34dc932015-04-12 09:27:43 +01001099 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1100 Register reg = kCoreCalleeSaves[i];
1101 if (allocated_registers_.ContainsCoreRegister(reg)) {
1102 __ popl(reg);
1103 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
1104 __ cfi().Restore(DWARFReg(reg));
1105 }
Mark Mendell5f874182015-03-04 15:42:45 -05001106 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001107 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001108 __ ret();
1109 __ cfi().RestoreState();
1110 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001111}
1112
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001113void CodeGeneratorX86::Bind(HBasicBlock* block) {
1114 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001115}
1116
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001117Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(DataType::Type type) const {
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001118 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001119 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001120 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001121 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001122 case DataType::Type::kInt8:
1123 case DataType::Type::kUint16:
1124 case DataType::Type::kInt16:
1125 case DataType::Type::kInt32:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001126 return Location::RegisterLocation(EAX);
1127
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001128 case DataType::Type::kInt64:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001129 return Location::RegisterPairLocation(EAX, EDX);
1130
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001131 case DataType::Type::kVoid:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001132 return Location::NoLocation();
1133
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001134 case DataType::Type::kFloat64:
1135 case DataType::Type::kFloat32:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001136 return Location::FpuRegisterLocation(XMM0);
1137 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001138
1139 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001140}
1141
1142Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
1143 return Location::RegisterLocation(kMethodRegisterArgument);
1144}
1145
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001146Location InvokeDexCallingConventionVisitorX86::GetNextLocation(DataType::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001147 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001148 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001149 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001150 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001151 case DataType::Type::kInt8:
1152 case DataType::Type::kUint16:
1153 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001154 case DataType::Type::kInt32: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001155 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001156 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001157 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001158 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001159 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001160 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001161 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001162 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001163
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001164 case DataType::Type::kInt64: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001165 uint32_t index = gp_index_;
1166 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001167 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001168 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001169 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
1170 calling_convention.GetRegisterPairAt(index));
1171 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001172 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001173 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1174 }
1175 }
1176
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001177 case DataType::Type::kFloat32: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001178 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001179 stack_index_++;
1180 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1181 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1182 } else {
1183 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1184 }
1185 }
1186
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001187 case DataType::Type::kFloat64: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001188 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001189 stack_index_ += 2;
1190 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1191 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1192 } else {
1193 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001194 }
1195 }
1196
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001197 case DataType::Type::kVoid:
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001198 LOG(FATAL) << "Unexpected parameter type " << type;
1199 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001200 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00001201 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001202}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001203
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001204void CodeGeneratorX86::Move32(Location destination, Location source) {
1205 if (source.Equals(destination)) {
1206 return;
1207 }
1208 if (destination.IsRegister()) {
1209 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001210 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001211 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001212 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001213 } else {
1214 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001215 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001216 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001217 } else if (destination.IsFpuRegister()) {
1218 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001219 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001220 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001221 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001222 } else {
1223 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001224 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001225 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001226 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001227 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001228 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001229 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001230 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001231 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05001232 } else if (source.IsConstant()) {
1233 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001234 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05001235 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001236 } else {
1237 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001238 __ pushl(Address(ESP, source.GetStackIndex()));
1239 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001240 }
1241 }
1242}
1243
1244void CodeGeneratorX86::Move64(Location destination, Location source) {
1245 if (source.Equals(destination)) {
1246 return;
1247 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001248 if (destination.IsRegisterPair()) {
1249 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001250 EmitParallelMoves(
1251 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1252 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001253 DataType::Type::kInt32,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001254 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001255 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001256 DataType::Type::kInt32);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001257 } else if (source.IsFpuRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001258 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
1259 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
1260 __ psrlq(src_reg, Immediate(32));
1261 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001262 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001263 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001264 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001265 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
1266 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001267 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
1268 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001269 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05001270 if (source.IsFpuRegister()) {
1271 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
1272 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001273 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001274 } else if (source.IsRegisterPair()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001275 size_t elem_size = DataType::Size(DataType::Type::kInt32);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001276 // Create stack space for 2 elements.
1277 __ subl(ESP, Immediate(2 * elem_size));
1278 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
1279 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
1280 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
1281 // And remove the temporary stack space we allocated.
1282 __ addl(ESP, Immediate(2 * elem_size));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001283 } else {
1284 LOG(FATAL) << "Unimplemented";
1285 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001286 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001287 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001288 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001289 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001290 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001291 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001292 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001293 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001294 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001295 } else if (source.IsConstant()) {
1296 HConstant* constant = source.GetConstant();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001297 DCHECK(constant->IsLongConstant() || constant->IsDoubleConstant());
1298 int64_t value = GetInt64ValueOf(constant);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001299 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001300 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
1301 Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001302 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001303 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001304 EmitParallelMoves(
1305 Location::StackSlot(source.GetStackIndex()),
1306 Location::StackSlot(destination.GetStackIndex()),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001307 DataType::Type::kInt32,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001308 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001309 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001310 DataType::Type::kInt32);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001311 }
1312 }
1313}
1314
Calin Juravle175dc732015-08-25 15:42:32 +01001315void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
1316 DCHECK(location.IsRegister());
1317 __ movl(location.AsRegister<Register>(), Immediate(value));
1318}
1319
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001320void CodeGeneratorX86::MoveLocation(Location dst, Location src, DataType::Type dst_type) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001321 HParallelMove move(GetGraph()->GetAllocator());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001322 if (dst_type == DataType::Type::kInt64 && !src.IsConstant() && !src.IsFpuRegister()) {
1323 move.AddMove(src.ToLow(), dst.ToLow(), DataType::Type::kInt32, nullptr);
1324 move.AddMove(src.ToHigh(), dst.ToHigh(), DataType::Type::kInt32, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001325 } else {
David Brazdil74eb1b22015-12-14 11:44:01 +00001326 move.AddMove(src, dst, dst_type, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001327 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001328 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001329}
1330
1331void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1332 if (location.IsRegister()) {
1333 locations->AddTemp(location);
1334 } else if (location.IsRegisterPair()) {
1335 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1336 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1337 } else {
1338 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1339 }
1340}
1341
David Brazdilfc6a86a2015-06-26 10:33:45 +00001342void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001343 DCHECK(!successor->IsExitBlock());
1344
1345 HBasicBlock* block = got->GetBlock();
1346 HInstruction* previous = got->GetPrevious();
1347
1348 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001349 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001350 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1351 return;
1352 }
1353
1354 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1355 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1356 }
1357 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001358 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001359 }
1360}
1361
David Brazdilfc6a86a2015-06-26 10:33:45 +00001362void LocationsBuilderX86::VisitGoto(HGoto* got) {
1363 got->SetLocations(nullptr);
1364}
1365
1366void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1367 HandleGoto(got, got->GetSuccessor());
1368}
1369
1370void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1371 try_boundary->SetLocations(nullptr);
1372}
1373
1374void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1375 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1376 if (!successor->IsExitBlock()) {
1377 HandleGoto(try_boundary, successor);
1378 }
1379}
1380
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001381void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001382 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001383}
1384
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001385void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001386}
1387
Mark Mendell152408f2015-12-31 12:28:50 -05001388template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001389void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001390 LabelType* true_label,
1391 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001392 if (cond->IsFPConditionTrueIfNaN()) {
1393 __ j(kUnordered, true_label);
1394 } else if (cond->IsFPConditionFalseIfNaN()) {
1395 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001396 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001397 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001398}
1399
Mark Mendell152408f2015-12-31 12:28:50 -05001400template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001401void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001402 LabelType* true_label,
1403 LabelType* false_label) {
Mark Mendellc4701932015-04-10 13:18:51 -04001404 LocationSummary* locations = cond->GetLocations();
1405 Location left = locations->InAt(0);
1406 Location right = locations->InAt(1);
1407 IfCondition if_cond = cond->GetCondition();
1408
Mark Mendellc4701932015-04-10 13:18:51 -04001409 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001410 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001411 IfCondition true_high_cond = if_cond;
1412 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001413 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001414
1415 // Set the conditions for the test, remembering that == needs to be
1416 // decided using the low words.
1417 switch (if_cond) {
1418 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001419 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001420 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001421 break;
1422 case kCondLT:
1423 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001424 break;
1425 case kCondLE:
1426 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001427 break;
1428 case kCondGT:
1429 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001430 break;
1431 case kCondGE:
1432 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001433 break;
Aart Bike9f37602015-10-09 11:15:55 -07001434 case kCondB:
1435 false_high_cond = kCondA;
1436 break;
1437 case kCondBE:
1438 true_high_cond = kCondB;
1439 break;
1440 case kCondA:
1441 false_high_cond = kCondB;
1442 break;
1443 case kCondAE:
1444 true_high_cond = kCondA;
1445 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001446 }
1447
1448 if (right.IsConstant()) {
1449 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001450 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001451 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001452
Aart Bika19616e2016-02-01 18:57:58 -08001453 codegen_->Compare32BitValue(left_high, val_high);
Mark Mendellc4701932015-04-10 13:18:51 -04001454 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001455 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001456 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001457 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001458 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001459 __ j(X86Condition(true_high_cond), true_label);
1460 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001461 }
1462 // Must be equal high, so compare the lows.
Aart Bika19616e2016-02-01 18:57:58 -08001463 codegen_->Compare32BitValue(left_low, val_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001464 } else if (right.IsRegisterPair()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001465 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001466 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001467
1468 __ cmpl(left_high, right_high);
1469 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001470 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001471 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001472 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001473 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001474 __ j(X86Condition(true_high_cond), true_label);
1475 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001476 }
1477 // Must be equal high, so compare the lows.
1478 __ cmpl(left_low, right_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001479 } else {
1480 DCHECK(right.IsDoubleStackSlot());
1481 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
1482 if (if_cond == kCondNE) {
1483 __ j(X86Condition(true_high_cond), true_label);
1484 } else if (if_cond == kCondEQ) {
1485 __ j(X86Condition(false_high_cond), false_label);
1486 } else {
1487 __ j(X86Condition(true_high_cond), true_label);
1488 __ j(X86Condition(false_high_cond), false_label);
1489 }
1490 // Must be equal high, so compare the lows.
1491 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Mark Mendellc4701932015-04-10 13:18:51 -04001492 }
1493 // The last comparison might be unsigned.
1494 __ j(final_condition, true_label);
1495}
1496
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001497void InstructionCodeGeneratorX86::GenerateFPCompare(Location lhs,
1498 Location rhs,
1499 HInstruction* insn,
1500 bool is_double) {
1501 HX86LoadFromConstantTable* const_area = insn->InputAt(1)->AsX86LoadFromConstantTable();
1502 if (is_double) {
1503 if (rhs.IsFpuRegister()) {
1504 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1505 } else if (const_area != nullptr) {
1506 DCHECK(const_area->IsEmittedAtUseSite());
1507 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(),
1508 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001509 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
1510 const_area->GetBaseMethodAddress(),
1511 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001512 } else {
1513 DCHECK(rhs.IsDoubleStackSlot());
1514 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1515 }
1516 } else {
1517 if (rhs.IsFpuRegister()) {
1518 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1519 } else if (const_area != nullptr) {
1520 DCHECK(const_area->IsEmittedAtUseSite());
1521 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(),
1522 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001523 const_area->GetConstant()->AsFloatConstant()->GetValue(),
1524 const_area->GetBaseMethodAddress(),
1525 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001526 } else {
1527 DCHECK(rhs.IsStackSlot());
1528 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1529 }
1530 }
1531}
1532
Mark Mendell152408f2015-12-31 12:28:50 -05001533template<class LabelType>
David Brazdil0debae72015-11-12 18:37:00 +00001534void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendell152408f2015-12-31 12:28:50 -05001535 LabelType* true_target_in,
1536 LabelType* false_target_in) {
David Brazdil0debae72015-11-12 18:37:00 +00001537 // Generated branching requires both targets to be explicit. If either of the
1538 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Mark Mendell152408f2015-12-31 12:28:50 -05001539 LabelType fallthrough_target;
1540 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1541 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
David Brazdil0debae72015-11-12 18:37:00 +00001542
Mark Mendellc4701932015-04-10 13:18:51 -04001543 LocationSummary* locations = condition->GetLocations();
1544 Location left = locations->InAt(0);
1545 Location right = locations->InAt(1);
1546
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001547 DataType::Type type = condition->InputAt(0)->GetType();
Mark Mendellc4701932015-04-10 13:18:51 -04001548 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001549 case DataType::Type::kInt64:
Mark Mendellc4701932015-04-10 13:18:51 -04001550 GenerateLongComparesAndJumps(condition, true_target, false_target);
1551 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001552 case DataType::Type::kFloat32:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001553 GenerateFPCompare(left, right, condition, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001554 GenerateFPJumps(condition, true_target, false_target);
1555 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001556 case DataType::Type::kFloat64:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001557 GenerateFPCompare(left, right, condition, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001558 GenerateFPJumps(condition, true_target, false_target);
1559 break;
1560 default:
1561 LOG(FATAL) << "Unexpected compare type " << type;
1562 }
1563
David Brazdil0debae72015-11-12 18:37:00 +00001564 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001565 __ jmp(false_target);
1566 }
David Brazdil0debae72015-11-12 18:37:00 +00001567
1568 if (fallthrough_target.IsLinked()) {
1569 __ Bind(&fallthrough_target);
1570 }
Mark Mendellc4701932015-04-10 13:18:51 -04001571}
1572
David Brazdil0debae72015-11-12 18:37:00 +00001573static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1574 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1575 // are set only strictly before `branch`. We can't use the eflags on long/FP
1576 // conditions if they are materialized due to the complex branching.
1577 return cond->IsCondition() &&
1578 cond->GetNext() == branch &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001579 cond->InputAt(0)->GetType() != DataType::Type::kInt64 &&
1580 !DataType::IsFloatingPointType(cond->InputAt(0)->GetType());
David Brazdil0debae72015-11-12 18:37:00 +00001581}
1582
Mark Mendell152408f2015-12-31 12:28:50 -05001583template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001584void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001585 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001586 LabelType* true_target,
1587 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001588 HInstruction* cond = instruction->InputAt(condition_input_index);
1589
1590 if (true_target == nullptr && false_target == nullptr) {
1591 // Nothing to do. The code always falls through.
1592 return;
1593 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001594 // Constant condition, statically compared against "true" (integer value 1).
1595 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001596 if (true_target != nullptr) {
1597 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001598 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001599 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001600 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001601 if (false_target != nullptr) {
1602 __ jmp(false_target);
1603 }
1604 }
1605 return;
1606 }
1607
1608 // The following code generates these patterns:
1609 // (1) true_target == nullptr && false_target != nullptr
1610 // - opposite condition true => branch to false_target
1611 // (2) true_target != nullptr && false_target == nullptr
1612 // - condition true => branch to true_target
1613 // (3) true_target != nullptr && false_target != nullptr
1614 // - condition true => branch to true_target
1615 // - branch to false_target
1616 if (IsBooleanValueOrMaterializedCondition(cond)) {
1617 if (AreEflagsSetFrom(cond, instruction)) {
1618 if (true_target == nullptr) {
1619 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
1620 } else {
1621 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
1622 }
1623 } else {
1624 // Materialized condition, compare against 0.
1625 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1626 if (lhs.IsRegister()) {
1627 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1628 } else {
1629 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1630 }
1631 if (true_target == nullptr) {
1632 __ j(kEqual, false_target);
1633 } else {
1634 __ j(kNotEqual, true_target);
1635 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001636 }
1637 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001638 // Condition has not been materialized, use its inputs as the comparison and
1639 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001640 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001641
1642 // If this is a long or FP comparison that has been folded into
1643 // the HCondition, generate the comparison directly.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001644 DataType::Type type = condition->InputAt(0)->GetType();
1645 if (type == DataType::Type::kInt64 || DataType::IsFloatingPointType(type)) {
David Brazdil0debae72015-11-12 18:37:00 +00001646 GenerateCompareTestAndBranch(condition, true_target, false_target);
1647 return;
1648 }
1649
1650 Location lhs = condition->GetLocations()->InAt(0);
1651 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001652 // LHS is guaranteed to be in a register (see LocationsBuilderX86::HandleCondition).
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001653 codegen_->GenerateIntCompare(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00001654 if (true_target == nullptr) {
1655 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1656 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001657 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001658 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001659 }
David Brazdil0debae72015-11-12 18:37:00 +00001660
1661 // If neither branch falls through (case 3), the conditional branch to `true_target`
1662 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1663 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001664 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001665 }
1666}
1667
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001668void LocationsBuilderX86::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001669 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00001670 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001671 locations->SetInAt(0, Location::Any());
1672 }
1673}
1674
1675void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001676 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1677 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1678 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1679 nullptr : codegen_->GetLabelOf(true_successor);
1680 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1681 nullptr : codegen_->GetLabelOf(false_successor);
1682 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001683}
1684
1685void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001686 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001687 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01001688 InvokeRuntimeCallingConvention calling_convention;
1689 RegisterSet caller_saves = RegisterSet::Empty();
1690 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1691 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00001692 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001693 locations->SetInAt(0, Location::Any());
1694 }
1695}
1696
1697void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001698 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001699 GenerateTestAndBranch<Label>(deoptimize,
1700 /* condition_input_index */ 0,
1701 slow_path->GetEntryLabel(),
1702 /* false_target */ nullptr);
1703}
1704
Mingyao Yang063fc772016-08-02 11:02:54 -07001705void LocationsBuilderX86::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001706 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yang063fc772016-08-02 11:02:54 -07001707 LocationSummary(flag, LocationSummary::kNoCall);
1708 locations->SetOut(Location::RequiresRegister());
1709}
1710
1711void InstructionCodeGeneratorX86::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
1712 __ movl(flag->GetLocations()->Out().AsRegister<Register>(),
1713 Address(ESP, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
1714}
1715
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001716static bool SelectCanUseCMOV(HSelect* select) {
1717 // There are no conditional move instructions for XMMs.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001718 if (DataType::IsFloatingPointType(select->GetType())) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001719 return false;
1720 }
1721
1722 // A FP condition doesn't generate the single CC that we need.
1723 // In 32 bit mode, a long condition doesn't generate a single CC either.
1724 HInstruction* condition = select->GetCondition();
1725 if (condition->IsCondition()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001726 DataType::Type compare_type = condition->InputAt(0)->GetType();
1727 if (compare_type == DataType::Type::kInt64 ||
1728 DataType::IsFloatingPointType(compare_type)) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001729 return false;
1730 }
1731 }
1732
1733 // We can generate a CMOV for this Select.
1734 return true;
1735}
1736
David Brazdil74eb1b22015-12-14 11:44:01 +00001737void LocationsBuilderX86::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001738 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001739 if (DataType::IsFloatingPointType(select->GetType())) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001740 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001741 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001742 } else {
1743 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001744 if (SelectCanUseCMOV(select)) {
1745 if (select->InputAt(1)->IsConstant()) {
1746 // Cmov can't handle a constant value.
1747 locations->SetInAt(1, Location::RequiresRegister());
1748 } else {
1749 locations->SetInAt(1, Location::Any());
1750 }
1751 } else {
1752 locations->SetInAt(1, Location::Any());
1753 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001754 }
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001755 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1756 locations->SetInAt(2, Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00001757 }
1758 locations->SetOut(Location::SameAsFirstInput());
1759}
1760
1761void InstructionCodeGeneratorX86::VisitSelect(HSelect* select) {
1762 LocationSummary* locations = select->GetLocations();
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001763 DCHECK(locations->InAt(0).Equals(locations->Out()));
1764 if (SelectCanUseCMOV(select)) {
1765 // If both the condition and the source types are integer, we can generate
1766 // a CMOV to implement Select.
1767
1768 HInstruction* select_condition = select->GetCondition();
1769 Condition cond = kNotEqual;
1770
1771 // Figure out how to test the 'condition'.
1772 if (select_condition->IsCondition()) {
1773 HCondition* condition = select_condition->AsCondition();
1774 if (!condition->IsEmittedAtUseSite()) {
1775 // This was a previously materialized condition.
1776 // Can we use the existing condition code?
1777 if (AreEflagsSetFrom(condition, select)) {
1778 // Materialization was the previous instruction. Condition codes are right.
1779 cond = X86Condition(condition->GetCondition());
1780 } else {
1781 // No, we have to recreate the condition code.
1782 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1783 __ testl(cond_reg, cond_reg);
1784 }
1785 } else {
1786 // We can't handle FP or long here.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001787 DCHECK_NE(condition->InputAt(0)->GetType(), DataType::Type::kInt64);
1788 DCHECK(!DataType::IsFloatingPointType(condition->InputAt(0)->GetType()));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001789 LocationSummary* cond_locations = condition->GetLocations();
Roland Levillain0b671c02016-08-19 12:02:34 +01001790 codegen_->GenerateIntCompare(cond_locations->InAt(0), cond_locations->InAt(1));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001791 cond = X86Condition(condition->GetCondition());
1792 }
1793 } else {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01001794 // Must be a Boolean condition, which needs to be compared to 0.
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001795 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1796 __ testl(cond_reg, cond_reg);
1797 }
1798
1799 // If the condition is true, overwrite the output, which already contains false.
1800 Location false_loc = locations->InAt(0);
1801 Location true_loc = locations->InAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001802 if (select->GetType() == DataType::Type::kInt64) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001803 // 64 bit conditional move.
1804 Register false_high = false_loc.AsRegisterPairHigh<Register>();
1805 Register false_low = false_loc.AsRegisterPairLow<Register>();
1806 if (true_loc.IsRegisterPair()) {
1807 __ cmovl(cond, false_high, true_loc.AsRegisterPairHigh<Register>());
1808 __ cmovl(cond, false_low, true_loc.AsRegisterPairLow<Register>());
1809 } else {
1810 __ cmovl(cond, false_high, Address(ESP, true_loc.GetHighStackIndex(kX86WordSize)));
1811 __ cmovl(cond, false_low, Address(ESP, true_loc.GetStackIndex()));
1812 }
1813 } else {
1814 // 32 bit conditional move.
1815 Register false_reg = false_loc.AsRegister<Register>();
1816 if (true_loc.IsRegister()) {
1817 __ cmovl(cond, false_reg, true_loc.AsRegister<Register>());
1818 } else {
1819 __ cmovl(cond, false_reg, Address(ESP, true_loc.GetStackIndex()));
1820 }
1821 }
1822 } else {
1823 NearLabel false_target;
1824 GenerateTestAndBranch<NearLabel>(
1825 select, /* condition_input_index */ 2, /* true_target */ nullptr, &false_target);
1826 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1827 __ Bind(&false_target);
1828 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001829}
1830
David Srbecky0cf44932015-12-09 14:09:59 +00001831void LocationsBuilderX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001832 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00001833}
1834
David Srbeckyd28f4a02016-03-14 17:14:24 +00001835void InstructionCodeGeneratorX86::VisitNativeDebugInfo(HNativeDebugInfo*) {
1836 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001837}
1838
1839void CodeGeneratorX86::GenerateNop() {
1840 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001841}
1842
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001843void LocationsBuilderX86::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001844 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01001845 new (GetGraph()->GetAllocator()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001846 // Handle the long/FP comparisons made in instruction simplification.
1847 switch (cond->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001848 case DataType::Type::kInt64: {
Mark Mendellc4701932015-04-10 13:18:51 -04001849 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell8659e842016-02-16 10:41:46 -05001850 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001851 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001852 locations->SetOut(Location::RequiresRegister());
1853 }
1854 break;
1855 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001856 case DataType::Type::kFloat32:
1857 case DataType::Type::kFloat64: {
Mark Mendellc4701932015-04-10 13:18:51 -04001858 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001859 if (cond->InputAt(1)->IsX86LoadFromConstantTable()) {
1860 DCHECK(cond->InputAt(1)->IsEmittedAtUseSite());
1861 } else if (cond->InputAt(1)->IsConstant()) {
1862 locations->SetInAt(1, Location::RequiresFpuRegister());
1863 } else {
1864 locations->SetInAt(1, Location::Any());
1865 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001866 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001867 locations->SetOut(Location::RequiresRegister());
1868 }
1869 break;
1870 }
1871 default:
1872 locations->SetInAt(0, Location::RequiresRegister());
1873 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001874 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001875 // We need a byte register.
1876 locations->SetOut(Location::RegisterLocation(ECX));
1877 }
1878 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001879 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001880}
1881
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001882void InstructionCodeGeneratorX86::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001883 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001884 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001885 }
Mark Mendellc4701932015-04-10 13:18:51 -04001886
1887 LocationSummary* locations = cond->GetLocations();
1888 Location lhs = locations->InAt(0);
1889 Location rhs = locations->InAt(1);
1890 Register reg = locations->Out().AsRegister<Register>();
Mark Mendell152408f2015-12-31 12:28:50 -05001891 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001892
1893 switch (cond->InputAt(0)->GetType()) {
1894 default: {
1895 // Integer case.
1896
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01001897 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04001898 __ xorl(reg, reg);
Roland Levillain0b671c02016-08-19 12:02:34 +01001899 codegen_->GenerateIntCompare(lhs, rhs);
Aart Bike9f37602015-10-09 11:15:55 -07001900 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001901 return;
1902 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001903 case DataType::Type::kInt64:
Mark Mendellc4701932015-04-10 13:18:51 -04001904 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1905 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001906 case DataType::Type::kFloat32:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001907 GenerateFPCompare(lhs, rhs, cond, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001908 GenerateFPJumps(cond, &true_label, &false_label);
1909 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001910 case DataType::Type::kFloat64:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001911 GenerateFPCompare(lhs, rhs, cond, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001912 GenerateFPJumps(cond, &true_label, &false_label);
1913 break;
1914 }
1915
1916 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001917 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001918
Roland Levillain4fa13f62015-07-06 18:11:54 +01001919 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001920 __ Bind(&false_label);
1921 __ xorl(reg, reg);
1922 __ jmp(&done_label);
1923
Roland Levillain4fa13f62015-07-06 18:11:54 +01001924 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001925 __ Bind(&true_label);
1926 __ movl(reg, Immediate(1));
1927 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001928}
1929
1930void LocationsBuilderX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001931 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001932}
1933
1934void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001935 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001936}
1937
1938void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001939 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001940}
1941
1942void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001943 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001944}
1945
1946void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001947 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001948}
1949
1950void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001951 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001952}
1953
1954void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001955 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001956}
1957
1958void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001959 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001960}
1961
1962void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001963 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001964}
1965
1966void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001967 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001968}
1969
1970void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001971 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001972}
1973
1974void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001975 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001976}
1977
Aart Bike9f37602015-10-09 11:15:55 -07001978void LocationsBuilderX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001979 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001980}
1981
1982void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001983 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001984}
1985
1986void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001987 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001988}
1989
1990void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001991 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001992}
1993
1994void LocationsBuilderX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001995 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001996}
1997
1998void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001999 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002000}
2001
2002void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002003 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002004}
2005
2006void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002007 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002008}
2009
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002010void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002011 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002012 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002013 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002014}
2015
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002016void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002017 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002018}
2019
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002020void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
2021 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002022 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002023 locations->SetOut(Location::ConstantLocation(constant));
2024}
2025
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002026void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002027 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002028}
2029
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002030void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002031 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002032 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002033 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002034}
2035
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002036void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002037 // Will be generated at use site.
2038}
2039
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002040void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
2041 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002042 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002043 locations->SetOut(Location::ConstantLocation(constant));
2044}
2045
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002046void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002047 // Will be generated at use site.
2048}
2049
2050void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
2051 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002052 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002053 locations->SetOut(Location::ConstantLocation(constant));
2054}
2055
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002056void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002057 // Will be generated at use site.
2058}
2059
Igor Murashkind01745e2017-04-05 16:40:31 -07002060void LocationsBuilderX86::VisitConstructorFence(HConstructorFence* constructor_fence) {
2061 constructor_fence->SetLocations(nullptr);
2062}
2063
2064void InstructionCodeGeneratorX86::VisitConstructorFence(
2065 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
2066 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
2067}
2068
Calin Juravle27df7582015-04-17 19:12:31 +01002069void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2070 memory_barrier->SetLocations(nullptr);
2071}
2072
2073void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00002074 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002075}
2076
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002077void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002078 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002079}
2080
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002081void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002082 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002083}
2084
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002085void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002086 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002087 new (GetGraph()->GetAllocator()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002088 switch (ret->InputAt(0)->GetType()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002089 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002090 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002091 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002092 case DataType::Type::kInt8:
2093 case DataType::Type::kUint16:
2094 case DataType::Type::kInt16:
2095 case DataType::Type::kInt32:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002096 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002097 break;
2098
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002099 case DataType::Type::kInt64:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002100 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002101 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002102 break;
2103
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002104 case DataType::Type::kFloat32:
2105 case DataType::Type::kFloat64:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002106 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002107 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002108 break;
2109
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002110 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002111 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002112 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002113}
2114
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002115void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002116 if (kIsDebugBuild) {
2117 switch (ret->InputAt(0)->GetType()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002118 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002119 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002120 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002121 case DataType::Type::kInt8:
2122 case DataType::Type::kUint16:
2123 case DataType::Type::kInt16:
2124 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002125 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002126 break;
2127
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002128 case DataType::Type::kInt64:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002129 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
2130 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002131 break;
2132
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002133 case DataType::Type::kFloat32:
2134 case DataType::Type::kFloat64:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002135 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002136 break;
2137
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002138 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002139 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002140 }
2141 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002142 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002143}
2144
Calin Juravle175dc732015-08-25 15:42:32 +01002145void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2146 // The trampoline uses the same calling convention as dex calling conventions,
2147 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2148 // the method_idx.
2149 HandleInvoke(invoke);
2150}
2151
2152void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2153 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2154}
2155
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002156void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002157 // Explicit clinit checks triggered by static invokes must have been pruned by
2158 // art::PrepareForRegisterAllocation.
2159 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002160
Mark Mendellfb8d2792015-03-31 22:16:59 -04002161 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002162 if (intrinsic.TryDispatch(invoke)) {
Vladimir Marko65979462017-05-19 17:25:12 +01002163 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeMethodLoadKind()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00002164 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002165 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04002166 return;
2167 }
2168
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002169 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002170
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002171 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
Vladimir Marko65979462017-05-19 17:25:12 +01002172 if (invoke->HasPcRelativeMethodLoadKind()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00002173 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002174 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002175}
2176
Mark Mendell09ed1a32015-03-25 08:30:06 -04002177static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
2178 if (invoke->GetLocations()->Intrinsified()) {
2179 IntrinsicCodeGeneratorX86 intrinsic(codegen);
2180 intrinsic.Dispatch(invoke);
2181 return true;
2182 }
2183 return false;
2184}
2185
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002186void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002187 // Explicit clinit checks triggered by static invokes must have been pruned by
2188 // art::PrepareForRegisterAllocation.
2189 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002190
Mark Mendell09ed1a32015-03-25 08:30:06 -04002191 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2192 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002193 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002194
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002195 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04002196 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002197 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002198}
2199
2200void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00002201 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
2202 if (intrinsic.TryDispatch(invoke)) {
2203 return;
2204 }
2205
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002206 HandleInvoke(invoke);
2207}
2208
2209void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002210 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002211 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002212}
2213
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002214void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002215 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2216 return;
2217 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002218
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002219 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002220 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002221}
2222
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002223void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002224 // This call to HandleInvoke allocates a temporary (core) register
2225 // which is also used to transfer the hidden argument from FP to
2226 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002227 HandleInvoke(invoke);
2228 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002229 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002230}
2231
2232void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
2233 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002234 LocationSummary* locations = invoke->GetLocations();
2235 Register temp = locations->GetTemp(0).AsRegister<Register>();
2236 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002237 Location receiver = locations->InAt(0);
2238 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2239
Roland Levillain0d5a2812015-11-13 10:07:31 +00002240 // Set the hidden argument. This is safe to do this here, as XMM7
2241 // won't be modified thereafter, before the `call` instruction.
2242 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002243 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002244 __ movd(hidden_reg, temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002245
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002246 if (receiver.IsStackSlot()) {
2247 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002248 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002249 __ movl(temp, Address(temp, class_offset));
2250 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002251 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002252 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002253 }
Roland Levillain4d027112015-07-01 15:41:14 +01002254 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002255 // Instead of simply (possibly) unpoisoning `temp` here, we should
2256 // emit a read barrier for the previous class reference load.
2257 // However this is not required in practice, as this is an
2258 // intermediate/temporary reference and because the current
2259 // concurrent copying collector keeps the from-space memory
2260 // intact/accessible until the end of the marking phase (the
2261 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002262 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002263 // temp = temp->GetAddressOfIMT()
2264 __ movl(temp,
2265 Address(temp, mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002266 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002267 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002268 invoke->GetImtIndex(), kX86PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002269 __ movl(temp, Address(temp, method_offset));
2270 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002271 __ call(Address(temp,
Andreas Gampe542451c2016-07-26 09:02:02 -07002272 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002273
2274 DCHECK(!codegen_->IsLeafMethod());
2275 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2276}
2277
Orion Hodsonac141392017-01-13 11:53:47 +00002278void LocationsBuilderX86::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2279 HandleInvoke(invoke);
2280}
2281
2282void InstructionCodeGeneratorX86::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2283 codegen_->GenerateInvokePolymorphicCall(invoke);
2284}
2285
Roland Levillain88cb1752014-10-20 16:36:47 +01002286void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2287 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002288 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Roland Levillain88cb1752014-10-20 16:36:47 +01002289 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002290 case DataType::Type::kInt32:
2291 case DataType::Type::kInt64:
Roland Levillain88cb1752014-10-20 16:36:47 +01002292 locations->SetInAt(0, Location::RequiresRegister());
2293 locations->SetOut(Location::SameAsFirstInput());
2294 break;
2295
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002296 case DataType::Type::kFloat32:
Roland Levillain5368c212014-11-27 15:03:41 +00002297 locations->SetInAt(0, Location::RequiresFpuRegister());
2298 locations->SetOut(Location::SameAsFirstInput());
2299 locations->AddTemp(Location::RequiresRegister());
2300 locations->AddTemp(Location::RequiresFpuRegister());
2301 break;
2302
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002303 case DataType::Type::kFloat64:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002304 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002305 locations->SetOut(Location::SameAsFirstInput());
2306 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002307 break;
2308
2309 default:
2310 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2311 }
2312}
2313
2314void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2315 LocationSummary* locations = neg->GetLocations();
2316 Location out = locations->Out();
2317 Location in = locations->InAt(0);
2318 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002319 case DataType::Type::kInt32:
Roland Levillain88cb1752014-10-20 16:36:47 +01002320 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002321 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002322 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002323 break;
2324
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002325 case DataType::Type::kInt64:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002326 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002327 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002328 __ negl(out.AsRegisterPairLow<Register>());
2329 // Negation is similar to subtraction from zero. The least
2330 // significant byte triggers a borrow when it is different from
2331 // zero; to take it into account, add 1 to the most significant
2332 // byte if the carry flag (CF) is set to 1 after the first NEGL
2333 // operation.
2334 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2335 __ negl(out.AsRegisterPairHigh<Register>());
2336 break;
2337
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002338 case DataType::Type::kFloat32: {
Roland Levillain5368c212014-11-27 15:03:41 +00002339 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002340 Register constant = locations->GetTemp(0).AsRegister<Register>();
2341 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002342 // Implement float negation with an exclusive or with value
2343 // 0x80000000 (mask for bit 31, representing the sign of a
2344 // single-precision floating-point number).
2345 __ movl(constant, Immediate(INT32_C(0x80000000)));
2346 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002347 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002348 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002349 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002350
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002351 case DataType::Type::kFloat64: {
Roland Levillain5368c212014-11-27 15:03:41 +00002352 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002353 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002354 // Implement double negation with an exclusive or with value
2355 // 0x8000000000000000 (mask for bit 63, representing the sign of
2356 // a double-precision floating-point number).
2357 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002358 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002359 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002360 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002361
2362 default:
2363 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2364 }
2365}
2366
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002367void LocationsBuilderX86::VisitX86FPNeg(HX86FPNeg* neg) {
2368 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002369 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002370 DCHECK(DataType::IsFloatingPointType(neg->GetType()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002371 locations->SetInAt(0, Location::RequiresFpuRegister());
2372 locations->SetInAt(1, Location::RequiresRegister());
2373 locations->SetOut(Location::SameAsFirstInput());
2374 locations->AddTemp(Location::RequiresFpuRegister());
2375}
2376
2377void InstructionCodeGeneratorX86::VisitX86FPNeg(HX86FPNeg* neg) {
2378 LocationSummary* locations = neg->GetLocations();
2379 Location out = locations->Out();
2380 DCHECK(locations->InAt(0).Equals(out));
2381
2382 Register constant_area = locations->InAt(1).AsRegister<Register>();
2383 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002384 if (neg->GetType() == DataType::Type::kFloat32) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00002385 __ movss(mask, codegen_->LiteralInt32Address(INT32_C(0x80000000),
2386 neg->GetBaseMethodAddress(),
2387 constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002388 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
2389 } else {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00002390 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000),
2391 neg->GetBaseMethodAddress(),
2392 constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002393 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
2394 }
2395}
2396
Roland Levillaindff1f282014-11-05 14:15:05 +00002397void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002398 DataType::Type result_type = conversion->GetResultType();
2399 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002400 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
2401 << input_type << " -> " << result_type;
Roland Levillain624279f2014-12-04 11:54:28 +00002402
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002403 // The float-to-long and double-to-long type conversions rely on a
2404 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002405 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002406 ((input_type == DataType::Type::kFloat32 || input_type == DataType::Type::kFloat64)
2407 && result_type == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002408 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002409 : LocationSummary::kNoCall;
2410 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002411 new (GetGraph()->GetAllocator()) LocationSummary(conversion, call_kind);
Roland Levillain624279f2014-12-04 11:54:28 +00002412
Roland Levillaindff1f282014-11-05 14:15:05 +00002413 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002414 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002415 case DataType::Type::kInt8:
Roland Levillain51d3fc42014-11-13 14:11:42 +00002416 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002417 case DataType::Type::kUint8:
2418 case DataType::Type::kInt8:
2419 case DataType::Type::kUint16:
2420 case DataType::Type::kInt16:
2421 case DataType::Type::kInt32:
2422 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2423 // Make the output overlap to please the register allocator. This greatly simplifies
2424 // the validation of the linear scan implementation
2425 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2426 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002427 case DataType::Type::kInt64: {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002428 HInstruction* input = conversion->InputAt(0);
2429 Location input_location = input->IsConstant()
2430 ? Location::ConstantLocation(input->AsConstant())
2431 : Location::RegisterPairLocation(EAX, EDX);
2432 locations->SetInAt(0, input_location);
2433 // Make the output overlap to please the register allocator. This greatly simplifies
2434 // the validation of the linear scan implementation
2435 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2436 break;
2437 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002438
2439 default:
2440 LOG(FATAL) << "Unexpected type conversion from " << input_type
2441 << " to " << result_type;
2442 }
2443 break;
2444
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002445 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002446 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002447 DCHECK(DataType::IsIntegralType(input_type)) << input_type;
2448 locations->SetInAt(0, Location::Any());
2449 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Roland Levillain01a8d712014-11-14 16:27:39 +00002450 break;
2451
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002452 case DataType::Type::kInt32:
Roland Levillain946e1432014-11-11 17:35:19 +00002453 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002454 case DataType::Type::kInt64:
Roland Levillain946e1432014-11-11 17:35:19 +00002455 locations->SetInAt(0, Location::Any());
2456 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2457 break;
2458
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002459 case DataType::Type::kFloat32:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002460 locations->SetInAt(0, Location::RequiresFpuRegister());
2461 locations->SetOut(Location::RequiresRegister());
2462 locations->AddTemp(Location::RequiresFpuRegister());
2463 break;
2464
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002465 case DataType::Type::kFloat64:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002466 locations->SetInAt(0, Location::RequiresFpuRegister());
2467 locations->SetOut(Location::RequiresRegister());
2468 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002469 break;
2470
2471 default:
2472 LOG(FATAL) << "Unexpected type conversion from " << input_type
2473 << " to " << result_type;
2474 }
2475 break;
2476
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002477 case DataType::Type::kInt64:
Roland Levillaindff1f282014-11-05 14:15:05 +00002478 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002479 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002480 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002481 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002482 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002483 case DataType::Type::kInt16:
2484 case DataType::Type::kInt32:
Roland Levillaindff1f282014-11-05 14:15:05 +00002485 locations->SetInAt(0, Location::RegisterLocation(EAX));
2486 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2487 break;
2488
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002489 case DataType::Type::kFloat32:
2490 case DataType::Type::kFloat64: {
Vladimir Marko949c91f2015-01-27 10:48:44 +00002491 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002492 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2493 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2494
Vladimir Marko949c91f2015-01-27 10:48:44 +00002495 // The runtime helper puts the result in EAX, EDX.
2496 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002497 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002498 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002499
2500 default:
2501 LOG(FATAL) << "Unexpected type conversion from " << input_type
2502 << " to " << result_type;
2503 }
2504 break;
2505
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002506 case DataType::Type::kFloat32:
Roland Levillaincff13742014-11-17 14:32:17 +00002507 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002508 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002509 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002510 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002511 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002512 case DataType::Type::kInt16:
2513 case DataType::Type::kInt32:
Roland Levillaincff13742014-11-17 14:32:17 +00002514 locations->SetInAt(0, Location::RequiresRegister());
2515 locations->SetOut(Location::RequiresFpuRegister());
2516 break;
2517
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002518 case DataType::Type::kInt64:
Roland Levillain232ade02015-04-20 15:14:36 +01002519 locations->SetInAt(0, Location::Any());
2520 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002521 break;
2522
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002523 case DataType::Type::kFloat64:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002524 locations->SetInAt(0, Location::RequiresFpuRegister());
2525 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002526 break;
2527
2528 default:
2529 LOG(FATAL) << "Unexpected type conversion from " << input_type
2530 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08002531 }
Roland Levillaincff13742014-11-17 14:32:17 +00002532 break;
2533
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002534 case DataType::Type::kFloat64:
Roland Levillaincff13742014-11-17 14:32:17 +00002535 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002536 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002537 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002538 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002539 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002540 case DataType::Type::kInt16:
2541 case DataType::Type::kInt32:
Roland Levillaincff13742014-11-17 14:32:17 +00002542 locations->SetInAt(0, Location::RequiresRegister());
2543 locations->SetOut(Location::RequiresFpuRegister());
2544 break;
2545
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002546 case DataType::Type::kInt64:
Roland Levillain232ade02015-04-20 15:14:36 +01002547 locations->SetInAt(0, Location::Any());
2548 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002549 break;
2550
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002551 case DataType::Type::kFloat32:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002552 locations->SetInAt(0, Location::RequiresFpuRegister());
2553 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002554 break;
2555
2556 default:
2557 LOG(FATAL) << "Unexpected type conversion from " << input_type
2558 << " to " << result_type;
2559 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002560 break;
2561
2562 default:
2563 LOG(FATAL) << "Unexpected type conversion from " << input_type
2564 << " to " << result_type;
2565 }
2566}
2567
2568void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2569 LocationSummary* locations = conversion->GetLocations();
2570 Location out = locations->Out();
2571 Location in = locations->InAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002572 DataType::Type result_type = conversion->GetResultType();
2573 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002574 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
2575 << input_type << " -> " << result_type;
Roland Levillaindff1f282014-11-05 14:15:05 +00002576 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002577 case DataType::Type::kUint8:
2578 switch (input_type) {
2579 case DataType::Type::kInt8:
2580 case DataType::Type::kUint16:
2581 case DataType::Type::kInt16:
2582 case DataType::Type::kInt32:
2583 if (in.IsRegister()) {
2584 __ movzxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
2585 } else {
2586 DCHECK(in.GetConstant()->IsIntConstant());
2587 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2588 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint8_t>(value)));
2589 }
2590 break;
2591 case DataType::Type::kInt64:
2592 if (in.IsRegisterPair()) {
2593 __ movzxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2594 } else {
2595 DCHECK(in.GetConstant()->IsLongConstant());
2596 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2597 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint8_t>(value)));
2598 }
2599 break;
2600
2601 default:
2602 LOG(FATAL) << "Unexpected type conversion from " << input_type
2603 << " to " << result_type;
2604 }
2605 break;
2606
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002607 case DataType::Type::kInt8:
Roland Levillain51d3fc42014-11-13 14:11:42 +00002608 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002609 case DataType::Type::kUint8:
2610 case DataType::Type::kUint16:
2611 case DataType::Type::kInt16:
2612 case DataType::Type::kInt32:
2613 if (in.IsRegister()) {
2614 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
2615 } else {
2616 DCHECK(in.GetConstant()->IsIntConstant());
2617 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2618 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2619 }
2620 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002621 case DataType::Type::kInt64:
Vladimir Markob52bbde2016-02-12 12:06:05 +00002622 if (in.IsRegisterPair()) {
2623 __ movsxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2624 } else {
2625 DCHECK(in.GetConstant()->IsLongConstant());
2626 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2627 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2628 }
2629 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002630
2631 default:
2632 LOG(FATAL) << "Unexpected type conversion from " << input_type
2633 << " to " << result_type;
2634 }
2635 break;
2636
2637 case DataType::Type::kUint16:
2638 switch (input_type) {
2639 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002640 case DataType::Type::kInt16:
2641 case DataType::Type::kInt32:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002642 if (in.IsRegister()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002643 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
2644 } else if (in.IsStackSlot()) {
2645 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002646 } else {
2647 DCHECK(in.GetConstant()->IsIntConstant());
2648 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002649 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
2650 }
2651 break;
2652 case DataType::Type::kInt64:
2653 if (in.IsRegisterPair()) {
2654 __ movzxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2655 } else if (in.IsDoubleStackSlot()) {
2656 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2657 } else {
2658 DCHECK(in.GetConstant()->IsLongConstant());
2659 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2660 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002661 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002662 break;
2663
2664 default:
2665 LOG(FATAL) << "Unexpected type conversion from " << input_type
2666 << " to " << result_type;
2667 }
2668 break;
2669
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002670 case DataType::Type::kInt16:
Roland Levillain01a8d712014-11-14 16:27:39 +00002671 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002672 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002673 case DataType::Type::kInt32:
Roland Levillain01a8d712014-11-14 16:27:39 +00002674 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002675 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002676 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002677 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002678 } else {
2679 DCHECK(in.GetConstant()->IsIntConstant());
2680 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002681 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002682 }
2683 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002684 case DataType::Type::kInt64:
2685 if (in.IsRegisterPair()) {
2686 __ movsxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2687 } else if (in.IsDoubleStackSlot()) {
2688 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2689 } else {
2690 DCHECK(in.GetConstant()->IsLongConstant());
2691 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2692 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
2693 }
2694 break;
Roland Levillain01a8d712014-11-14 16:27:39 +00002695
2696 default:
2697 LOG(FATAL) << "Unexpected type conversion from " << input_type
2698 << " to " << result_type;
2699 }
2700 break;
2701
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002702 case DataType::Type::kInt32:
Roland Levillain946e1432014-11-11 17:35:19 +00002703 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002704 case DataType::Type::kInt64:
Roland Levillain946e1432014-11-11 17:35:19 +00002705 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002706 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002707 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002708 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002709 } else {
2710 DCHECK(in.IsConstant());
2711 DCHECK(in.GetConstant()->IsLongConstant());
2712 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002713 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002714 }
2715 break;
2716
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002717 case DataType::Type::kFloat32: {
Roland Levillain3f8f9362014-12-02 17:45:01 +00002718 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2719 Register output = out.AsRegister<Register>();
2720 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002721 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002722
2723 __ movl(output, Immediate(kPrimIntMax));
2724 // temp = int-to-float(output)
2725 __ cvtsi2ss(temp, output);
2726 // if input >= temp goto done
2727 __ comiss(input, temp);
2728 __ j(kAboveEqual, &done);
2729 // if input == NaN goto nan
2730 __ j(kUnordered, &nan);
2731 // output = float-to-int-truncate(input)
2732 __ cvttss2si(output, input);
2733 __ jmp(&done);
2734 __ Bind(&nan);
2735 // output = 0
2736 __ xorl(output, output);
2737 __ Bind(&done);
2738 break;
2739 }
2740
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002741 case DataType::Type::kFloat64: {
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002742 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2743 Register output = out.AsRegister<Register>();
2744 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002745 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002746
2747 __ movl(output, Immediate(kPrimIntMax));
2748 // temp = int-to-double(output)
2749 __ cvtsi2sd(temp, output);
2750 // if input >= temp goto done
2751 __ comisd(input, temp);
2752 __ j(kAboveEqual, &done);
2753 // if input == NaN goto nan
2754 __ j(kUnordered, &nan);
2755 // output = double-to-int-truncate(input)
2756 __ cvttsd2si(output, input);
2757 __ jmp(&done);
2758 __ Bind(&nan);
2759 // output = 0
2760 __ xorl(output, output);
2761 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002762 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002763 }
Roland Levillain946e1432014-11-11 17:35:19 +00002764
2765 default:
2766 LOG(FATAL) << "Unexpected type conversion from " << input_type
2767 << " to " << result_type;
2768 }
2769 break;
2770
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002771 case DataType::Type::kInt64:
Roland Levillaindff1f282014-11-05 14:15:05 +00002772 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002773 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002774 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002775 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002776 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002777 case DataType::Type::kInt16:
2778 case DataType::Type::kInt32:
Roland Levillaindff1f282014-11-05 14:15:05 +00002779 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2780 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002781 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002782 __ cdq();
2783 break;
2784
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002785 case DataType::Type::kFloat32:
Serban Constantinescuba45db02016-07-12 22:53:02 +01002786 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002787 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002788 break;
2789
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002790 case DataType::Type::kFloat64:
Serban Constantinescuba45db02016-07-12 22:53:02 +01002791 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002792 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002793 break;
2794
2795 default:
2796 LOG(FATAL) << "Unexpected type conversion from " << input_type
2797 << " to " << result_type;
2798 }
2799 break;
2800
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002801 case DataType::Type::kFloat32:
Roland Levillaincff13742014-11-17 14:32:17 +00002802 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002803 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002804 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002805 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002806 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002807 case DataType::Type::kInt16:
2808 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002809 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002810 break;
2811
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002812 case DataType::Type::kInt64: {
Roland Levillain232ade02015-04-20 15:14:36 +01002813 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002814
Roland Levillain232ade02015-04-20 15:14:36 +01002815 // Create stack space for the call to
2816 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2817 // TODO: enhance register allocator to ask for stack temporaries.
2818 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002819 adjustment = DataType::Size(DataType::Type::kInt64);
Roland Levillain232ade02015-04-20 15:14:36 +01002820 __ subl(ESP, Immediate(adjustment));
2821 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002822
Roland Levillain232ade02015-04-20 15:14:36 +01002823 // Load the value to the FP stack, using temporaries if needed.
2824 PushOntoFPStack(in, 0, adjustment, false, true);
2825
2826 if (out.IsStackSlot()) {
2827 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2828 } else {
2829 __ fstps(Address(ESP, 0));
2830 Location stack_temp = Location::StackSlot(0);
2831 codegen_->Move32(out, stack_temp);
2832 }
2833
2834 // Remove the temporary stack space we allocated.
2835 if (adjustment != 0) {
2836 __ addl(ESP, Immediate(adjustment));
2837 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002838 break;
2839 }
2840
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002841 case DataType::Type::kFloat64:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002842 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002843 break;
2844
2845 default:
2846 LOG(FATAL) << "Unexpected type conversion from " << input_type
2847 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08002848 }
Roland Levillaincff13742014-11-17 14:32:17 +00002849 break;
2850
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002851 case DataType::Type::kFloat64:
Roland Levillaincff13742014-11-17 14:32:17 +00002852 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002853 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002854 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002855 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002856 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002857 case DataType::Type::kInt16:
2858 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002859 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002860 break;
2861
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002862 case DataType::Type::kInt64: {
Roland Levillain232ade02015-04-20 15:14:36 +01002863 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002864
Roland Levillain232ade02015-04-20 15:14:36 +01002865 // Create stack space for the call to
2866 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2867 // TODO: enhance register allocator to ask for stack temporaries.
2868 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002869 adjustment = DataType::Size(DataType::Type::kInt64);
Roland Levillain232ade02015-04-20 15:14:36 +01002870 __ subl(ESP, Immediate(adjustment));
2871 }
2872
2873 // Load the value to the FP stack, using temporaries if needed.
2874 PushOntoFPStack(in, 0, adjustment, false, true);
2875
2876 if (out.IsDoubleStackSlot()) {
2877 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2878 } else {
2879 __ fstpl(Address(ESP, 0));
2880 Location stack_temp = Location::DoubleStackSlot(0);
2881 codegen_->Move64(out, stack_temp);
2882 }
2883
2884 // Remove the temporary stack space we allocated.
2885 if (adjustment != 0) {
2886 __ addl(ESP, Immediate(adjustment));
2887 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002888 break;
2889 }
2890
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002891 case DataType::Type::kFloat32:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002892 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002893 break;
2894
2895 default:
2896 LOG(FATAL) << "Unexpected type conversion from " << input_type
2897 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08002898 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002899 break;
2900
2901 default:
2902 LOG(FATAL) << "Unexpected type conversion from " << input_type
2903 << " to " << result_type;
2904 }
2905}
2906
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002907void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002908 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002909 new (GetGraph()->GetAllocator()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002910 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002911 case DataType::Type::kInt32: {
Mark Mendell09b84632015-02-13 17:48:38 -05002912 locations->SetInAt(0, Location::RequiresRegister());
2913 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2914 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2915 break;
2916 }
2917
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002918 case DataType::Type::kInt64: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002919 locations->SetInAt(0, Location::RequiresRegister());
2920 locations->SetInAt(1, Location::Any());
2921 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002922 break;
2923 }
2924
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002925 case DataType::Type::kFloat32:
2926 case DataType::Type::kFloat64: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002927 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002928 if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2929 DCHECK(add->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002930 } else if (add->InputAt(1)->IsConstant()) {
2931 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002932 } else {
2933 locations->SetInAt(1, Location::Any());
2934 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002935 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002936 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002937 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002938
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002939 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002940 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2941 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002942 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002943}
2944
2945void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2946 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002947 Location first = locations->InAt(0);
2948 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002949 Location out = locations->Out();
2950
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002951 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002952 case DataType::Type::kInt32: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002953 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002954 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2955 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002956 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2957 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05002958 } else {
2959 __ leal(out.AsRegister<Register>(), Address(
2960 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
2961 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002962 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002963 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
2964 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2965 __ addl(out.AsRegister<Register>(), Immediate(value));
2966 } else {
2967 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
2968 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002969 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05002970 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002971 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002972 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002973 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002974 }
2975
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002976 case DataType::Type::kInt64: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002977 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002978 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2979 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002980 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002981 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
2982 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002983 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002984 } else {
2985 DCHECK(second.IsConstant()) << second;
2986 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2987 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2988 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002989 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002990 break;
2991 }
2992
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002993 case DataType::Type::kFloat32: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002994 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002995 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002996 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2997 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002998 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002999 __ addss(first.AsFpuRegister<XmmRegister>(),
3000 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003001 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3002 const_area->GetBaseMethodAddress(),
3003 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003004 } else {
3005 DCHECK(second.IsStackSlot());
3006 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003007 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003008 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003009 }
3010
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003011 case DataType::Type::kFloat64: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003012 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003013 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04003014 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3015 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003016 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003017 __ addsd(first.AsFpuRegister<XmmRegister>(),
3018 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003019 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3020 const_area->GetBaseMethodAddress(),
3021 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003022 } else {
3023 DCHECK(second.IsDoubleStackSlot());
3024 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003025 }
3026 break;
3027 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003028
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003029 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003030 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003031 }
3032}
3033
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003034void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003035 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003036 new (GetGraph()->GetAllocator()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003037 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003038 case DataType::Type::kInt32:
3039 case DataType::Type::kInt64: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003040 locations->SetInAt(0, Location::RequiresRegister());
3041 locations->SetInAt(1, Location::Any());
3042 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003043 break;
3044 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003045 case DataType::Type::kFloat32:
3046 case DataType::Type::kFloat64: {
Calin Juravle11351682014-10-23 15:38:15 +01003047 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003048 if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3049 DCHECK(sub->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003050 } else if (sub->InputAt(1)->IsConstant()) {
3051 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003052 } else {
3053 locations->SetInAt(1, Location::Any());
3054 }
Calin Juravle11351682014-10-23 15:38:15 +01003055 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003056 break;
Calin Juravle11351682014-10-23 15:38:15 +01003057 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003058
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003059 default:
Calin Juravle11351682014-10-23 15:38:15 +01003060 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003061 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003062}
3063
3064void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
3065 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003066 Location first = locations->InAt(0);
3067 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01003068 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003069 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003070 case DataType::Type::kInt32: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003071 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003072 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003073 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003074 __ subl(first.AsRegister<Register>(),
3075 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003076 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003077 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003078 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003079 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003080 }
3081
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003082 case DataType::Type::kInt64: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003083 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01003084 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3085 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003086 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01003087 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003088 __ sbbl(first.AsRegisterPairHigh<Register>(),
3089 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003090 } else {
3091 DCHECK(second.IsConstant()) << second;
3092 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3093 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3094 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003095 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003096 break;
3097 }
3098
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003099 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003100 if (second.IsFpuRegister()) {
3101 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3102 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3103 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003104 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003105 __ subss(first.AsFpuRegister<XmmRegister>(),
3106 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003107 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3108 const_area->GetBaseMethodAddress(),
3109 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003110 } else {
3111 DCHECK(second.IsStackSlot());
3112 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3113 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003114 break;
Calin Juravle11351682014-10-23 15:38:15 +01003115 }
3116
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003117 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003118 if (second.IsFpuRegister()) {
3119 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3120 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3121 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003122 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003123 __ subsd(first.AsFpuRegister<XmmRegister>(),
3124 codegen_->LiteralDoubleAddress(
3125 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003126 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003127 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3128 } else {
3129 DCHECK(second.IsDoubleStackSlot());
3130 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3131 }
Calin Juravle11351682014-10-23 15:38:15 +01003132 break;
3133 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003134
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003135 default:
Calin Juravle11351682014-10-23 15:38:15 +01003136 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003137 }
3138}
3139
Calin Juravle34bacdf2014-10-07 20:23:36 +01003140void LocationsBuilderX86::VisitMul(HMul* mul) {
3141 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003142 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Calin Juravle34bacdf2014-10-07 20:23:36 +01003143 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003144 case DataType::Type::kInt32:
Calin Juravle34bacdf2014-10-07 20:23:36 +01003145 locations->SetInAt(0, Location::RequiresRegister());
3146 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003147 if (mul->InputAt(1)->IsIntConstant()) {
3148 // Can use 3 operand multiply.
3149 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3150 } else {
3151 locations->SetOut(Location::SameAsFirstInput());
3152 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003153 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003154 case DataType::Type::kInt64: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003155 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003156 locations->SetInAt(1, Location::Any());
3157 locations->SetOut(Location::SameAsFirstInput());
3158 // Needed for imul on 32bits with 64bits output.
3159 locations->AddTemp(Location::RegisterLocation(EAX));
3160 locations->AddTemp(Location::RegisterLocation(EDX));
3161 break;
3162 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003163 case DataType::Type::kFloat32:
3164 case DataType::Type::kFloat64: {
Calin Juravleb5bfa962014-10-21 18:02:24 +01003165 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003166 if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3167 DCHECK(mul->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003168 } else if (mul->InputAt(1)->IsConstant()) {
3169 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003170 } else {
3171 locations->SetInAt(1, Location::Any());
3172 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003173 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003174 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003175 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003176
3177 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003178 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003179 }
3180}
3181
3182void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
3183 LocationSummary* locations = mul->GetLocations();
3184 Location first = locations->InAt(0);
3185 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003186 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003187
3188 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003189 case DataType::Type::kInt32:
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003190 // The constant may have ended up in a register, so test explicitly to avoid
3191 // problems where the output may not be the same as the first operand.
3192 if (mul->InputAt(1)->IsIntConstant()) {
3193 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3194 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
3195 } else if (second.IsRegister()) {
3196 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003197 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003198 } else {
3199 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003200 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003201 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003202 }
3203 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003204
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003205 case DataType::Type::kInt64: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003206 Register in1_hi = first.AsRegisterPairHigh<Register>();
3207 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003208 Register eax = locations->GetTemp(0).AsRegister<Register>();
3209 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003210
3211 DCHECK_EQ(EAX, eax);
3212 DCHECK_EQ(EDX, edx);
3213
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003214 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01003215 // output: in1
3216 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3217 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3218 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003219 if (second.IsConstant()) {
3220 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003221
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003222 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3223 int32_t low_value = Low32Bits(value);
3224 int32_t high_value = High32Bits(value);
3225 Immediate low(low_value);
3226 Immediate high(high_value);
3227
3228 __ movl(eax, high);
3229 // eax <- in1.lo * in2.hi
3230 __ imull(eax, in1_lo);
3231 // in1.hi <- in1.hi * in2.lo
3232 __ imull(in1_hi, low);
3233 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3234 __ addl(in1_hi, eax);
3235 // move in2_lo to eax to prepare for double precision
3236 __ movl(eax, low);
3237 // edx:eax <- in1.lo * in2.lo
3238 __ mull(in1_lo);
3239 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3240 __ addl(in1_hi, edx);
3241 // in1.lo <- (in1.lo * in2.lo)[31:0];
3242 __ movl(in1_lo, eax);
3243 } else if (second.IsRegisterPair()) {
3244 Register in2_hi = second.AsRegisterPairHigh<Register>();
3245 Register in2_lo = second.AsRegisterPairLow<Register>();
3246
3247 __ movl(eax, in2_hi);
3248 // eax <- in1.lo * in2.hi
3249 __ imull(eax, in1_lo);
3250 // in1.hi <- in1.hi * in2.lo
3251 __ imull(in1_hi, in2_lo);
3252 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3253 __ addl(in1_hi, eax);
3254 // move in1_lo to eax to prepare for double precision
3255 __ movl(eax, in1_lo);
3256 // edx:eax <- in1.lo * in2.lo
3257 __ mull(in2_lo);
3258 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3259 __ addl(in1_hi, edx);
3260 // in1.lo <- (in1.lo * in2.lo)[31:0];
3261 __ movl(in1_lo, eax);
3262 } else {
3263 DCHECK(second.IsDoubleStackSlot()) << second;
3264 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
3265 Address in2_lo(ESP, second.GetStackIndex());
3266
3267 __ movl(eax, in2_hi);
3268 // eax <- in1.lo * in2.hi
3269 __ imull(eax, in1_lo);
3270 // in1.hi <- in1.hi * in2.lo
3271 __ imull(in1_hi, in2_lo);
3272 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3273 __ addl(in1_hi, eax);
3274 // move in1_lo to eax to prepare for double precision
3275 __ movl(eax, in1_lo);
3276 // edx:eax <- in1.lo * in2.lo
3277 __ mull(in2_lo);
3278 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3279 __ addl(in1_hi, edx);
3280 // in1.lo <- (in1.lo * in2.lo)[31:0];
3281 __ movl(in1_lo, eax);
3282 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003283
3284 break;
3285 }
3286
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003287 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003288 DCHECK(first.Equals(locations->Out()));
3289 if (second.IsFpuRegister()) {
3290 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3291 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3292 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003293 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003294 __ mulss(first.AsFpuRegister<XmmRegister>(),
3295 codegen_->LiteralFloatAddress(
3296 const_area->GetConstant()->AsFloatConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003297 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003298 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3299 } else {
3300 DCHECK(second.IsStackSlot());
3301 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3302 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003303 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003304 }
3305
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003306 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003307 DCHECK(first.Equals(locations->Out()));
3308 if (second.IsFpuRegister()) {
3309 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3310 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3311 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003312 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003313 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3314 codegen_->LiteralDoubleAddress(
3315 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003316 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003317 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3318 } else {
3319 DCHECK(second.IsDoubleStackSlot());
3320 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3321 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003322 break;
3323 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003324
3325 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003326 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003327 }
3328}
3329
Roland Levillain232ade02015-04-20 15:14:36 +01003330void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3331 uint32_t temp_offset,
3332 uint32_t stack_adjustment,
3333 bool is_fp,
3334 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003335 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003336 DCHECK(!is_wide);
3337 if (is_fp) {
3338 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3339 } else {
3340 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3341 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003342 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003343 DCHECK(is_wide);
3344 if (is_fp) {
3345 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3346 } else {
3347 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3348 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003349 } else {
3350 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003351 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003352 Location stack_temp = Location::StackSlot(temp_offset);
3353 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003354 if (is_fp) {
3355 __ flds(Address(ESP, temp_offset));
3356 } else {
3357 __ filds(Address(ESP, temp_offset));
3358 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003359 } else {
3360 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3361 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003362 if (is_fp) {
3363 __ fldl(Address(ESP, temp_offset));
3364 } else {
3365 __ fildl(Address(ESP, temp_offset));
3366 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003367 }
3368 }
3369}
3370
3371void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003372 DataType::Type type = rem->GetResultType();
3373 bool is_float = type == DataType::Type::kFloat32;
3374 size_t elem_size = DataType::Size(type);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003375 LocationSummary* locations = rem->GetLocations();
3376 Location first = locations->InAt(0);
3377 Location second = locations->InAt(1);
3378 Location out = locations->Out();
3379
3380 // Create stack space for 2 elements.
3381 // TODO: enhance register allocator to ask for stack temporaries.
3382 __ subl(ESP, Immediate(2 * elem_size));
3383
3384 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003385 const bool is_wide = !is_float;
3386 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
3387 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003388
3389 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003390 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003391 __ Bind(&retry);
3392 __ fprem();
3393
3394 // Move FP status to AX.
3395 __ fstsw();
3396
3397 // And see if the argument reduction is complete. This is signaled by the
3398 // C2 FPU flag bit set to 0.
3399 __ andl(EAX, Immediate(kC2ConditionMask));
3400 __ j(kNotEqual, &retry);
3401
3402 // We have settled on the final value. Retrieve it into an XMM register.
3403 // Store FP top of stack to real stack.
3404 if (is_float) {
3405 __ fsts(Address(ESP, 0));
3406 } else {
3407 __ fstl(Address(ESP, 0));
3408 }
3409
3410 // Pop the 2 items from the FP stack.
3411 __ fucompp();
3412
3413 // Load the value from the stack into an XMM register.
3414 DCHECK(out.IsFpuRegister()) << out;
3415 if (is_float) {
3416 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3417 } else {
3418 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3419 }
3420
3421 // And remove the temporary stack space we allocated.
3422 __ addl(ESP, Immediate(2 * elem_size));
3423}
3424
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003425
3426void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3427 DCHECK(instruction->IsDiv() || instruction->IsRem());
3428
3429 LocationSummary* locations = instruction->GetLocations();
3430 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003431 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003432
3433 Register out_register = locations->Out().AsRegister<Register>();
3434 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003435 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003436
3437 DCHECK(imm == 1 || imm == -1);
3438
3439 if (instruction->IsRem()) {
3440 __ xorl(out_register, out_register);
3441 } else {
3442 __ movl(out_register, input_register);
3443 if (imm == -1) {
3444 __ negl(out_register);
3445 }
3446 }
3447}
3448
3449
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003450void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003451 LocationSummary* locations = instruction->GetLocations();
3452
3453 Register out_register = locations->Out().AsRegister<Register>();
3454 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003455 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003456 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3457 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003458
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003459 Register num = locations->GetTemp(0).AsRegister<Register>();
3460
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003461 __ leal(num, Address(input_register, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003462 __ testl(input_register, input_register);
3463 __ cmovl(kGreaterEqual, num, input_register);
3464 int shift = CTZ(imm);
3465 __ sarl(num, Immediate(shift));
3466
3467 if (imm < 0) {
3468 __ negl(num);
3469 }
3470
3471 __ movl(out_register, num);
3472}
3473
3474void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3475 DCHECK(instruction->IsDiv() || instruction->IsRem());
3476
3477 LocationSummary* locations = instruction->GetLocations();
3478 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3479
3480 Register eax = locations->InAt(0).AsRegister<Register>();
3481 Register out = locations->Out().AsRegister<Register>();
3482 Register num;
3483 Register edx;
3484
3485 if (instruction->IsDiv()) {
3486 edx = locations->GetTemp(0).AsRegister<Register>();
3487 num = locations->GetTemp(1).AsRegister<Register>();
3488 } else {
3489 edx = locations->Out().AsRegister<Register>();
3490 num = locations->GetTemp(0).AsRegister<Register>();
3491 }
3492
3493 DCHECK_EQ(EAX, eax);
3494 DCHECK_EQ(EDX, edx);
3495 if (instruction->IsDiv()) {
3496 DCHECK_EQ(EAX, out);
3497 } else {
3498 DCHECK_EQ(EDX, out);
3499 }
3500
3501 int64_t magic;
3502 int shift;
3503 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3504
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003505 // Save the numerator.
3506 __ movl(num, eax);
3507
3508 // EAX = magic
3509 __ movl(eax, Immediate(magic));
3510
3511 // EDX:EAX = magic * numerator
3512 __ imull(num);
3513
3514 if (imm > 0 && magic < 0) {
3515 // EDX += num
3516 __ addl(edx, num);
3517 } else if (imm < 0 && magic > 0) {
3518 __ subl(edx, num);
3519 }
3520
3521 // Shift if needed.
3522 if (shift != 0) {
3523 __ sarl(edx, Immediate(shift));
3524 }
3525
3526 // EDX += 1 if EDX < 0
3527 __ movl(eax, edx);
3528 __ shrl(edx, Immediate(31));
3529 __ addl(edx, eax);
3530
3531 if (instruction->IsRem()) {
3532 __ movl(eax, num);
3533 __ imull(edx, Immediate(imm));
3534 __ subl(eax, edx);
3535 __ movl(edx, eax);
3536 } else {
3537 __ movl(eax, edx);
3538 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003539}
3540
Calin Juravlebacfec32014-11-14 15:54:36 +00003541void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3542 DCHECK(instruction->IsDiv() || instruction->IsRem());
3543
3544 LocationSummary* locations = instruction->GetLocations();
3545 Location out = locations->Out();
3546 Location first = locations->InAt(0);
3547 Location second = locations->InAt(1);
3548 bool is_div = instruction->IsDiv();
3549
3550 switch (instruction->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003551 case DataType::Type::kInt32: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003552 DCHECK_EQ(EAX, first.AsRegister<Register>());
3553 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003554
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003555 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003556 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003557
3558 if (imm == 0) {
3559 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3560 } else if (imm == 1 || imm == -1) {
3561 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003562 } else if (is_div && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003563 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003564 } else {
3565 DCHECK(imm <= -2 || imm >= 2);
3566 GenerateDivRemWithAnyConstant(instruction);
3567 }
3568 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01003569 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) DivRemMinusOneSlowPathX86(
David Srbecky9cd6d372016-02-09 15:24:47 +00003570 instruction, out.AsRegister<Register>(), is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003571 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003572
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003573 Register second_reg = second.AsRegister<Register>();
3574 // 0x80000000/-1 triggers an arithmetic exception!
3575 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3576 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003577
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003578 __ cmpl(second_reg, Immediate(-1));
3579 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003580
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003581 // edx:eax <- sign-extended of eax
3582 __ cdq();
3583 // eax = quotient, edx = remainder
3584 __ idivl(second_reg);
3585 __ Bind(slow_path->GetExitLabel());
3586 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003587 break;
3588 }
3589
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003590 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003591 InvokeRuntimeCallingConvention calling_convention;
3592 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3593 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3594 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3595 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3596 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3597 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3598
3599 if (is_div) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003600 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003601 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003602 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003603 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003604 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003605 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003606 break;
3607 }
3608
3609 default:
3610 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3611 }
3612}
3613
Calin Juravle7c4954d2014-10-28 16:57:40 +00003614void LocationsBuilderX86::VisitDiv(HDiv* div) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003615 LocationSummary::CallKind call_kind = (div->GetResultType() == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003616 ? LocationSummary::kCallOnMainOnly
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003617 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01003618 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(div, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003619
Calin Juravle7c4954d2014-10-28 16:57:40 +00003620 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003621 case DataType::Type::kInt32: {
Calin Juravled0d48522014-11-04 16:40:20 +00003622 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003623 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003624 locations->SetOut(Location::SameAsFirstInput());
3625 // Intel uses edx:eax as the dividend.
3626 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003627 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3628 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3629 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003630 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003631 locations->AddTemp(Location::RequiresRegister());
3632 }
Calin Juravled0d48522014-11-04 16:40:20 +00003633 break;
3634 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003635 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003636 InvokeRuntimeCallingConvention calling_convention;
3637 locations->SetInAt(0, Location::RegisterPairLocation(
3638 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3639 locations->SetInAt(1, Location::RegisterPairLocation(
3640 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3641 // Runtime helper puts the result in EAX, EDX.
3642 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003643 break;
3644 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003645 case DataType::Type::kFloat32:
3646 case DataType::Type::kFloat64: {
Calin Juravle7c4954d2014-10-28 16:57:40 +00003647 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003648 if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3649 DCHECK(div->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003650 } else if (div->InputAt(1)->IsConstant()) {
3651 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003652 } else {
3653 locations->SetInAt(1, Location::Any());
3654 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003655 locations->SetOut(Location::SameAsFirstInput());
3656 break;
3657 }
3658
3659 default:
3660 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3661 }
3662}
3663
3664void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3665 LocationSummary* locations = div->GetLocations();
3666 Location first = locations->InAt(0);
3667 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003668
3669 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003670 case DataType::Type::kInt32:
3671 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003672 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003673 break;
3674 }
3675
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003676 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003677 if (second.IsFpuRegister()) {
3678 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3679 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3680 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003681 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003682 __ divss(first.AsFpuRegister<XmmRegister>(),
3683 codegen_->LiteralFloatAddress(
3684 const_area->GetConstant()->AsFloatConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003685 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003686 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3687 } else {
3688 DCHECK(second.IsStackSlot());
3689 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3690 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003691 break;
3692 }
3693
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003694 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003695 if (second.IsFpuRegister()) {
3696 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3697 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3698 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003699 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003700 __ divsd(first.AsFpuRegister<XmmRegister>(),
3701 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003702 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3703 const_area->GetBaseMethodAddress(),
3704 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003705 } else {
3706 DCHECK(second.IsDoubleStackSlot());
3707 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3708 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003709 break;
3710 }
3711
3712 default:
3713 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3714 }
3715}
3716
Calin Juravlebacfec32014-11-14 15:54:36 +00003717void LocationsBuilderX86::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003718 DataType::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003719
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003720 LocationSummary::CallKind call_kind = (rem->GetResultType() == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003721 ? LocationSummary::kCallOnMainOnly
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003722 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01003723 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003724
Calin Juravled2ec87d2014-12-08 14:24:46 +00003725 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003726 case DataType::Type::kInt32: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003727 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003728 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003729 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003730 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3731 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3732 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003733 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003734 locations->AddTemp(Location::RequiresRegister());
3735 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003736 break;
3737 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003738 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003739 InvokeRuntimeCallingConvention calling_convention;
3740 locations->SetInAt(0, Location::RegisterPairLocation(
3741 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3742 locations->SetInAt(1, Location::RegisterPairLocation(
3743 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3744 // Runtime helper puts the result in EAX, EDX.
3745 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3746 break;
3747 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003748 case DataType::Type::kFloat64:
3749 case DataType::Type::kFloat32: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003750 locations->SetInAt(0, Location::Any());
3751 locations->SetInAt(1, Location::Any());
3752 locations->SetOut(Location::RequiresFpuRegister());
3753 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003754 break;
3755 }
3756
3757 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003758 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003759 }
3760}
3761
3762void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003763 DataType::Type type = rem->GetResultType();
Calin Juravlebacfec32014-11-14 15:54:36 +00003764 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003765 case DataType::Type::kInt32:
3766 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003767 GenerateDivRemIntegral(rem);
3768 break;
3769 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003770 case DataType::Type::kFloat32:
3771 case DataType::Type::kFloat64: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003772 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003773 break;
3774 }
3775 default:
3776 LOG(FATAL) << "Unexpected rem type " << type;
3777 }
3778}
3779
Calin Juravled0d48522014-11-04 16:40:20 +00003780void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003781 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003782 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003783 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003784 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003785 case DataType::Type::kInt8:
3786 case DataType::Type::kUint16:
3787 case DataType::Type::kInt16:
3788 case DataType::Type::kInt32: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003789 locations->SetInAt(0, Location::Any());
3790 break;
3791 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003792 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003793 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
3794 if (!instruction->IsConstant()) {
3795 locations->AddTemp(Location::RequiresRegister());
3796 }
3797 break;
3798 }
3799 default:
3800 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3801 }
Calin Juravled0d48522014-11-04 16:40:20 +00003802}
3803
3804void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01003805 SlowPathCode* slow_path =
3806 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003807 codegen_->AddSlowPath(slow_path);
3808
3809 LocationSummary* locations = instruction->GetLocations();
3810 Location value = locations->InAt(0);
3811
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003812 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003813 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003814 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003815 case DataType::Type::kInt8:
3816 case DataType::Type::kUint16:
3817 case DataType::Type::kInt16:
3818 case DataType::Type::kInt32: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003819 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003820 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003821 __ j(kEqual, slow_path->GetEntryLabel());
3822 } else if (value.IsStackSlot()) {
3823 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
3824 __ j(kEqual, slow_path->GetEntryLabel());
3825 } else {
3826 DCHECK(value.IsConstant()) << value;
3827 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01003828 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003829 }
3830 }
3831 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003832 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003833 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003834 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003835 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003836 __ movl(temp, value.AsRegisterPairLow<Register>());
3837 __ orl(temp, value.AsRegisterPairHigh<Register>());
3838 __ j(kEqual, slow_path->GetEntryLabel());
3839 } else {
3840 DCHECK(value.IsConstant()) << value;
3841 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3842 __ jmp(slow_path->GetEntryLabel());
3843 }
3844 }
3845 break;
3846 }
3847 default:
3848 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003849 }
Calin Juravled0d48522014-11-04 16:40:20 +00003850}
3851
Calin Juravle9aec02f2014-11-18 23:06:35 +00003852void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3853 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3854
3855 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003856 new (GetGraph()->GetAllocator()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003857
3858 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003859 case DataType::Type::kInt32:
3860 case DataType::Type::kInt64: {
Mark P Mendell73945692015-04-29 14:56:17 +00003861 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003862 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003863 // The shift count needs to be in CL or a constant.
3864 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003865 locations->SetOut(Location::SameAsFirstInput());
3866 break;
3867 }
3868 default:
3869 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3870 }
3871}
3872
3873void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3874 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3875
3876 LocationSummary* locations = op->GetLocations();
3877 Location first = locations->InAt(0);
3878 Location second = locations->InAt(1);
3879 DCHECK(first.Equals(locations->Out()));
3880
3881 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003882 case DataType::Type::kInt32: {
Mark P Mendell73945692015-04-29 14:56:17 +00003883 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003884 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003885 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003886 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003887 DCHECK_EQ(ECX, second_reg);
3888 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003889 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003890 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003891 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003892 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003893 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003894 }
3895 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003896 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003897 if (shift == 0) {
3898 return;
3899 }
3900 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003901 if (op->IsShl()) {
3902 __ shll(first_reg, imm);
3903 } else if (op->IsShr()) {
3904 __ sarl(first_reg, imm);
3905 } else {
3906 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003907 }
3908 }
3909 break;
3910 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003911 case DataType::Type::kInt64: {
Mark P Mendell73945692015-04-29 14:56:17 +00003912 if (second.IsRegister()) {
3913 Register second_reg = second.AsRegister<Register>();
3914 DCHECK_EQ(ECX, second_reg);
3915 if (op->IsShl()) {
3916 GenerateShlLong(first, second_reg);
3917 } else if (op->IsShr()) {
3918 GenerateShrLong(first, second_reg);
3919 } else {
3920 GenerateUShrLong(first, second_reg);
3921 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003922 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003923 // Shift by a constant.
Roland Levillain5b5b9312016-03-22 14:57:31 +00003924 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003925 // Nothing to do if the shift is 0, as the input is already the output.
3926 if (shift != 0) {
3927 if (op->IsShl()) {
3928 GenerateShlLong(first, shift);
3929 } else if (op->IsShr()) {
3930 GenerateShrLong(first, shift);
3931 } else {
3932 GenerateUShrLong(first, shift);
3933 }
3934 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003935 }
3936 break;
3937 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003938 default:
3939 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3940 }
3941}
3942
Mark P Mendell73945692015-04-29 14:56:17 +00003943void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3944 Register low = loc.AsRegisterPairLow<Register>();
3945 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003946 if (shift == 1) {
3947 // This is just an addition.
3948 __ addl(low, low);
3949 __ adcl(high, high);
3950 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003951 // Shift by 32 is easy. High gets low, and low gets 0.
3952 codegen_->EmitParallelMoves(
3953 loc.ToLow(),
3954 loc.ToHigh(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003955 DataType::Type::kInt32,
Mark P Mendell73945692015-04-29 14:56:17 +00003956 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3957 loc.ToLow(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003958 DataType::Type::kInt32);
Mark P Mendell73945692015-04-29 14:56:17 +00003959 } else if (shift > 32) {
3960 // Low part becomes 0. High part is low part << (shift-32).
3961 __ movl(high, low);
3962 __ shll(high, Immediate(shift - 32));
3963 __ xorl(low, low);
3964 } else {
3965 // Between 1 and 31.
3966 __ shld(high, low, Immediate(shift));
3967 __ shll(low, Immediate(shift));
3968 }
3969}
3970
Calin Juravle9aec02f2014-11-18 23:06:35 +00003971void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003972 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003973 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
3974 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
3975 __ testl(shifter, Immediate(32));
3976 __ j(kEqual, &done);
3977 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
3978 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
3979 __ Bind(&done);
3980}
3981
Mark P Mendell73945692015-04-29 14:56:17 +00003982void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
3983 Register low = loc.AsRegisterPairLow<Register>();
3984 Register high = loc.AsRegisterPairHigh<Register>();
3985 if (shift == 32) {
3986 // Need to copy the sign.
3987 DCHECK_NE(low, high);
3988 __ movl(low, high);
3989 __ sarl(high, Immediate(31));
3990 } else if (shift > 32) {
3991 DCHECK_NE(low, high);
3992 // High part becomes sign. Low part is shifted by shift - 32.
3993 __ movl(low, high);
3994 __ sarl(high, Immediate(31));
3995 __ sarl(low, Immediate(shift - 32));
3996 } else {
3997 // Between 1 and 31.
3998 __ shrd(low, high, Immediate(shift));
3999 __ sarl(high, Immediate(shift));
4000 }
4001}
4002
Calin Juravle9aec02f2014-11-18 23:06:35 +00004003void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004004 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004005 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4006 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
4007 __ testl(shifter, Immediate(32));
4008 __ j(kEqual, &done);
4009 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4010 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
4011 __ Bind(&done);
4012}
4013
Mark P Mendell73945692015-04-29 14:56:17 +00004014void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
4015 Register low = loc.AsRegisterPairLow<Register>();
4016 Register high = loc.AsRegisterPairHigh<Register>();
4017 if (shift == 32) {
4018 // Shift by 32 is easy. Low gets high, and high gets 0.
4019 codegen_->EmitParallelMoves(
4020 loc.ToHigh(),
4021 loc.ToLow(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004022 DataType::Type::kInt32,
Mark P Mendell73945692015-04-29 14:56:17 +00004023 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
4024 loc.ToHigh(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004025 DataType::Type::kInt32);
Mark P Mendell73945692015-04-29 14:56:17 +00004026 } else if (shift > 32) {
4027 // Low part is high >> (shift - 32). High part becomes 0.
4028 __ movl(low, high);
4029 __ shrl(low, Immediate(shift - 32));
4030 __ xorl(high, high);
4031 } else {
4032 // Between 1 and 31.
4033 __ shrd(low, high, Immediate(shift));
4034 __ shrl(high, Immediate(shift));
4035 }
4036}
4037
Calin Juravle9aec02f2014-11-18 23:06:35 +00004038void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004039 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004040 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4041 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
4042 __ testl(shifter, Immediate(32));
4043 __ j(kEqual, &done);
4044 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4045 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
4046 __ Bind(&done);
4047}
4048
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004049void LocationsBuilderX86::VisitRor(HRor* ror) {
4050 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004051 new (GetGraph()->GetAllocator()) LocationSummary(ror, LocationSummary::kNoCall);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004052
4053 switch (ror->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004054 case DataType::Type::kInt64:
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004055 // Add the temporary needed.
4056 locations->AddTemp(Location::RequiresRegister());
4057 FALLTHROUGH_INTENDED;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004058 case DataType::Type::kInt32:
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004059 locations->SetInAt(0, Location::RequiresRegister());
4060 // The shift count needs to be in CL (unless it is a constant).
4061 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
4062 locations->SetOut(Location::SameAsFirstInput());
4063 break;
4064 default:
4065 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4066 UNREACHABLE();
4067 }
4068}
4069
4070void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
4071 LocationSummary* locations = ror->GetLocations();
4072 Location first = locations->InAt(0);
4073 Location second = locations->InAt(1);
4074
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004075 if (ror->GetResultType() == DataType::Type::kInt32) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004076 Register first_reg = first.AsRegister<Register>();
4077 if (second.IsRegister()) {
4078 Register second_reg = second.AsRegister<Register>();
4079 __ rorl(first_reg, second_reg);
4080 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004081 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004082 __ rorl(first_reg, imm);
4083 }
4084 return;
4085 }
4086
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004087 DCHECK_EQ(ror->GetResultType(), DataType::Type::kInt64);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004088 Register first_reg_lo = first.AsRegisterPairLow<Register>();
4089 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
4090 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
4091 if (second.IsRegister()) {
4092 Register second_reg = second.AsRegister<Register>();
4093 DCHECK_EQ(second_reg, ECX);
4094 __ movl(temp_reg, first_reg_hi);
4095 __ shrd(first_reg_hi, first_reg_lo, second_reg);
4096 __ shrd(first_reg_lo, temp_reg, second_reg);
4097 __ movl(temp_reg, first_reg_hi);
4098 __ testl(second_reg, Immediate(32));
4099 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
4100 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
4101 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004102 int32_t shift_amt = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004103 if (shift_amt == 0) {
4104 // Already fine.
4105 return;
4106 }
4107 if (shift_amt == 32) {
4108 // Just swap.
4109 __ movl(temp_reg, first_reg_lo);
4110 __ movl(first_reg_lo, first_reg_hi);
4111 __ movl(first_reg_hi, temp_reg);
4112 return;
4113 }
4114
4115 Immediate imm(shift_amt);
4116 // Save the constents of the low value.
4117 __ movl(temp_reg, first_reg_lo);
4118
4119 // Shift right into low, feeding bits from high.
4120 __ shrd(first_reg_lo, first_reg_hi, imm);
4121
4122 // Shift right into high, feeding bits from the original low.
4123 __ shrd(first_reg_hi, temp_reg, imm);
4124
4125 // Swap if needed.
4126 if (shift_amt > 32) {
4127 __ movl(temp_reg, first_reg_lo);
4128 __ movl(first_reg_lo, first_reg_hi);
4129 __ movl(first_reg_hi, temp_reg);
4130 }
4131 }
4132}
4133
Calin Juravle9aec02f2014-11-18 23:06:35 +00004134void LocationsBuilderX86::VisitShl(HShl* shl) {
4135 HandleShift(shl);
4136}
4137
4138void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
4139 HandleShift(shl);
4140}
4141
4142void LocationsBuilderX86::VisitShr(HShr* shr) {
4143 HandleShift(shr);
4144}
4145
4146void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
4147 HandleShift(shr);
4148}
4149
4150void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
4151 HandleShift(ushr);
4152}
4153
4154void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
4155 HandleShift(ushr);
4156}
4157
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004158void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004159 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
4160 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004161 locations->SetOut(Location::RegisterLocation(EAX));
David Brazdil6de19382016-01-08 17:37:10 +00004162 if (instruction->IsStringAlloc()) {
4163 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4164 } else {
4165 InvokeRuntimeCallingConvention calling_convention;
4166 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00004167 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004168}
4169
4170void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004171 // Note: if heap poisoning is enabled, the entry point takes cares
4172 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004173 if (instruction->IsStringAlloc()) {
4174 // String is allocated through StringFactory. Call NewEmptyString entry point.
4175 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07004176 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004177 __ fs()->movl(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString)));
4178 __ call(Address(temp, code_offset.Int32Value()));
4179 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4180 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01004181 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00004182 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00004183 DCHECK(!codegen_->IsLeafMethod());
4184 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004185}
4186
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004187void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004188 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
4189 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004190 locations->SetOut(Location::RegisterLocation(EAX));
4191 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004192 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4193 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004194}
4195
4196void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004197 // Note: if heap poisoning is enabled, the entry point takes cares
4198 // of poisoning the reference.
Nicolas Geoffrayd0958442017-01-30 14:57:16 +00004199 QuickEntrypointEnum entrypoint =
4200 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
4201 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004202 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004203 DCHECK(!codegen_->IsLeafMethod());
4204}
4205
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004206void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004207 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004208 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004209 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4210 if (location.IsStackSlot()) {
4211 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4212 } else if (location.IsDoubleStackSlot()) {
4213 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004214 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004215 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004216}
4217
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004218void InstructionCodeGeneratorX86::VisitParameterValue(
4219 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4220}
4221
4222void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
4223 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004224 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004225 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4226}
4227
4228void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004229}
4230
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004231void LocationsBuilderX86::VisitClassTableGet(HClassTableGet* instruction) {
4232 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004233 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004234 locations->SetInAt(0, Location::RequiresRegister());
4235 locations->SetOut(Location::RequiresRegister());
4236}
4237
4238void InstructionCodeGeneratorX86::VisitClassTableGet(HClassTableGet* instruction) {
4239 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004240 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004241 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004242 instruction->GetIndex(), kX86PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004243 __ movl(locations->Out().AsRegister<Register>(),
4244 Address(locations->InAt(0).AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004245 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004246 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004247 instruction->GetIndex(), kX86PointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004248 __ movl(locations->Out().AsRegister<Register>(),
4249 Address(locations->InAt(0).AsRegister<Register>(),
4250 mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
4251 // temp = temp->GetImtEntryAt(method_offset);
4252 __ movl(locations->Out().AsRegister<Register>(),
4253 Address(locations->Out().AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004254 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004255}
4256
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004257void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004258 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004259 new (GetGraph()->GetAllocator()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004260 locations->SetInAt(0, Location::RequiresRegister());
4261 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004262}
4263
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004264void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
4265 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01004266 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004267 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01004268 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004269 switch (not_->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004270 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004271 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004272 break;
4273
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004274 case DataType::Type::kInt64:
Roland Levillain70566432014-10-24 16:20:17 +01004275 __ notl(out.AsRegisterPairLow<Register>());
4276 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004277 break;
4278
4279 default:
4280 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4281 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004282}
4283
David Brazdil66d126e2015-04-03 16:02:44 +01004284void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
4285 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004286 new (GetGraph()->GetAllocator()) LocationSummary(bool_not, LocationSummary::kNoCall);
David Brazdil66d126e2015-04-03 16:02:44 +01004287 locations->SetInAt(0, Location::RequiresRegister());
4288 locations->SetOut(Location::SameAsFirstInput());
4289}
4290
4291void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004292 LocationSummary* locations = bool_not->GetLocations();
4293 Location in = locations->InAt(0);
4294 Location out = locations->Out();
4295 DCHECK(in.Equals(out));
4296 __ xorl(out.AsRegister<Register>(), Immediate(1));
4297}
4298
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004299void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004300 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004301 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004302 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004303 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004304 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004305 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004306 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004307 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004308 case DataType::Type::kInt32:
4309 case DataType::Type::kInt64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00004310 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00004311 locations->SetInAt(1, Location::Any());
4312 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4313 break;
4314 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004315 case DataType::Type::kFloat32:
4316 case DataType::Type::kFloat64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00004317 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004318 if (compare->InputAt(1)->IsX86LoadFromConstantTable()) {
4319 DCHECK(compare->InputAt(1)->IsEmittedAtUseSite());
4320 } else if (compare->InputAt(1)->IsConstant()) {
4321 locations->SetInAt(1, Location::RequiresFpuRegister());
4322 } else {
4323 locations->SetInAt(1, Location::Any());
4324 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004325 locations->SetOut(Location::RequiresRegister());
4326 break;
4327 }
4328 default:
4329 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4330 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004331}
4332
4333void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004334 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004335 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004336 Location left = locations->InAt(0);
4337 Location right = locations->InAt(1);
4338
Mark Mendell0c9497d2015-08-21 09:30:05 -04004339 NearLabel less, greater, done;
Aart Bika19616e2016-02-01 18:57:58 -08004340 Condition less_cond = kLess;
4341
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004342 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004343 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004344 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004345 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004346 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004347 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004348 case DataType::Type::kInt32: {
Roland Levillain0b671c02016-08-19 12:02:34 +01004349 codegen_->GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08004350 break;
4351 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004352 case DataType::Type::kInt64: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004353 Register left_low = left.AsRegisterPairLow<Register>();
4354 Register left_high = left.AsRegisterPairHigh<Register>();
4355 int32_t val_low = 0;
4356 int32_t val_high = 0;
4357 bool right_is_const = false;
4358
4359 if (right.IsConstant()) {
4360 DCHECK(right.GetConstant()->IsLongConstant());
4361 right_is_const = true;
4362 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
4363 val_low = Low32Bits(val);
4364 val_high = High32Bits(val);
4365 }
4366
Calin Juravleddb7df22014-11-25 20:56:51 +00004367 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004368 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004369 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004370 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004371 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004372 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004373 codegen_->Compare32BitValue(left_high, val_high);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004374 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004375 __ j(kLess, &less); // Signed compare.
4376 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004377 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004378 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004379 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004380 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004381 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004382 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004383 codegen_->Compare32BitValue(left_low, val_low);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004384 }
Aart Bika19616e2016-02-01 18:57:58 -08004385 less_cond = kBelow; // for CF (unsigned).
Calin Juravleddb7df22014-11-25 20:56:51 +00004386 break;
4387 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004388 case DataType::Type::kFloat32: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004389 GenerateFPCompare(left, right, compare, false);
Calin Juravleddb7df22014-11-25 20:56:51 +00004390 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004391 less_cond = kBelow; // for CF (floats).
Calin Juravleddb7df22014-11-25 20:56:51 +00004392 break;
4393 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004394 case DataType::Type::kFloat64: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004395 GenerateFPCompare(left, right, compare, true);
Calin Juravleddb7df22014-11-25 20:56:51 +00004396 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004397 less_cond = kBelow; // for CF (floats).
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004398 break;
4399 }
4400 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004401 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004402 }
Aart Bika19616e2016-02-01 18:57:58 -08004403
Calin Juravleddb7df22014-11-25 20:56:51 +00004404 __ movl(out, Immediate(0));
4405 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08004406 __ j(less_cond, &less);
Calin Juravleddb7df22014-11-25 20:56:51 +00004407
4408 __ Bind(&greater);
4409 __ movl(out, Immediate(1));
4410 __ jmp(&done);
4411
4412 __ Bind(&less);
4413 __ movl(out, Immediate(-1));
4414
4415 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004416}
4417
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004418void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004419 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004420 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004421 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004422 locations->SetInAt(i, Location::Any());
4423 }
4424 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004425}
4426
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004427void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004428 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004429}
4430
Roland Levillain7c1559a2015-12-15 10:55:36 +00004431void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004432 /*
4433 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
4434 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
4435 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4436 */
4437 switch (kind) {
4438 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004439 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004440 break;
4441 }
4442 case MemBarrierKind::kAnyStore:
4443 case MemBarrierKind::kLoadAny:
4444 case MemBarrierKind::kStoreStore: {
4445 // nop
4446 break;
4447 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004448 case MemBarrierKind::kNTStoreStore:
4449 // Non-Temporal Store/Store needs an explicit fence.
4450 MemoryFence(/* non-temporal */ true);
4451 break;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004452 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004453}
4454
Vladimir Markodc151b22015-10-15 18:02:30 +01004455HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
4456 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004457 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004458 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004459}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004460
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004461Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4462 Register temp) {
4463 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Markoc53c0792015-11-19 15:48:33 +00004464 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004465 if (!invoke->GetLocations()->Intrinsified()) {
4466 return location.AsRegister<Register>();
4467 }
4468 // For intrinsics we allow any location, so it may be on the stack.
4469 if (!location.IsRegister()) {
4470 __ movl(temp, Address(ESP, location.GetStackIndex()));
4471 return temp;
4472 }
4473 // For register locations, check if the register was saved. If so, get it from the stack.
4474 // Note: There is a chance that the register was saved but not overwritten, so we could
4475 // save one load. However, since this is just an intrinsic slow path we prefer this
4476 // simple and more robust approach rather that trying to determine if that's the case.
4477 SlowPathCode* slow_path = GetCurrentSlowPath();
Vladimir Marko4ee8e292017-06-02 15:39:30 +00004478 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
4479 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4480 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4481 __ movl(temp, Address(ESP, stack_offset));
4482 return temp;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004483 }
4484 return location.AsRegister<Register>();
4485}
4486
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004487void CodeGeneratorX86::GenerateStaticOrDirectCall(
4488 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Vladimir Marko58155012015-08-19 12:49:41 +00004489 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4490 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004491 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00004492 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004493 uint32_t offset =
4494 GetThreadOffset<kX86PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
4495 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004496 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004497 }
Vladimir Marko58155012015-08-19 12:49:41 +00004498 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004499 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004500 break;
Vladimir Marko65979462017-05-19 17:25:12 +01004501 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
4502 DCHECK(GetCompilerOptions().IsBootImage());
4503 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4504 temp.AsRegister<Register>());
4505 __ leal(temp.AsRegister<Register>(), Address(base_reg, CodeGeneratorX86::kDummy32BitOffset));
4506 RecordBootMethodPatch(invoke);
4507 break;
4508 }
Vladimir Marko58155012015-08-19 12:49:41 +00004509 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4510 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
4511 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004512 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004513 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4514 temp.AsRegister<Register>());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004515 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004516 // Bind a new fixup label at the end of the "movl" insn.
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004517 __ Bind(NewMethodBssEntryPatch(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004518 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress(),
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004519 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex())));
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004520 break;
4521 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004522 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
4523 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
4524 return; // No code pointer retrieval; the runtime performs the call directly.
Vladimir Marko9b688a02015-05-06 14:12:42 +01004525 }
Vladimir Marko58155012015-08-19 12:49:41 +00004526 }
4527
4528 switch (invoke->GetCodePtrLocation()) {
4529 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4530 __ call(GetFrameEntryLabel());
4531 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004532 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4533 // (callee_method + offset_of_quick_compiled_code)()
4534 __ call(Address(callee_method.AsRegister<Register>(),
4535 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07004536 kX86PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004537 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04004538 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004539 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Mark Mendell09ed1a32015-03-25 08:30:06 -04004540
4541 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04004542}
4543
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004544void CodeGeneratorX86::GenerateVirtualCall(
4545 HInvokeVirtual* invoke, Location temp_in, SlowPathCode* slow_path) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004546 Register temp = temp_in.AsRegister<Register>();
4547 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4548 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004549
4550 // Use the calling convention instead of the location of the receiver, as
4551 // intrinsics may have put the receiver in a different register. In the intrinsics
4552 // slow path, the arguments have been moved to the right place, so here we are
4553 // guaranteed that the receiver is the first register of the calling convention.
4554 InvokeDexCallingConvention calling_convention;
4555 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004556 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004557 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004558 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004559 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004560 // Instead of simply (possibly) unpoisoning `temp` here, we should
4561 // emit a read barrier for the previous class reference load.
4562 // However this is not required in practice, as this is an
4563 // intermediate/temporary reference and because the current
4564 // concurrent copying collector keeps the from-space memory
4565 // intact/accessible until the end of the marking phase (the
4566 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004567 __ MaybeUnpoisonHeapReference(temp);
4568 // temp = temp->GetMethodAt(method_offset);
4569 __ movl(temp, Address(temp, method_offset));
4570 // call temp->GetEntryPoint();
4571 __ call(Address(
Andreas Gampe542451c2016-07-26 09:02:02 -07004572 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004573 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004574}
4575
Vladimir Marko65979462017-05-19 17:25:12 +01004576void CodeGeneratorX86::RecordBootMethodPatch(HInvokeStaticOrDirect* invoke) {
4577 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
4578 HX86ComputeBaseMethodAddress* address =
4579 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
4580 boot_image_method_patches_.emplace_back(address,
4581 *invoke->GetTargetMethod().dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07004582 invoke->GetTargetMethod().index);
Vladimir Marko65979462017-05-19 17:25:12 +01004583 __ Bind(&boot_image_method_patches_.back().label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004584}
4585
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004586Label* CodeGeneratorX86::NewMethodBssEntryPatch(
4587 HX86ComputeBaseMethodAddress* method_address,
4588 MethodReference target_method) {
4589 // Add the patch entry and bind its label at the end of the instruction.
4590 method_bss_entry_patches_.emplace_back(method_address,
4591 *target_method.dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07004592 target_method.index);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004593 return &method_bss_entry_patches_.back().label;
4594}
4595
Vladimir Marko1998cd02017-01-13 13:02:58 +00004596void CodeGeneratorX86::RecordBootTypePatch(HLoadClass* load_class) {
Vladimir Marko00916b92017-05-17 17:45:45 +01004597 HX86ComputeBaseMethodAddress* address = load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004598 boot_image_type_patches_.emplace_back(address,
4599 load_class->GetDexFile(),
Vladimir Marko1998cd02017-01-13 13:02:58 +00004600 load_class->GetTypeIndex().index_);
4601 __ Bind(&boot_image_type_patches_.back().label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004602}
4603
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004604Label* CodeGeneratorX86::NewTypeBssEntryPatch(HLoadClass* load_class) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004605 HX86ComputeBaseMethodAddress* address =
4606 load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
4607 type_bss_entry_patches_.emplace_back(
4608 address, load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00004609 return &type_bss_entry_patches_.back().label;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004610}
4611
Vladimir Marko65979462017-05-19 17:25:12 +01004612void CodeGeneratorX86::RecordBootStringPatch(HLoadString* load_string) {
Vladimir Marko65979462017-05-19 17:25:12 +01004613 HX86ComputeBaseMethodAddress* address = load_string->InputAt(0)->AsX86ComputeBaseMethodAddress();
4614 string_patches_.emplace_back(address,
4615 load_string->GetDexFile(),
4616 load_string->GetStringIndex().index_);
4617 __ Bind(&string_patches_.back().label);
4618}
4619
Vladimir Markoaad75c62016-10-03 08:46:48 +00004620Label* CodeGeneratorX86::NewStringBssEntryPatch(HLoadString* load_string) {
4621 DCHECK(!GetCompilerOptions().IsBootImage());
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004622 HX86ComputeBaseMethodAddress* address =
4623 load_string->InputAt(0)->AsX86ComputeBaseMethodAddress();
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004624 string_bss_entry_patches_.emplace_back(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004625 address, load_string->GetDexFile(), load_string->GetStringIndex().index_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004626 return &string_bss_entry_patches_.back().label;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004627}
4628
Vladimir Markoaad75c62016-10-03 08:46:48 +00004629// The label points to the end of the "movl" or another instruction but the literal offset
4630// for method patch needs to point to the embedded constant which occupies the last 4 bytes.
4631constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
4632
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004633template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +00004634inline void CodeGeneratorX86::EmitPcRelativeLinkerPatches(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004635 const ArenaDeque<X86PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004636 ArenaVector<linker::LinkerPatch>* linker_patches) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004637 for (const X86PcRelativePatchInfo& info : infos) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00004638 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004639 linker_patches->push_back(Factory(
4640 literal_offset, &info.dex_file, GetMethodAddressOffset(info.method_address), info.index));
Vladimir Markoaad75c62016-10-03 08:46:48 +00004641 }
4642}
4643
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004644void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Marko58155012015-08-19 12:49:41 +00004645 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004646 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01004647 boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004648 method_bss_entry_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +00004649 boot_image_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01004650 type_bss_entry_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004651 string_patches_.size() +
4652 string_bss_entry_patches_.size();
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004653 linker_patches->reserve(size);
Vladimir Marko764d4542017-05-16 10:31:41 +01004654 if (GetCompilerOptions().IsBootImage()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004655 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
4656 boot_image_method_patches_, linker_patches);
4657 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
4658 boot_image_type_patches_, linker_patches);
4659 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
4660 string_patches_, linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004661 } else {
Vladimir Marko65979462017-05-19 17:25:12 +01004662 DCHECK(boot_image_method_patches_.empty());
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004663 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeClassTablePatch>(
4664 boot_image_type_patches_, linker_patches);
4665 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringInternTablePatch>(
4666 string_patches_, linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004667 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004668 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
4669 method_bss_entry_patches_, linker_patches);
4670 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
4671 type_bss_entry_patches_, linker_patches);
4672 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
4673 string_bss_entry_patches_, linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00004674 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00004675}
4676
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004677void CodeGeneratorX86::MarkGCCard(Register temp,
4678 Register card,
4679 Register object,
4680 Register value,
4681 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004682 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004683 if (value_can_be_null) {
4684 __ testl(value, value);
4685 __ j(kEqual, &is_null);
4686 }
Andreas Gampe542451c2016-07-26 09:02:02 -07004687 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86PointerSize>().Int32Value()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004688 __ movl(temp, object);
4689 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004690 __ movb(Address(temp, card, TIMES_1, 0),
4691 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004692 if (value_can_be_null) {
4693 __ Bind(&is_null);
4694 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004695}
4696
Calin Juravle52c48962014-12-16 17:02:57 +00004697void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4698 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00004699
4700 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004701 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004702 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004703 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
4704 kEmitCompilerReadBarrier
4705 ? LocationSummary::kCallOnSlowPath
4706 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004707 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004708 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004709 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004710 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004711
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004712 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004713 locations->SetOut(Location::RequiresFpuRegister());
4714 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004715 // The output overlaps in case of long: we don't want the low move
4716 // to overwrite the object's location. Likewise, in the case of
4717 // an object field get with read barriers enabled, we do not want
4718 // the move to overwrite the object's location, as we need it to emit
4719 // the read barrier.
4720 locations->SetOut(
4721 Location::RequiresRegister(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004722 (object_field_get_with_read_barrier || instruction->GetType() == DataType::Type::kInt64) ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00004723 Location::kOutputOverlap :
4724 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004725 }
Calin Juravle52c48962014-12-16 17:02:57 +00004726
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004727 if (field_info.IsVolatile() && (field_info.GetFieldType() == DataType::Type::kInt64)) {
Calin Juravle52c48962014-12-16 17:02:57 +00004728 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00004729 // So we use an XMM register as a temp to achieve atomicity (first
4730 // load the temp into the XMM and then copy the XMM into the
4731 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00004732 locations->AddTemp(Location::RequiresFpuRegister());
4733 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004734}
4735
Calin Juravle52c48962014-12-16 17:02:57 +00004736void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
4737 const FieldInfo& field_info) {
4738 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004739
Calin Juravle52c48962014-12-16 17:02:57 +00004740 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004741 Location base_loc = locations->InAt(0);
4742 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004743 Location out = locations->Out();
4744 bool is_volatile = field_info.IsVolatile();
Vladimir Marko61b92282017-10-11 13:23:17 +01004745 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
4746 DataType::Type load_type = instruction->GetType();
Calin Juravle52c48962014-12-16 17:02:57 +00004747 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4748
Vladimir Marko61b92282017-10-11 13:23:17 +01004749 switch (load_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004750 case DataType::Type::kBool:
4751 case DataType::Type::kUint8: {
Calin Juravle52c48962014-12-16 17:02:57 +00004752 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004753 break;
4754 }
4755
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004756 case DataType::Type::kInt8: {
Calin Juravle52c48962014-12-16 17:02:57 +00004757 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004758 break;
4759 }
4760
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004761 case DataType::Type::kUint16: {
4762 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004763 break;
4764 }
4765
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004766 case DataType::Type::kInt16: {
4767 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004768 break;
4769 }
4770
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004771 case DataType::Type::kInt32:
Calin Juravle52c48962014-12-16 17:02:57 +00004772 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004773 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00004774
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004775 case DataType::Type::kReference: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004776 // /* HeapReference<Object> */ out = *(base + offset)
4777 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004778 // Note that a potential implicit null check is handled in this
4779 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4780 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00004781 instruction, out, base, offset, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00004782 if (is_volatile) {
4783 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4784 }
4785 } else {
4786 __ movl(out.AsRegister<Register>(), Address(base, offset));
4787 codegen_->MaybeRecordImplicitNullCheck(instruction);
4788 if (is_volatile) {
4789 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4790 }
4791 // If read barriers are enabled, emit read barriers other than
4792 // Baker's using a slow path (and also unpoison the loaded
4793 // reference, if heap poisoning is enabled).
4794 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4795 }
4796 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004797 }
4798
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004799 case DataType::Type::kInt64: {
Calin Juravle52c48962014-12-16 17:02:57 +00004800 if (is_volatile) {
4801 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4802 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004803 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004804 __ movd(out.AsRegisterPairLow<Register>(), temp);
4805 __ psrlq(temp, Immediate(32));
4806 __ movd(out.AsRegisterPairHigh<Register>(), temp);
4807 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004808 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00004809 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004810 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004811 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
4812 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004813 break;
4814 }
4815
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004816 case DataType::Type::kFloat32: {
Calin Juravle52c48962014-12-16 17:02:57 +00004817 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004818 break;
4819 }
4820
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004821 case DataType::Type::kFloat64: {
Calin Juravle52c48962014-12-16 17:02:57 +00004822 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004823 break;
4824 }
4825
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004826 case DataType::Type::kVoid:
Vladimir Marko61b92282017-10-11 13:23:17 +01004827 LOG(FATAL) << "Unreachable type " << load_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004828 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004829 }
Calin Juravle52c48962014-12-16 17:02:57 +00004830
Vladimir Marko61b92282017-10-11 13:23:17 +01004831 if (load_type == DataType::Type::kReference || load_type == DataType::Type::kInt64) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004832 // Potential implicit null checks, in the case of reference or
4833 // long fields, are handled in the previous switch statement.
4834 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004835 codegen_->MaybeRecordImplicitNullCheck(instruction);
4836 }
4837
Calin Juravle52c48962014-12-16 17:02:57 +00004838 if (is_volatile) {
Vladimir Marko61b92282017-10-11 13:23:17 +01004839 if (load_type == DataType::Type::kReference) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004840 // Memory barriers, in the case of references, are also handled
4841 // in the previous switch statement.
4842 } else {
4843 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4844 }
Roland Levillain4d027112015-07-01 15:41:14 +01004845 }
Calin Juravle52c48962014-12-16 17:02:57 +00004846}
4847
4848void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4849 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4850
4851 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004852 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00004853 locations->SetInAt(0, Location::RequiresRegister());
4854 bool is_volatile = field_info.IsVolatile();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004855 DataType::Type field_type = field_info.GetFieldType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004856 bool is_byte_type = DataType::Size(field_type) == 1u;
Calin Juravle52c48962014-12-16 17:02:57 +00004857
4858 // The register allocator does not support multiple
4859 // inputs that die at entry with one in a specific register.
4860 if (is_byte_type) {
4861 // Ensure the value is in a byte register.
4862 locations->SetInAt(1, Location::RegisterLocation(EAX));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004863 } else if (DataType::IsFloatingPointType(field_type)) {
4864 if (is_volatile && field_type == DataType::Type::kFloat64) {
Mark Mendell81489372015-11-04 11:30:41 -05004865 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4866 locations->SetInAt(1, Location::RequiresFpuRegister());
4867 } else {
4868 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4869 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004870 } else if (is_volatile && field_type == DataType::Type::kInt64) {
Mark Mendell81489372015-11-04 11:30:41 -05004871 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00004872 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004873
Calin Juravle52c48962014-12-16 17:02:57 +00004874 // 64bits value can be atomically written to an address with movsd and an XMM register.
4875 // We need two XMM registers because there's no easier way to (bit) copy a register pair
4876 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
4877 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
4878 // isolated cases when we need this it isn't worth adding the extra complexity.
4879 locations->AddTemp(Location::RequiresFpuRegister());
4880 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004881 } else {
4882 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4883
4884 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4885 // Temporary registers for the write barrier.
4886 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
4887 // Ensure the card is in a byte register.
4888 locations->AddTemp(Location::RegisterLocation(ECX));
4889 }
Calin Juravle52c48962014-12-16 17:02:57 +00004890 }
4891}
4892
4893void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004894 const FieldInfo& field_info,
4895 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004896 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4897
4898 LocationSummary* locations = instruction->GetLocations();
4899 Register base = locations->InAt(0).AsRegister<Register>();
4900 Location value = locations->InAt(1);
4901 bool is_volatile = field_info.IsVolatile();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004902 DataType::Type field_type = field_info.GetFieldType();
Calin Juravle52c48962014-12-16 17:02:57 +00004903 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004904 bool needs_write_barrier =
4905 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004906
4907 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004908 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004909 }
4910
Mark Mendell81489372015-11-04 11:30:41 -05004911 bool maybe_record_implicit_null_check_done = false;
4912
Calin Juravle52c48962014-12-16 17:02:57 +00004913 switch (field_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004914 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004915 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004916 case DataType::Type::kInt8: {
Calin Juravle52c48962014-12-16 17:02:57 +00004917 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
4918 break;
4919 }
4920
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004921 case DataType::Type::kUint16:
4922 case DataType::Type::kInt16: {
Mark Mendell81489372015-11-04 11:30:41 -05004923 if (value.IsConstant()) {
Nicolas Geoffray78612082017-07-24 14:18:53 +01004924 __ movw(Address(base, offset),
4925 Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Mark Mendell81489372015-11-04 11:30:41 -05004926 } else {
4927 __ movw(Address(base, offset), value.AsRegister<Register>());
4928 }
Calin Juravle52c48962014-12-16 17:02:57 +00004929 break;
4930 }
4931
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004932 case DataType::Type::kInt32:
4933 case DataType::Type::kReference: {
Roland Levillain4d027112015-07-01 15:41:14 +01004934 if (kPoisonHeapReferences && needs_write_barrier) {
4935 // Note that in the case where `value` is a null reference,
4936 // we do not enter this block, as the reference does not
4937 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004938 DCHECK_EQ(field_type, DataType::Type::kReference);
Roland Levillain4d027112015-07-01 15:41:14 +01004939 Register temp = locations->GetTemp(0).AsRegister<Register>();
4940 __ movl(temp, value.AsRegister<Register>());
4941 __ PoisonHeapReference(temp);
4942 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05004943 } else if (value.IsConstant()) {
4944 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4945 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01004946 } else {
Nicolas Geoffray03971632016-03-17 10:44:24 +00004947 DCHECK(value.IsRegister()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01004948 __ movl(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::kInt64: {
Calin Juravle52c48962014-12-16 17:02:57 +00004954 if (is_volatile) {
4955 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4956 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
4957 __ movd(temp1, value.AsRegisterPairLow<Register>());
4958 __ movd(temp2, value.AsRegisterPairHigh<Register>());
4959 __ punpckldq(temp1, temp2);
4960 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00004961 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05004962 } else if (value.IsConstant()) {
4963 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4964 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4965 codegen_->MaybeRecordImplicitNullCheck(instruction);
4966 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00004967 } else {
4968 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004969 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004970 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
4971 }
Mark Mendell81489372015-11-04 11:30:41 -05004972 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00004973 break;
4974 }
4975
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004976 case DataType::Type::kFloat32: {
Mark Mendell81489372015-11-04 11:30:41 -05004977 if (value.IsConstant()) {
4978 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4979 __ movl(Address(base, offset), Immediate(v));
4980 } else {
4981 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4982 }
Calin Juravle52c48962014-12-16 17:02:57 +00004983 break;
4984 }
4985
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004986 case DataType::Type::kFloat64: {
Mark Mendell81489372015-11-04 11:30:41 -05004987 if (value.IsConstant()) {
4988 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4989 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4990 codegen_->MaybeRecordImplicitNullCheck(instruction);
4991 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
4992 maybe_record_implicit_null_check_done = true;
4993 } else {
4994 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4995 }
Calin Juravle52c48962014-12-16 17:02:57 +00004996 break;
4997 }
4998
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004999 case DataType::Type::kVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00005000 LOG(FATAL) << "Unreachable type " << field_type;
5001 UNREACHABLE();
5002 }
5003
Mark Mendell81489372015-11-04 11:30:41 -05005004 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005005 codegen_->MaybeRecordImplicitNullCheck(instruction);
5006 }
5007
Roland Levillain4d027112015-07-01 15:41:14 +01005008 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005009 Register temp = locations->GetTemp(0).AsRegister<Register>();
5010 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005011 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00005012 }
5013
Calin Juravle52c48962014-12-16 17:02:57 +00005014 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005015 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00005016 }
5017}
5018
5019void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5020 HandleFieldGet(instruction, instruction->GetFieldInfo());
5021}
5022
5023void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5024 HandleFieldGet(instruction, instruction->GetFieldInfo());
5025}
5026
5027void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5028 HandleFieldSet(instruction, instruction->GetFieldInfo());
5029}
5030
5031void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005032 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005033}
5034
5035void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5036 HandleFieldSet(instruction, instruction->GetFieldInfo());
5037}
5038
5039void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005040 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005041}
5042
5043void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5044 HandleFieldGet(instruction, instruction->GetFieldInfo());
5045}
5046
5047void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5048 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005049}
5050
Calin Juravlee460d1d2015-09-29 04:52:17 +01005051void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
5052 HUnresolvedInstanceFieldGet* instruction) {
5053 FieldAccessCallingConventionX86 calling_convention;
5054 codegen_->CreateUnresolvedFieldLocationSummary(
5055 instruction, instruction->GetFieldType(), calling_convention);
5056}
5057
5058void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
5059 HUnresolvedInstanceFieldGet* instruction) {
5060 FieldAccessCallingConventionX86 calling_convention;
5061 codegen_->GenerateUnresolvedFieldAccess(instruction,
5062 instruction->GetFieldType(),
5063 instruction->GetFieldIndex(),
5064 instruction->GetDexPc(),
5065 calling_convention);
5066}
5067
5068void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
5069 HUnresolvedInstanceFieldSet* instruction) {
5070 FieldAccessCallingConventionX86 calling_convention;
5071 codegen_->CreateUnresolvedFieldLocationSummary(
5072 instruction, instruction->GetFieldType(), calling_convention);
5073}
5074
5075void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
5076 HUnresolvedInstanceFieldSet* instruction) {
5077 FieldAccessCallingConventionX86 calling_convention;
5078 codegen_->GenerateUnresolvedFieldAccess(instruction,
5079 instruction->GetFieldType(),
5080 instruction->GetFieldIndex(),
5081 instruction->GetDexPc(),
5082 calling_convention);
5083}
5084
5085void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
5086 HUnresolvedStaticFieldGet* instruction) {
5087 FieldAccessCallingConventionX86 calling_convention;
5088 codegen_->CreateUnresolvedFieldLocationSummary(
5089 instruction, instruction->GetFieldType(), calling_convention);
5090}
5091
5092void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
5093 HUnresolvedStaticFieldGet* instruction) {
5094 FieldAccessCallingConventionX86 calling_convention;
5095 codegen_->GenerateUnresolvedFieldAccess(instruction,
5096 instruction->GetFieldType(),
5097 instruction->GetFieldIndex(),
5098 instruction->GetDexPc(),
5099 calling_convention);
5100}
5101
5102void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
5103 HUnresolvedStaticFieldSet* instruction) {
5104 FieldAccessCallingConventionX86 calling_convention;
5105 codegen_->CreateUnresolvedFieldLocationSummary(
5106 instruction, instruction->GetFieldType(), calling_convention);
5107}
5108
5109void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
5110 HUnresolvedStaticFieldSet* instruction) {
5111 FieldAccessCallingConventionX86 calling_convention;
5112 codegen_->GenerateUnresolvedFieldAccess(instruction,
5113 instruction->GetFieldType(),
5114 instruction->GetFieldIndex(),
5115 instruction->GetDexPc(),
5116 calling_convention);
5117}
5118
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005119void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005120 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5121 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
5122 ? Location::RequiresRegister()
5123 : Location::Any();
5124 locations->SetInAt(0, loc);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005125}
5126
Calin Juravle2ae48182016-03-16 14:05:09 +00005127void CodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
5128 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005129 return;
5130 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005131 LocationSummary* locations = instruction->GetLocations();
5132 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005133
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005134 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00005135 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005136}
5137
Calin Juravle2ae48182016-03-16 14:05:09 +00005138void CodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005139 SlowPathCode* slow_path = new (GetScopedAllocator()) NullCheckSlowPathX86(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005140 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005141
5142 LocationSummary* locations = instruction->GetLocations();
5143 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005144
5145 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04005146 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005147 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005148 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005149 } else {
5150 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00005151 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005152 __ jmp(slow_path->GetEntryLabel());
5153 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005154 }
5155 __ j(kEqual, slow_path->GetEntryLabel());
5156}
5157
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005158void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005159 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005160}
5161
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005162void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005163 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005164 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005165 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005166 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
5167 object_array_get_with_read_barrier
5168 ? LocationSummary::kCallOnSlowPath
5169 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005170 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005171 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005172 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005173 locations->SetInAt(0, Location::RequiresRegister());
5174 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005175 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005176 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5177 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005178 // The output overlaps in case of long: we don't want the low move
5179 // to overwrite the array's location. Likewise, in the case of an
5180 // object array get with read barriers enabled, we do not want the
5181 // move to overwrite the array's location, as we need it to emit
5182 // the read barrier.
5183 locations->SetOut(
5184 Location::RequiresRegister(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005185 (instruction->GetType() == DataType::Type::kInt64 || object_array_get_with_read_barrier)
5186 ? Location::kOutputOverlap
5187 : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005188 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005189}
5190
5191void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
5192 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005193 Location obj_loc = locations->InAt(0);
5194 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005195 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005196 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005197 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005198
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005199 DataType::Type type = instruction->GetType();
Calin Juravle77520bc2015-01-12 18:45:46 +00005200 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005201 case DataType::Type::kBool:
5202 case DataType::Type::kUint8: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005203 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005204 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005205 break;
5206 }
5207
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005208 case DataType::Type::kInt8: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005209 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005210 __ movsxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005211 break;
5212 }
5213
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005214 case DataType::Type::kUint16: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005215 Register out = out_loc.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07005216 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5217 // Branch cases into compressed and uncompressed for each index's type.
5218 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
5219 NearLabel done, not_compressed;
Vladimir Marko3c89d422017-02-17 11:30:23 +00005220 __ testb(Address(obj, count_offset), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005221 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005222 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
5223 "Expecting 0=compressed, 1=uncompressed");
5224 __ j(kNotZero, &not_compressed);
jessicahandojo4877b792016-09-08 19:49:13 -07005225 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
5226 __ jmp(&done);
5227 __ Bind(&not_compressed);
5228 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5229 __ Bind(&done);
5230 } else {
5231 // Common case for charAt of array of char or when string compression's
5232 // feature is turned off.
5233 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5234 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005235 break;
5236 }
5237
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005238 case DataType::Type::kInt16: {
5239 Register out = out_loc.AsRegister<Register>();
5240 __ movsxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5241 break;
5242 }
5243
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005244 case DataType::Type::kInt32: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005245 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005246 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005247 break;
5248 }
5249
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005250 case DataType::Type::kReference: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005251 static_assert(
5252 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5253 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00005254 // /* HeapReference<Object> */ out =
5255 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5256 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005257 // Note that a potential implicit null check is handled in this
5258 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
5259 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00005260 instruction, out_loc, obj, data_offset, index, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005261 } else {
5262 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005263 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
5264 codegen_->MaybeRecordImplicitNullCheck(instruction);
5265 // If read barriers are enabled, emit read barriers other than
5266 // Baker's using a slow path (and also unpoison the loaded
5267 // reference, if heap poisoning is enabled).
Roland Levillain7c1559a2015-12-15 10:55:36 +00005268 if (index.IsConstant()) {
5269 uint32_t offset =
5270 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005271 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5272 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005273 codegen_->MaybeGenerateReadBarrierSlow(
5274 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5275 }
5276 }
5277 break;
5278 }
5279
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005280 case DataType::Type::kInt64: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005281 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005282 __ movl(out_loc.AsRegisterPairLow<Register>(),
5283 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
5284 codegen_->MaybeRecordImplicitNullCheck(instruction);
5285 __ movl(out_loc.AsRegisterPairHigh<Register>(),
5286 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005287 break;
5288 }
5289
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005290 case DataType::Type::kFloat32: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005291 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005292 __ movss(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005293 break;
5294 }
5295
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005296 case DataType::Type::kFloat64: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005297 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005298 __ movsd(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005299 break;
5300 }
5301
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005302 case DataType::Type::kVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00005303 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005304 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005305 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005306
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005307 if (type == DataType::Type::kReference || type == DataType::Type::kInt64) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005308 // Potential implicit null checks, in the case of reference or
5309 // long arrays, are handled in the previous switch statement.
5310 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005311 codegen_->MaybeRecordImplicitNullCheck(instruction);
5312 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005313}
5314
5315void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005316 DataType::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005317
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005318 bool needs_write_barrier =
5319 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005320 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005321
Vladimir Markoca6fff82017-10-03 14:49:14 +01005322 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Nicolas Geoffray39468442014-09-02 15:17:15 +01005323 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01005324 may_need_runtime_call_for_type_check ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00005325 LocationSummary::kCallOnSlowPath :
5326 LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005327
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005328 bool is_byte_type = DataType::Size(value_type) == 1u;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005329 // We need the inputs to be different than the output in case of long operation.
5330 // In case of a byte operation, the register allocator does not support multiple
5331 // inputs that die at entry with one in a specific register.
5332 locations->SetInAt(0, Location::RequiresRegister());
5333 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5334 if (is_byte_type) {
5335 // Ensure the value is in a byte register.
5336 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005337 } else if (DataType::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05005338 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005339 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005340 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
5341 }
5342 if (needs_write_barrier) {
5343 // Temporary registers for the write barrier.
5344 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5345 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005346 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005347 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005348}
5349
5350void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
5351 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005352 Location array_loc = locations->InAt(0);
5353 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005354 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005355 Location value = locations->InAt(2);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005356 DataType::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005357 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5358 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5359 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005360 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005361 bool needs_write_barrier =
5362 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005363
5364 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005365 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005366 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005367 case DataType::Type::kInt8: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005368 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005369 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_1, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005370 if (value.IsRegister()) {
5371 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005372 } else {
Nicolas Geoffray78612082017-07-24 14:18:53 +01005373 __ movb(address, Immediate(CodeGenerator::GetInt8ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005374 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005375 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005376 break;
5377 }
5378
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005379 case DataType::Type::kUint16:
5380 case DataType::Type::kInt16: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005381 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005382 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_2, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005383 if (value.IsRegister()) {
5384 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005385 } else {
Nicolas Geoffray78612082017-07-24 14:18:53 +01005386 __ movw(address, Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005387 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005388 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005389 break;
5390 }
5391
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005392 case DataType::Type::kReference: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005393 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005394 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005395
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005396 if (!value.IsRegister()) {
5397 // Just setting null.
5398 DCHECK(instruction->InputAt(2)->IsNullConstant());
5399 DCHECK(value.IsConstant()) << value;
5400 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00005401 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005402 DCHECK(!needs_write_barrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005403 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005404 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005405 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005406
5407 DCHECK(needs_write_barrier);
5408 Register register_value = value.AsRegister<Register>();
Roland Levillain16d9f942016-08-25 17:27:56 +01005409 // We cannot use a NearLabel for `done`, as its range may be too
5410 // short when Baker read barriers are enabled.
5411 Label done;
5412 NearLabel not_null, do_put;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005413 SlowPathCode* slow_path = nullptr;
Roland Levillain16d9f942016-08-25 17:27:56 +01005414 Location temp_loc = locations->GetTemp(0);
5415 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005416 if (may_need_runtime_call_for_type_check) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005417 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathX86(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005418 codegen_->AddSlowPath(slow_path);
5419 if (instruction->GetValueCanBeNull()) {
5420 __ testl(register_value, register_value);
5421 __ j(kNotEqual, &not_null);
5422 __ movl(address, Immediate(0));
5423 codegen_->MaybeRecordImplicitNullCheck(instruction);
5424 __ jmp(&done);
5425 __ Bind(&not_null);
5426 }
5427
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005428 // Note that when Baker read barriers are enabled, the type
5429 // checks are performed without read barriers. This is fine,
5430 // even in the case where a class object is in the from-space
5431 // after the flip, as a comparison involving such a type would
5432 // not produce a false positive; it may of course produce a
5433 // false negative, in which case we would take the ArraySet
5434 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01005435
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005436 // /* HeapReference<Class> */ temp = array->klass_
5437 __ movl(temp, Address(array, class_offset));
5438 codegen_->MaybeRecordImplicitNullCheck(instruction);
5439 __ MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01005440
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005441 // /* HeapReference<Class> */ temp = temp->component_type_
5442 __ movl(temp, Address(temp, component_offset));
5443 // If heap poisoning is enabled, no need to unpoison `temp`
5444 // nor the object reference in `register_value->klass`, as
5445 // we are comparing two poisoned references.
5446 __ cmpl(temp, Address(register_value, class_offset));
Roland Levillain16d9f942016-08-25 17:27:56 +01005447
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005448 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5449 __ j(kEqual, &do_put);
5450 // If heap poisoning is enabled, the `temp` reference has
5451 // not been unpoisoned yet; unpoison it now.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005452 __ MaybeUnpoisonHeapReference(temp);
5453
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005454 // If heap poisoning is enabled, no need to unpoison the
5455 // heap reference loaded below, as it is only used for a
5456 // comparison with null.
5457 __ cmpl(Address(temp, super_offset), Immediate(0));
5458 __ j(kNotEqual, slow_path->GetEntryLabel());
5459 __ Bind(&do_put);
5460 } else {
5461 __ j(kNotEqual, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005462 }
5463 }
5464
5465 if (kPoisonHeapReferences) {
5466 __ movl(temp, register_value);
5467 __ PoisonHeapReference(temp);
5468 __ movl(address, temp);
5469 } else {
5470 __ movl(address, register_value);
5471 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005472 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005473 codegen_->MaybeRecordImplicitNullCheck(instruction);
5474 }
5475
5476 Register card = locations->GetTemp(1).AsRegister<Register>();
5477 codegen_->MarkGCCard(
5478 temp, card, array, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
5479 __ Bind(&done);
5480
5481 if (slow_path != nullptr) {
5482 __ Bind(slow_path->GetExitLabel());
5483 }
5484
5485 break;
5486 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005487
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005488 case DataType::Type::kInt32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005489 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005490 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005491 if (value.IsRegister()) {
5492 __ movl(address, value.AsRegister<Register>());
5493 } else {
5494 DCHECK(value.IsConstant()) << value;
5495 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5496 __ movl(address, Immediate(v));
5497 }
5498 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005499 break;
5500 }
5501
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005502 case DataType::Type::kInt64: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005503 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005504 if (value.IsRegisterPair()) {
5505 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
5506 value.AsRegisterPairLow<Register>());
5507 codegen_->MaybeRecordImplicitNullCheck(instruction);
5508 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
5509 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005510 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005511 DCHECK(value.IsConstant());
5512 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
5513 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
5514 Immediate(Low32Bits(val)));
5515 codegen_->MaybeRecordImplicitNullCheck(instruction);
5516 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
5517 Immediate(High32Bits(val)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005518 }
5519 break;
5520 }
5521
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005522 case DataType::Type::kFloat32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005523 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005524 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005525 if (value.IsFpuRegister()) {
5526 __ movss(address, value.AsFpuRegister<XmmRegister>());
5527 } else {
5528 DCHECK(value.IsConstant());
5529 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
5530 __ movl(address, Immediate(v));
5531 }
5532 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005533 break;
5534 }
5535
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005536 case DataType::Type::kFloat64: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005537 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005538 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005539 if (value.IsFpuRegister()) {
5540 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5541 } else {
5542 DCHECK(value.IsConstant());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005543 Address address_hi =
5544 CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset + kX86WordSize);
Mark Mendell81489372015-11-04 11:30:41 -05005545 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5546 __ movl(address, Immediate(Low32Bits(v)));
5547 codegen_->MaybeRecordImplicitNullCheck(instruction);
5548 __ movl(address_hi, Immediate(High32Bits(v)));
5549 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005550 break;
5551 }
5552
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005553 case DataType::Type::kVoid:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005554 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005555 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005556 }
5557}
5558
5559void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005560 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005561 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04005562 if (!instruction->IsEmittedAtUseSite()) {
5563 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5564 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005565}
5566
5567void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04005568 if (instruction->IsEmittedAtUseSite()) {
5569 return;
5570 }
5571
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005572 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005573 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005574 Register obj = locations->InAt(0).AsRegister<Register>();
5575 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005576 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005577 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo4877b792016-09-08 19:49:13 -07005578 // Mask out most significant bit in case the array is String's array of char.
5579 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005580 __ shrl(out, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005581 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005582}
5583
5584void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005585 RegisterSet caller_saves = RegisterSet::Empty();
5586 InvokeRuntimeCallingConvention calling_convention;
5587 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5588 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5589 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005590 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04005591 HInstruction* length = instruction->InputAt(1);
5592 if (!length->IsEmittedAtUseSite()) {
5593 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5594 }
jessicahandojo4877b792016-09-08 19:49:13 -07005595 // Need register to see array's length.
5596 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5597 locations->AddTemp(Location::RequiresRegister());
5598 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005599}
5600
5601void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
jessicahandojo4877b792016-09-08 19:49:13 -07005602 const bool is_string_compressed_char_at =
5603 mirror::kUseStringCompression && instruction->IsStringCharAt();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005604 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005605 Location index_loc = locations->InAt(0);
5606 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07005607 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01005608 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005609
Mark Mendell99dbd682015-04-22 16:18:52 -04005610 if (length_loc.IsConstant()) {
5611 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5612 if (index_loc.IsConstant()) {
5613 // BCE will remove the bounds check if we are guarenteed to pass.
5614 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5615 if (index < 0 || index >= length) {
5616 codegen_->AddSlowPath(slow_path);
5617 __ jmp(slow_path->GetEntryLabel());
5618 } else {
5619 // Some optimization after BCE may have generated this, and we should not
5620 // generate a bounds check if it is a valid range.
5621 }
5622 return;
5623 }
5624
5625 // We have to reverse the jump condition because the length is the constant.
5626 Register index_reg = index_loc.AsRegister<Register>();
5627 __ cmpl(index_reg, Immediate(length));
5628 codegen_->AddSlowPath(slow_path);
5629 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005630 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04005631 HInstruction* array_length = instruction->InputAt(1);
5632 if (array_length->IsEmittedAtUseSite()) {
5633 // Address the length field in the array.
5634 DCHECK(array_length->IsArrayLength());
5635 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
5636 Location array_loc = array_length->GetLocations()->InAt(0);
5637 Address array_len(array_loc.AsRegister<Register>(), len_offset);
jessicahandojo4877b792016-09-08 19:49:13 -07005638 if (is_string_compressed_char_at) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005639 // TODO: if index_loc.IsConstant(), compare twice the index (to compensate for
5640 // the string compression flag) with the in-memory length and avoid the temporary.
jessicahandojo4877b792016-09-08 19:49:13 -07005641 Register length_reg = locations->GetTemp(0).AsRegister<Register>();
5642 __ movl(length_reg, array_len);
5643 codegen_->MaybeRecordImplicitNullCheck(array_length);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005644 __ shrl(length_reg, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005645 codegen_->GenerateIntCompare(length_reg, index_loc);
Mark Mendellee8d9712016-07-12 11:13:15 -04005646 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07005647 // Checking bounds for general case:
5648 // Array of char or string's array with feature compression off.
5649 if (index_loc.IsConstant()) {
5650 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5651 __ cmpl(array_len, Immediate(value));
5652 } else {
5653 __ cmpl(array_len, index_loc.AsRegister<Register>());
5654 }
5655 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendellee8d9712016-07-12 11:13:15 -04005656 }
Mark Mendell99dbd682015-04-22 16:18:52 -04005657 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005658 codegen_->GenerateIntCompare(length_loc, index_loc);
Mark Mendell99dbd682015-04-22 16:18:52 -04005659 }
5660 codegen_->AddSlowPath(slow_path);
5661 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005662 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005663}
5664
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005665void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005666 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005667}
5668
5669void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01005670 if (instruction->GetNext()->IsSuspendCheck() &&
5671 instruction->GetBlock()->GetLoopInformation() != nullptr) {
5672 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
5673 // The back edge will generate the suspend check.
5674 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
5675 }
5676
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005677 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5678}
5679
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005680void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005681 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5682 instruction, LocationSummary::kCallOnSlowPath);
Aart Bikb13c65b2017-03-21 20:14:07 -07005683 // In suspend check slow path, usually there are no caller-save registers at all.
5684 // If SIMD instructions are present, however, we force spilling all live SIMD
5685 // registers in full width (since the runtime only saves/restores lower part).
Aart Bik5576f372017-03-23 16:17:37 -07005686 locations->SetCustomSlowPathCallerSaves(
5687 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005688}
5689
5690void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005691 HBasicBlock* block = instruction->GetBlock();
5692 if (block->GetLoopInformation() != nullptr) {
5693 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5694 // The back edge will generate the suspend check.
5695 return;
5696 }
5697 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5698 // The goto will generate the suspend check.
5699 return;
5700 }
5701 GenerateSuspendCheck(instruction, nullptr);
5702}
5703
5704void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
5705 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005706 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005707 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
5708 if (slow_path == nullptr) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005709 slow_path =
5710 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005711 instruction->SetSlowPath(slow_path);
5712 codegen_->AddSlowPath(slow_path);
5713 if (successor != nullptr) {
5714 DCHECK(successor->IsLoopHeader());
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005715 }
5716 } else {
5717 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5718 }
5719
Andreas Gampe542451c2016-07-26 09:02:02 -07005720 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86PointerSize>().Int32Value()),
Roland Levillain7c1559a2015-12-15 10:55:36 +00005721 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005722 if (successor == nullptr) {
5723 __ j(kNotEqual, slow_path->GetEntryLabel());
5724 __ Bind(slow_path->GetReturnLabel());
5725 } else {
5726 __ j(kEqual, codegen_->GetLabelOf(successor));
5727 __ jmp(slow_path->GetEntryLabel());
5728 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005729}
5730
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005731X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
5732 return codegen_->GetAssembler();
5733}
5734
Mark Mendell7c8d0092015-01-26 11:21:33 -05005735void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005736 ScratchRegisterScope ensure_scratch(
5737 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5738 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5739 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5740 __ movl(temp_reg, Address(ESP, src + stack_offset));
5741 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005742}
5743
5744void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005745 ScratchRegisterScope ensure_scratch(
5746 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5747 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5748 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5749 __ movl(temp_reg, Address(ESP, src + stack_offset));
5750 __ movl(Address(ESP, dst + stack_offset), temp_reg);
5751 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
5752 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005753}
5754
5755void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005756 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005757 Location source = move->GetSource();
5758 Location destination = move->GetDestination();
5759
5760 if (source.IsRegister()) {
5761 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005762 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005763 } else if (destination.IsFpuRegister()) {
5764 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005765 } else {
5766 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005767 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005768 }
David Brazdil74eb1b22015-12-14 11:44:01 +00005769 } else if (source.IsRegisterPair()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005770 size_t elem_size = DataType::Size(DataType::Type::kInt32);
David Brazdil74eb1b22015-12-14 11:44:01 +00005771 // Create stack space for 2 elements.
5772 __ subl(ESP, Immediate(2 * elem_size));
5773 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
5774 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
5775 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
5776 // And remove the temporary stack space we allocated.
5777 __ addl(ESP, Immediate(2 * elem_size));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005778 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005779 if (destination.IsRegister()) {
5780 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
5781 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005782 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005783 } else if (destination.IsRegisterPair()) {
5784 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
5785 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
5786 __ psrlq(src_reg, Immediate(32));
5787 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005788 } else if (destination.IsStackSlot()) {
5789 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07005790 } else if (destination.IsDoubleStackSlot()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005791 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07005792 } else {
5793 DCHECK(destination.IsSIMDStackSlot());
5794 __ movups(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005795 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005796 } else if (source.IsStackSlot()) {
5797 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005798 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005799 } else if (destination.IsFpuRegister()) {
5800 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005801 } else {
5802 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005803 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
5804 }
5805 } else if (source.IsDoubleStackSlot()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005806 if (destination.IsRegisterPair()) {
5807 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
5808 __ movl(destination.AsRegisterPairHigh<Register>(),
5809 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
5810 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005811 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
5812 } else {
5813 DCHECK(destination.IsDoubleStackSlot()) << destination;
5814 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005815 }
Aart Bik5576f372017-03-23 16:17:37 -07005816 } else if (source.IsSIMDStackSlot()) {
5817 DCHECK(destination.IsFpuRegister());
5818 __ movups(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005819 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005820 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005821 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005822 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005823 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005824 if (value == 0) {
5825 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
5826 } else {
5827 __ movl(destination.AsRegister<Register>(), Immediate(value));
5828 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005829 } else {
5830 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05005831 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005832 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005833 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005834 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005835 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005836 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005837 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005838 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5839 if (value == 0) {
5840 // Easy handling of 0.0.
5841 __ xorps(dest, dest);
5842 } else {
5843 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005844 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5845 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
5846 __ movl(temp, Immediate(value));
5847 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005848 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005849 } else {
5850 DCHECK(destination.IsStackSlot()) << destination;
5851 __ movl(Address(ESP, destination.GetStackIndex()), imm);
5852 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005853 } else if (constant->IsLongConstant()) {
5854 int64_t value = constant->AsLongConstant()->GetValue();
5855 int32_t low_value = Low32Bits(value);
5856 int32_t high_value = High32Bits(value);
5857 Immediate low(low_value);
5858 Immediate high(high_value);
5859 if (destination.IsDoubleStackSlot()) {
5860 __ movl(Address(ESP, destination.GetStackIndex()), low);
5861 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5862 } else {
5863 __ movl(destination.AsRegisterPairLow<Register>(), low);
5864 __ movl(destination.AsRegisterPairHigh<Register>(), high);
5865 }
5866 } else {
5867 DCHECK(constant->IsDoubleConstant());
5868 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005869 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005870 int32_t low_value = Low32Bits(value);
5871 int32_t high_value = High32Bits(value);
5872 Immediate low(low_value);
5873 Immediate high(high_value);
5874 if (destination.IsFpuRegister()) {
5875 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5876 if (value == 0) {
5877 // Easy handling of 0.0.
5878 __ xorpd(dest, dest);
5879 } else {
5880 __ pushl(high);
5881 __ pushl(low);
5882 __ movsd(dest, Address(ESP, 0));
5883 __ addl(ESP, Immediate(8));
5884 }
5885 } else {
5886 DCHECK(destination.IsDoubleStackSlot()) << destination;
5887 __ movl(Address(ESP, destination.GetStackIndex()), low);
5888 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5889 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005890 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005891 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005892 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005893 }
5894}
5895
Mark Mendella5c19ce2015-04-01 12:51:05 -04005896void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005897 Register suggested_scratch = reg == EAX ? EBX : EAX;
5898 ScratchRegisterScope ensure_scratch(
5899 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
5900
5901 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5902 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
5903 __ movl(Address(ESP, mem + stack_offset), reg);
5904 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005905}
5906
Mark Mendell7c8d0092015-01-26 11:21:33 -05005907void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005908 ScratchRegisterScope ensure_scratch(
5909 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5910
5911 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5912 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5913 __ movl(temp_reg, Address(ESP, mem + stack_offset));
5914 __ movss(Address(ESP, mem + stack_offset), reg);
5915 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005916}
5917
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005918void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005919 ScratchRegisterScope ensure_scratch1(
5920 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005921
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005922 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
5923 ScratchRegisterScope ensure_scratch2(
5924 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005925
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005926 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
5927 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
5928 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
5929 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
5930 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
5931 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005932}
5933
5934void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005935 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005936 Location source = move->GetSource();
5937 Location destination = move->GetDestination();
5938
5939 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04005940 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
5941 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
5942 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
5943 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
5944 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005945 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005946 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005947 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005948 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005949 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5950 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005951 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
5952 // Use XOR Swap algorithm to avoid a temporary.
5953 DCHECK_NE(source.reg(), destination.reg());
5954 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5955 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5956 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5957 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
5958 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
5959 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
5960 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005961 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
5962 // Take advantage of the 16 bytes in the XMM register.
5963 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
5964 Address stack(ESP, destination.GetStackIndex());
5965 // Load the double into the high doubleword.
5966 __ movhpd(reg, stack);
5967
5968 // Store the low double into the destination.
5969 __ movsd(stack, reg);
5970
5971 // Move the high double to the low double.
5972 __ psrldq(reg, Immediate(8));
5973 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
5974 // Take advantage of the 16 bytes in the XMM register.
5975 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
5976 Address stack(ESP, source.GetStackIndex());
5977 // Load the double into the high doubleword.
5978 __ movhpd(reg, stack);
5979
5980 // Store the low double into the destination.
5981 __ movsd(stack, reg);
5982
5983 // Move the high double to the low double.
5984 __ psrldq(reg, Immediate(8));
5985 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
5986 Exchange(destination.GetStackIndex(), source.GetStackIndex());
5987 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005988 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005989 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005990 }
5991}
5992
5993void ParallelMoveResolverX86::SpillScratch(int reg) {
5994 __ pushl(static_cast<Register>(reg));
5995}
5996
5997void ParallelMoveResolverX86::RestoreScratch(int reg) {
5998 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005999}
6000
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006001HLoadClass::LoadKind CodeGeneratorX86::GetSupportedLoadClassKind(
6002 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006003 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006004 case HLoadClass::LoadKind::kInvalid:
6005 LOG(FATAL) << "UNREACHABLE";
6006 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006007 case HLoadClass::LoadKind::kReferrersClass:
6008 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006009 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006010 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006011 case HLoadClass::LoadKind::kBssEntry:
Vladimir Marko764d4542017-05-16 10:31:41 +01006012 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006013 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006014 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006015 DCHECK(Runtime::Current()->UseJitCompilation());
6016 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01006017 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006018 case HLoadClass::LoadKind::kRuntimeCall:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006019 break;
6020 }
6021 return desired_class_load_kind;
6022}
6023
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006024void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00006025 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006026 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006027 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00006028 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006029 cls,
6030 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00006031 Location::RegisterLocation(EAX));
Vladimir Markoea4c1262017-02-06 19:59:33 +00006032 DCHECK_EQ(calling_convention.GetRegisterAt(0), EAX);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006033 return;
6034 }
Vladimir Marko41559982017-01-06 14:04:23 +00006035 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006036
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006037 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
6038 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006039 ? LocationSummary::kCallOnSlowPath
6040 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01006041 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006042 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006043 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006044 }
6045
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006046 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006047 load_kind == HLoadClass::LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006048 load_kind == HLoadClass::LoadKind::kBootImageClassTable ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006049 load_kind == HLoadClass::LoadKind::kBssEntry) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006050 locations->SetInAt(0, Location::RequiresRegister());
6051 }
6052 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006053 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
6054 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6055 // Rely on the type resolution and/or initialization to save everything.
6056 RegisterSet caller_saves = RegisterSet::Empty();
6057 InvokeRuntimeCallingConvention calling_convention;
6058 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6059 locations->SetCustomSlowPathCallerSaves(caller_saves);
6060 } else {
6061 // For non-Baker read barrier we have a temp-clobbering call.
6062 }
6063 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006064}
6065
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006066Label* CodeGeneratorX86::NewJitRootClassPatch(const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01006067 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006068 Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006069 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006070 // Add a patch entry and return the label.
Vladimir Marko174b2e22017-10-12 13:34:49 +01006071 jit_class_patches_.emplace_back(dex_file, type_index.index_);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006072 PatchInfo<Label>* info = &jit_class_patches_.back();
6073 return &info->label;
6074}
6075
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006076// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6077// move.
6078void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00006079 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006080 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00006081 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01006082 return;
6083 }
Vladimir Marko41559982017-01-06 14:04:23 +00006084 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01006085
Vladimir Marko41559982017-01-06 14:04:23 +00006086 LocationSummary* locations = cls->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006087 Location out_loc = locations->Out();
6088 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006089
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006090 bool generate_null_check = false;
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006091 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
6092 ? kWithoutReadBarrier
6093 : kCompilerReadBarrierOption;
Vladimir Marko41559982017-01-06 14:04:23 +00006094 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006095 case HLoadClass::LoadKind::kReferrersClass: {
6096 DCHECK(!cls->CanCallRuntime());
6097 DCHECK(!cls->MustGenerateClinitCheck());
6098 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6099 Register current_method = locations->InAt(0).AsRegister<Register>();
6100 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006101 cls,
6102 out_loc,
6103 Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()),
Roland Levillain00468f32016-10-27 18:02:48 +01006104 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006105 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006106 break;
6107 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006108 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006109 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006110 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006111 Register method_address = locations->InAt(0).AsRegister<Register>();
6112 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Marko1998cd02017-01-13 13:02:58 +00006113 codegen_->RecordBootTypePatch(cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006114 break;
6115 }
6116 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006117 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006118 uint32_t address = dchecked_integral_cast<uint32_t>(
6119 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
6120 DCHECK_NE(address, 0u);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006121 __ movl(out, Immediate(address));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006122 break;
6123 }
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006124 case HLoadClass::LoadKind::kBootImageClassTable: {
6125 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6126 Register method_address = locations->InAt(0).AsRegister<Register>();
6127 __ movl(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
6128 codegen_->RecordBootTypePatch(cls);
6129 // Extract the reference from the slot data, i.e. clear the hash bits.
6130 int32_t masked_hash = ClassTable::TableSlot::MaskHash(
6131 ComputeModifiedUtf8Hash(cls->GetDexFile().StringByTypeIdx(cls->GetTypeIndex())));
6132 if (masked_hash != 0) {
6133 __ subl(out, Immediate(masked_hash));
6134 }
6135 break;
6136 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006137 case HLoadClass::LoadKind::kBssEntry: {
6138 Register method_address = locations->InAt(0).AsRegister<Register>();
6139 Address address(method_address, CodeGeneratorX86::kDummy32BitOffset);
6140 Label* fixup_label = codegen_->NewTypeBssEntryPatch(cls);
6141 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
6142 generate_null_check = true;
6143 break;
6144 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006145 case HLoadClass::LoadKind::kJitTableAddress: {
6146 Address address = Address::Absolute(CodeGeneratorX86::kDummy32BitOffset);
6147 Label* fixup_label = codegen_->NewJitRootClassPatch(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006148 cls->GetDexFile(), cls->GetTypeIndex(), cls->GetClass());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006149 // /* GcRoot<mirror::Class> */ out = *address
Vladimir Markoea4c1262017-02-06 19:59:33 +00006150 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006151 break;
6152 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006153 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006154 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00006155 LOG(FATAL) << "UNREACHABLE";
6156 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006157 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006158
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006159 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6160 DCHECK(cls->CanCallRuntime());
Vladimir Marko174b2e22017-10-12 13:34:49 +01006161 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathX86(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006162 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
6163 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006164
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006165 if (generate_null_check) {
6166 __ testl(out, out);
6167 __ j(kEqual, slow_path->GetEntryLabel());
6168 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006169
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006170 if (cls->MustGenerateClinitCheck()) {
6171 GenerateClassInitializationCheck(slow_path, out);
6172 } else {
6173 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006174 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006175 }
6176}
6177
6178void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
6179 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006180 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006181 locations->SetInAt(0, Location::RequiresRegister());
6182 if (check->HasUses()) {
6183 locations->SetOut(Location::SameAsFirstInput());
6184 }
6185}
6186
6187void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006188 // We assume the class to not be null.
Vladimir Marko174b2e22017-10-12 13:34:49 +01006189 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006190 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006191 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00006192 GenerateClassInitializationCheck(slow_path,
6193 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006194}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006195
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006196void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07006197 SlowPathCode* slow_path, Register class_reg) {
Igor Murashkin86083f72017-10-27 10:59:04 -07006198 __ cmpb(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006199 Immediate(mirror::Class::kStatusInitialized));
6200 __ j(kLess, slow_path->GetEntryLabel());
6201 __ Bind(slow_path->GetExitLabel());
6202 // No need for memory fence, thanks to the X86 memory model.
6203}
6204
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006205HLoadString::LoadKind CodeGeneratorX86::GetSupportedLoadStringKind(
6206 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006207 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006208 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006209 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00006210 case HLoadString::LoadKind::kBssEntry:
Vladimir Marko764d4542017-05-16 10:31:41 +01006211 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006212 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006213 case HLoadString::LoadKind::kJitTableAddress:
6214 DCHECK(Runtime::Current()->UseJitCompilation());
6215 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01006216 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006217 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006218 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006219 }
6220 return desired_string_load_kind;
6221}
6222
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006223void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006224 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01006225 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006226 HLoadString::LoadKind load_kind = load->GetLoadKind();
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006227 if (load_kind == HLoadString::LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006228 load_kind == HLoadString::LoadKind::kBootImageInternTable ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00006229 load_kind == HLoadString::LoadKind::kBssEntry) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006230 locations->SetInAt(0, Location::RequiresRegister());
6231 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006232 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006233 locations->SetOut(Location::RegisterLocation(EAX));
6234 } else {
6235 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006236 if (load_kind == HLoadString::LoadKind::kBssEntry) {
6237 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00006238 // Rely on the pResolveString to save everything.
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006239 RegisterSet caller_saves = RegisterSet::Empty();
6240 InvokeRuntimeCallingConvention calling_convention;
6241 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6242 locations->SetCustomSlowPathCallerSaves(caller_saves);
6243 } else {
6244 // For non-Baker read barrier we have a temp-clobbering call.
6245 }
6246 }
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006247 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006248}
6249
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006250Label* CodeGeneratorX86::NewJitRootStringPatch(const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01006251 dex::StringIndex string_index,
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006252 Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006253 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006254 // Add a patch entry and return the label.
Vladimir Marko174b2e22017-10-12 13:34:49 +01006255 jit_string_patches_.emplace_back(dex_file, string_index.index_);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006256 PatchInfo<Label>* info = &jit_string_patches_.back();
6257 return &info->label;
6258}
6259
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006260// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6261// move.
6262void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006263 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006264 Location out_loc = locations->Out();
6265 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006266
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006267 switch (load->GetLoadKind()) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006268 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006269 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006270 Register method_address = locations->InAt(0).AsRegister<Register>();
6271 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Markoaad75c62016-10-03 08:46:48 +00006272 codegen_->RecordBootStringPatch(load);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006273 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006274 }
6275 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006276 uint32_t address = dchecked_integral_cast<uint32_t>(
6277 reinterpret_cast<uintptr_t>(load->GetString().Get()));
6278 DCHECK_NE(address, 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006279 __ movl(out, Immediate(address));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006280 return;
6281 }
6282 case HLoadString::LoadKind::kBootImageInternTable: {
6283 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6284 Register method_address = locations->InAt(0).AsRegister<Register>();
6285 __ movl(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
6286 codegen_->RecordBootStringPatch(load);
6287 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006288 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00006289 case HLoadString::LoadKind::kBssEntry: {
6290 Register method_address = locations->InAt(0).AsRegister<Register>();
6291 Address address = Address(method_address, CodeGeneratorX86::kDummy32BitOffset);
6292 Label* fixup_label = codegen_->NewStringBssEntryPatch(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006293 // /* GcRoot<mirror::String> */ out = *address /* PC-relative */
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006294 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
Vladimir Marko174b2e22017-10-12 13:34:49 +01006295 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadStringSlowPathX86(load);
Vladimir Markoaad75c62016-10-03 08:46:48 +00006296 codegen_->AddSlowPath(slow_path);
6297 __ testl(out, out);
6298 __ j(kEqual, slow_path->GetEntryLabel());
6299 __ Bind(slow_path->GetExitLabel());
6300 return;
6301 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006302 case HLoadString::LoadKind::kJitTableAddress: {
6303 Address address = Address::Absolute(CodeGeneratorX86::kDummy32BitOffset);
6304 Label* fixup_label = codegen_->NewJitRootStringPatch(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006305 load->GetDexFile(), load->GetStringIndex(), load->GetString());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006306 // /* GcRoot<mirror::String> */ out = *address
6307 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
6308 return;
6309 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006310 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006311 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006312 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006313
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006314 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006315 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006316 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006317 __ movl(calling_convention.GetRegisterAt(0), Immediate(load->GetStringIndex().index_));
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006318 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6319 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006320}
6321
David Brazdilcb1c0552015-08-04 16:22:25 +01006322static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006323 return Address::Absolute(Thread::ExceptionOffset<kX86PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01006324}
6325
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006326void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
6327 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006328 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006329 locations->SetOut(Location::RequiresRegister());
6330}
6331
6332void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01006333 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
6334}
6335
6336void LocationsBuilderX86::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006337 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
David Brazdilcb1c0552015-08-04 16:22:25 +01006338}
6339
6340void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6341 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006342}
6343
6344void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006345 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6346 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006347 InvokeRuntimeCallingConvention calling_convention;
6348 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6349}
6350
6351void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006352 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006353 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006354}
6355
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006356// Temp is used for read barrier.
6357static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
6358 if (kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00006359 !kUseBakerReadBarrier &&
6360 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain7c1559a2015-12-15 10:55:36 +00006361 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006362 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
6363 return 1;
6364 }
6365 return 0;
6366}
6367
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006368// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006369// interface pointer, one for loading the current interface.
6370// The other checks have one temp for loading the object's class.
6371static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
6372 if (type_check_kind == TypeCheckKind::kInterfaceCheck && !kPoisonHeapReferences) {
6373 return 2;
6374 }
6375 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006376}
6377
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006378void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006379 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006380 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01006381 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006382 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006383 case TypeCheckKind::kExactCheck:
6384 case TypeCheckKind::kAbstractClassCheck:
6385 case TypeCheckKind::kClassHierarchyCheck:
6386 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006387 call_kind =
6388 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01006389 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006390 break;
6391 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006392 case TypeCheckKind::kUnresolvedCheck:
6393 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006394 call_kind = LocationSummary::kCallOnSlowPath;
6395 break;
6396 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006397
Vladimir Markoca6fff82017-10-03 14:49:14 +01006398 LocationSummary* locations =
6399 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01006400 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006401 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006402 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006403 locations->SetInAt(0, Location::RequiresRegister());
6404 locations->SetInAt(1, Location::Any());
6405 // Note that TypeCheckSlowPathX86 uses this "out" register too.
6406 locations->SetOut(Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006407 // When read barriers are enabled, we need a temporary register for some cases.
6408 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006409}
6410
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006411void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006412 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006413 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006414 Location obj_loc = locations->InAt(0);
6415 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006416 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006417 Location out_loc = locations->Out();
6418 Register out = out_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006419 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
6420 DCHECK_LE(num_temps, 1u);
6421 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006422 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006423 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6424 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6425 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07006426 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006427 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006428
6429 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006430 // Avoid null check if we know obj is not null.
6431 if (instruction->MustDoNullCheck()) {
6432 __ testl(obj, obj);
6433 __ j(kEqual, &zero);
6434 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006435
Roland Levillain7c1559a2015-12-15 10:55:36 +00006436 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006437 case TypeCheckKind::kExactCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006438 // /* HeapReference<Class> */ out = obj->klass_
6439 GenerateReferenceLoadTwoRegisters(instruction,
6440 out_loc,
6441 obj_loc,
6442 class_offset,
6443 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006444 if (cls.IsRegister()) {
6445 __ cmpl(out, cls.AsRegister<Register>());
6446 } else {
6447 DCHECK(cls.IsStackSlot()) << cls;
6448 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6449 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006450
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006451 // Classes must be equal for the instanceof to succeed.
6452 __ j(kNotEqual, &zero);
6453 __ movl(out, Immediate(1));
6454 __ jmp(&done);
6455 break;
6456 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006457
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006458 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006459 // /* HeapReference<Class> */ out = obj->klass_
6460 GenerateReferenceLoadTwoRegisters(instruction,
6461 out_loc,
6462 obj_loc,
6463 class_offset,
6464 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006465 // If the class is abstract, we eagerly fetch the super class of the
6466 // object to avoid doing a comparison we know will fail.
6467 NearLabel loop;
6468 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006469 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006470 GenerateReferenceLoadOneRegister(instruction,
6471 out_loc,
6472 super_offset,
6473 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006474 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006475 __ testl(out, out);
6476 // If `out` is null, we use it for the result, and jump to `done`.
6477 __ j(kEqual, &done);
6478 if (cls.IsRegister()) {
6479 __ cmpl(out, cls.AsRegister<Register>());
6480 } else {
6481 DCHECK(cls.IsStackSlot()) << cls;
6482 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6483 }
6484 __ j(kNotEqual, &loop);
6485 __ movl(out, Immediate(1));
6486 if (zero.IsLinked()) {
6487 __ jmp(&done);
6488 }
6489 break;
6490 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006491
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006492 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006493 // /* HeapReference<Class> */ out = obj->klass_
6494 GenerateReferenceLoadTwoRegisters(instruction,
6495 out_loc,
6496 obj_loc,
6497 class_offset,
6498 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006499 // Walk over the class hierarchy to find a match.
6500 NearLabel loop, success;
6501 __ Bind(&loop);
6502 if (cls.IsRegister()) {
6503 __ cmpl(out, cls.AsRegister<Register>());
6504 } else {
6505 DCHECK(cls.IsStackSlot()) << cls;
6506 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6507 }
6508 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006509 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006510 GenerateReferenceLoadOneRegister(instruction,
6511 out_loc,
6512 super_offset,
6513 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006514 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006515 __ testl(out, out);
6516 __ j(kNotEqual, &loop);
6517 // If `out` is null, we use it for the result, and jump to `done`.
6518 __ jmp(&done);
6519 __ Bind(&success);
6520 __ movl(out, Immediate(1));
6521 if (zero.IsLinked()) {
6522 __ jmp(&done);
6523 }
6524 break;
6525 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006526
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006527 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006528 // /* HeapReference<Class> */ out = obj->klass_
6529 GenerateReferenceLoadTwoRegisters(instruction,
6530 out_loc,
6531 obj_loc,
6532 class_offset,
6533 kCompilerReadBarrierOption);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006534 // Do an exact check.
6535 NearLabel exact_check;
6536 if (cls.IsRegister()) {
6537 __ cmpl(out, cls.AsRegister<Register>());
6538 } else {
6539 DCHECK(cls.IsStackSlot()) << cls;
6540 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6541 }
6542 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006543 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006544 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006545 GenerateReferenceLoadOneRegister(instruction,
6546 out_loc,
6547 component_offset,
6548 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006549 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006550 __ testl(out, out);
6551 // If `out` is null, we use it for the result, and jump to `done`.
6552 __ j(kEqual, &done);
6553 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
6554 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006555 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006556 __ movl(out, Immediate(1));
6557 __ jmp(&done);
6558 break;
6559 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006560
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006561 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006562 // No read barrier since the slow path will retry upon failure.
6563 // /* HeapReference<Class> */ out = obj->klass_
6564 GenerateReferenceLoadTwoRegisters(instruction,
6565 out_loc,
6566 obj_loc,
6567 class_offset,
6568 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006569 if (cls.IsRegister()) {
6570 __ cmpl(out, cls.AsRegister<Register>());
6571 } else {
6572 DCHECK(cls.IsStackSlot()) << cls;
6573 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6574 }
6575 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01006576 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
6577 instruction, /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006578 codegen_->AddSlowPath(slow_path);
6579 __ j(kNotEqual, slow_path->GetEntryLabel());
6580 __ movl(out, Immediate(1));
6581 if (zero.IsLinked()) {
6582 __ jmp(&done);
6583 }
6584 break;
6585 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006586
Calin Juravle98893e12015-10-02 21:05:03 +01006587 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006588 case TypeCheckKind::kInterfaceCheck: {
6589 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006590 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00006591 // cases.
6592 //
6593 // We cannot directly call the InstanceofNonTrivial runtime
6594 // entry point without resorting to a type checking slow path
6595 // here (i.e. by calling InvokeRuntime directly), as it would
6596 // require to assign fixed registers for the inputs of this
6597 // HInstanceOf instruction (following the runtime calling
6598 // convention), which might be cluttered by the potential first
6599 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006600 //
6601 // TODO: Introduce a new runtime entry point taking the object
6602 // to test (instead of its class) as argument, and let it deal
6603 // with the read barrier issues. This will let us refactor this
6604 // case of the `switch` code as it was previously (with a direct
6605 // call to the runtime not using a type checking slow path).
6606 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006607 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01006608 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
6609 instruction, /* is_fatal */ false);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006610 codegen_->AddSlowPath(slow_path);
6611 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006612 if (zero.IsLinked()) {
6613 __ jmp(&done);
6614 }
6615 break;
6616 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006617 }
6618
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006619 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006620 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006621 __ xorl(out, out);
6622 }
6623
6624 if (done.IsLinked()) {
6625 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006626 }
6627
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006628 if (slow_path != nullptr) {
6629 __ Bind(slow_path->GetExitLabel());
6630 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006631}
6632
Mathieu Chartier5ac321b2016-11-09 16:33:54 -08006633static bool IsTypeCheckSlowPathFatal(TypeCheckKind type_check_kind, bool throws_into_catch) {
6634 switch (type_check_kind) {
6635 case TypeCheckKind::kExactCheck:
6636 case TypeCheckKind::kAbstractClassCheck:
6637 case TypeCheckKind::kClassHierarchyCheck:
6638 case TypeCheckKind::kArrayObjectCheck:
6639 return !throws_into_catch && !kEmitCompilerReadBarrier;
6640 case TypeCheckKind::kInterfaceCheck:
6641 return !throws_into_catch && !kEmitCompilerReadBarrier && !kPoisonHeapReferences;
6642 case TypeCheckKind::kArrayCheck:
6643 case TypeCheckKind::kUnresolvedCheck:
6644 return false;
6645 }
6646 LOG(FATAL) << "Unreachable";
6647 UNREACHABLE();
6648}
6649
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006650void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006651 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006652 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Mathieu Chartier5ac321b2016-11-09 16:33:54 -08006653 LocationSummary::CallKind call_kind =
6654 IsTypeCheckSlowPathFatal(type_check_kind, throws_into_catch)
6655 ? LocationSummary::kNoCall
6656 : LocationSummary::kCallOnSlowPath;
Vladimir Markoca6fff82017-10-03 14:49:14 +01006657 LocationSummary* locations =
6658 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006659 locations->SetInAt(0, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006660 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
6661 // Require a register for the interface check since there is a loop that compares the class to
6662 // a memory address.
6663 locations->SetInAt(1, Location::RequiresRegister());
6664 } else {
6665 locations->SetInAt(1, Location::Any());
6666 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006667 // Note that TypeCheckSlowPathX86 uses this "temp" register too.
6668 locations->AddTemp(Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006669 // When read barriers are enabled, we need an additional temporary register for some cases.
6670 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
6671}
6672
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006673void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006674 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006675 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006676 Location obj_loc = locations->InAt(0);
6677 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006678 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006679 Location temp_loc = locations->GetTemp(0);
6680 Register temp = temp_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006681 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
6682 DCHECK_GE(num_temps, 1u);
6683 DCHECK_LE(num_temps, 2u);
6684 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
6685 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6686 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6687 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6688 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
6689 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
6690 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
6691 const uint32_t object_array_data_offset =
6692 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006693
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006694 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
6695 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
6696 // read barriers is done for performance and code size reasons.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006697 bool is_type_check_slow_path_fatal =
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006698 IsTypeCheckSlowPathFatal(type_check_kind, instruction->CanThrowIntoCatchBlock());
6699
Roland Levillain0d5a2812015-11-13 10:07:31 +00006700 SlowPathCode* type_check_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006701 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
6702 instruction, is_type_check_slow_path_fatal);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006703 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006704
Roland Levillain0d5a2812015-11-13 10:07:31 +00006705 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006706 // Avoid null check if we know obj is not null.
6707 if (instruction->MustDoNullCheck()) {
6708 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006709 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006710 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006711
Roland Levillain0d5a2812015-11-13 10:07:31 +00006712 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006713 case TypeCheckKind::kExactCheck:
6714 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006715 // /* HeapReference<Class> */ temp = obj->klass_
6716 GenerateReferenceLoadTwoRegisters(instruction,
6717 temp_loc,
6718 obj_loc,
6719 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006720 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006721
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006722 if (cls.IsRegister()) {
6723 __ cmpl(temp, cls.AsRegister<Register>());
6724 } else {
6725 DCHECK(cls.IsStackSlot()) << cls;
6726 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6727 }
6728 // Jump to slow path for throwing the exception or doing a
6729 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006730 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006731 break;
6732 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006733
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006734 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006735 // /* HeapReference<Class> */ temp = obj->klass_
6736 GenerateReferenceLoadTwoRegisters(instruction,
6737 temp_loc,
6738 obj_loc,
6739 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006740 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006741
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006742 // If the class is abstract, we eagerly fetch the super class of the
6743 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006744 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006745 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006746 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006747 GenerateReferenceLoadOneRegister(instruction,
6748 temp_loc,
6749 super_offset,
6750 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006751 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006752
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006753 // If the class reference currently in `temp` is null, jump to the slow path to throw the
6754 // exception.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006755 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006756 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006757
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006758 // Otherwise, compare the classes
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006759 if (cls.IsRegister()) {
6760 __ cmpl(temp, cls.AsRegister<Register>());
6761 } else {
6762 DCHECK(cls.IsStackSlot()) << cls;
6763 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6764 }
6765 __ j(kNotEqual, &loop);
6766 break;
6767 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006768
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006769 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006770 // /* HeapReference<Class> */ temp = obj->klass_
6771 GenerateReferenceLoadTwoRegisters(instruction,
6772 temp_loc,
6773 obj_loc,
6774 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006775 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006776
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006777 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006778 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006779 __ Bind(&loop);
6780 if (cls.IsRegister()) {
6781 __ cmpl(temp, cls.AsRegister<Register>());
6782 } else {
6783 DCHECK(cls.IsStackSlot()) << cls;
6784 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6785 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006786 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006787
Roland Levillain0d5a2812015-11-13 10:07:31 +00006788 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006789 GenerateReferenceLoadOneRegister(instruction,
6790 temp_loc,
6791 super_offset,
6792 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006793 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006794
6795 // If the class reference currently in `temp` is not null, jump
6796 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006797 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006798 __ j(kNotZero, &loop);
6799 // Otherwise, jump to the slow path to throw the exception.;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006800 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006801 break;
6802 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006803
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006804 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006805 // /* HeapReference<Class> */ temp = obj->klass_
6806 GenerateReferenceLoadTwoRegisters(instruction,
6807 temp_loc,
6808 obj_loc,
6809 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006810 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006811
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006812 // Do an exact check.
6813 if (cls.IsRegister()) {
6814 __ cmpl(temp, cls.AsRegister<Register>());
6815 } else {
6816 DCHECK(cls.IsStackSlot()) << cls;
6817 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6818 }
6819 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006820
6821 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006822 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006823 GenerateReferenceLoadOneRegister(instruction,
6824 temp_loc,
6825 component_offset,
6826 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006827 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006828
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006829 // If the component type is null (i.e. the object not an array), jump to the slow path to
6830 // throw the exception. Otherwise proceed with the check.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006831 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006832 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006833
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006834 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006835 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006836 break;
6837 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006838
Calin Juravle98893e12015-10-02 21:05:03 +01006839 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006840 // We always go into the type check slow path for the unresolved check case.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006841 // We cannot directly call the CheckCast runtime entry point
6842 // without resorting to a type checking slow path here (i.e. by
6843 // calling InvokeRuntime directly), as it would require to
6844 // assign fixed registers for the inputs of this HInstanceOf
6845 // instruction (following the runtime calling convention), which
6846 // might be cluttered by the potential first read barrier
6847 // emission at the beginning of this method.
6848 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006849 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006850
6851 case TypeCheckKind::kInterfaceCheck: {
6852 // Fast path for the interface check. Since we compare with a memory location in the inner
6853 // loop we would need to have cls poisoned. However unpoisoning cls would reset the
6854 // conditional flags and cause the conditional jump to be incorrect. Therefore we just jump
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006855 // to the slow path if we are running under poisoning.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006856 if (!kPoisonHeapReferences) {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006857 // Try to avoid read barriers to improve the fast path. We can not get false positives by
6858 // doing this.
6859 // /* HeapReference<Class> */ temp = obj->klass_
6860 GenerateReferenceLoadTwoRegisters(instruction,
6861 temp_loc,
6862 obj_loc,
6863 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006864 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006865
6866 // /* HeapReference<Class> */ temp = temp->iftable_
6867 GenerateReferenceLoadTwoRegisters(instruction,
6868 temp_loc,
6869 temp_loc,
6870 iftable_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006871 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08006872 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006873 __ movl(maybe_temp2_loc.AsRegister<Register>(), Address(temp, array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08006874 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006875 NearLabel start_loop;
6876 __ Bind(&start_loop);
Mathieu Chartier6beced42016-11-15 15:51:31 -08006877 // Need to subtract first to handle the empty array case.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006878 __ subl(maybe_temp2_loc.AsRegister<Register>(), Immediate(2));
Mathieu Chartier6beced42016-11-15 15:51:31 -08006879 __ j(kNegative, type_check_slow_path->GetEntryLabel());
6880 // Go to next interface if the classes do not match.
6881 __ cmpl(cls.AsRegister<Register>(),
6882 CodeGeneratorX86::ArrayAddress(temp,
6883 maybe_temp2_loc,
6884 TIMES_4,
6885 object_array_data_offset));
6886 __ j(kNotEqual, &start_loop);
6887 } else {
6888 __ jmp(type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006889 }
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006890 break;
6891 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006892 }
6893 __ Bind(&done);
6894
Roland Levillain0d5a2812015-11-13 10:07:31 +00006895 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006896}
6897
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006898void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006899 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6900 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006901 InvokeRuntimeCallingConvention calling_convention;
6902 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6903}
6904
6905void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006906 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject
6907 : kQuickUnlockObject,
Alexandre Rames8158f282015-08-07 10:26:17 +01006908 instruction,
Serban Constantinescuba45db02016-07-12 22:53:02 +01006909 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006910 if (instruction->IsEnter()) {
6911 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6912 } else {
6913 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6914 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006915}
6916
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006917void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6918void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6919void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6920
6921void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6922 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006923 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006924 DCHECK(instruction->GetResultType() == DataType::Type::kInt32
6925 || instruction->GetResultType() == DataType::Type::kInt64);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006926 locations->SetInAt(0, Location::RequiresRegister());
6927 locations->SetInAt(1, Location::Any());
6928 locations->SetOut(Location::SameAsFirstInput());
6929}
6930
6931void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
6932 HandleBitwiseOperation(instruction);
6933}
6934
6935void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
6936 HandleBitwiseOperation(instruction);
6937}
6938
6939void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
6940 HandleBitwiseOperation(instruction);
6941}
6942
6943void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6944 LocationSummary* locations = instruction->GetLocations();
6945 Location first = locations->InAt(0);
6946 Location second = locations->InAt(1);
6947 DCHECK(first.Equals(locations->Out()));
6948
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006949 if (instruction->GetResultType() == DataType::Type::kInt32) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006950 if (second.IsRegister()) {
6951 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006952 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006953 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006954 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006955 } else {
6956 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006957 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006958 }
6959 } else if (second.IsConstant()) {
6960 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006961 __ andl(first.AsRegister<Register>(),
6962 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006963 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006964 __ orl(first.AsRegister<Register>(),
6965 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006966 } else {
6967 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00006968 __ xorl(first.AsRegister<Register>(),
6969 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006970 }
6971 } else {
6972 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006973 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006974 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006975 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006976 } else {
6977 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006978 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006979 }
6980 }
6981 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006982 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006983 if (second.IsRegisterPair()) {
6984 if (instruction->IsAnd()) {
6985 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6986 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6987 } else if (instruction->IsOr()) {
6988 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6989 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6990 } else {
6991 DCHECK(instruction->IsXor());
6992 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6993 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6994 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006995 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006996 if (instruction->IsAnd()) {
6997 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6998 __ andl(first.AsRegisterPairHigh<Register>(),
6999 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7000 } else if (instruction->IsOr()) {
7001 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7002 __ orl(first.AsRegisterPairHigh<Register>(),
7003 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7004 } else {
7005 DCHECK(instruction->IsXor());
7006 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7007 __ xorl(first.AsRegisterPairHigh<Register>(),
7008 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7009 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007010 } else {
7011 DCHECK(second.IsConstant()) << second;
7012 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007013 int32_t low_value = Low32Bits(value);
7014 int32_t high_value = High32Bits(value);
7015 Immediate low(low_value);
7016 Immediate high(high_value);
7017 Register first_low = first.AsRegisterPairLow<Register>();
7018 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007019 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007020 if (low_value == 0) {
7021 __ xorl(first_low, first_low);
7022 } else if (low_value != -1) {
7023 __ andl(first_low, low);
7024 }
7025 if (high_value == 0) {
7026 __ xorl(first_high, first_high);
7027 } else if (high_value != -1) {
7028 __ andl(first_high, high);
7029 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007030 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007031 if (low_value != 0) {
7032 __ orl(first_low, low);
7033 }
7034 if (high_value != 0) {
7035 __ orl(first_high, high);
7036 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007037 } else {
7038 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007039 if (low_value != 0) {
7040 __ xorl(first_low, low);
7041 }
7042 if (high_value != 0) {
7043 __ xorl(first_high, high);
7044 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007045 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007046 }
7047 }
7048}
7049
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007050void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(
7051 HInstruction* instruction,
7052 Location out,
7053 uint32_t offset,
7054 Location maybe_temp,
7055 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007056 Register out_reg = out.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007057 if (read_barrier_option == kWithReadBarrier) {
7058 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007059 if (kUseBakerReadBarrier) {
7060 // Load with fast path based Baker's read barrier.
7061 // /* HeapReference<Object> */ out = *(out + offset)
7062 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00007063 instruction, out, out_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007064 } else {
7065 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007066 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain7c1559a2015-12-15 10:55:36 +00007067 // in the following move operation, as we will need it for the
7068 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00007069 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007070 __ movl(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007071 // /* HeapReference<Object> */ out = *(out + offset)
7072 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007073 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007074 }
7075 } else {
7076 // Plain load with no read barrier.
7077 // /* HeapReference<Object> */ out = *(out + offset)
7078 __ movl(out_reg, Address(out_reg, offset));
7079 __ MaybeUnpoisonHeapReference(out_reg);
7080 }
7081}
7082
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007083void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(
7084 HInstruction* instruction,
7085 Location out,
7086 Location obj,
7087 uint32_t offset,
7088 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007089 Register out_reg = out.AsRegister<Register>();
7090 Register obj_reg = obj.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007091 if (read_barrier_option == kWithReadBarrier) {
7092 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007093 if (kUseBakerReadBarrier) {
7094 // Load with fast path based Baker's read barrier.
7095 // /* HeapReference<Object> */ out = *(obj + offset)
7096 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00007097 instruction, out, obj_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007098 } else {
7099 // Load with slow path based read barrier.
7100 // /* HeapReference<Object> */ out = *(obj + offset)
7101 __ movl(out_reg, Address(obj_reg, offset));
7102 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
7103 }
7104 } else {
7105 // Plain load with no read barrier.
7106 // /* HeapReference<Object> */ out = *(obj + offset)
7107 __ movl(out_reg, Address(obj_reg, offset));
7108 __ MaybeUnpoisonHeapReference(out_reg);
7109 }
7110}
7111
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007112void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(
7113 HInstruction* instruction,
7114 Location root,
7115 const Address& address,
7116 Label* fixup_label,
7117 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007118 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007119 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07007120 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007121 if (kUseBakerReadBarrier) {
7122 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
7123 // Baker's read barrier are used:
7124 //
Roland Levillaind966ce72017-02-09 16:20:14 +00007125 // root = obj.field;
7126 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
7127 // if (temp != null) {
7128 // root = temp(root)
Roland Levillain7c1559a2015-12-15 10:55:36 +00007129 // }
7130
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007131 // /* GcRoot<mirror::Object> */ root = *address
7132 __ movl(root_reg, address);
7133 if (fixup_label != nullptr) {
7134 __ Bind(fixup_label);
7135 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007136 static_assert(
7137 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
7138 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
7139 "have different sizes.");
7140 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
7141 "art::mirror::CompressedReference<mirror::Object> and int32_t "
7142 "have different sizes.");
7143
Vladimir Marko953437b2016-08-24 08:30:46 +00007144 // Slow path marking the GC root `root`.
Vladimir Marko174b2e22017-10-12 13:34:49 +01007145 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathX86(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007146 instruction, root, /* unpoison_ref_before_marking */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007147 codegen_->AddSlowPath(slow_path);
7148
Roland Levillaind966ce72017-02-09 16:20:14 +00007149 // Test the entrypoint (`Thread::Current()->pReadBarrierMarkReg ## root.reg()`).
7150 const int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +01007151 Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(root.reg());
Roland Levillaind966ce72017-02-09 16:20:14 +00007152 __ fs()->cmpl(Address::Absolute(entry_point_offset), Immediate(0));
7153 // The entrypoint is null when the GC is not marking.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007154 __ j(kNotEqual, slow_path->GetEntryLabel());
7155 __ Bind(slow_path->GetExitLabel());
7156 } else {
7157 // GC root loaded through a slow path for read barriers other
7158 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007159 // /* GcRoot<mirror::Object>* */ root = address
7160 __ leal(root_reg, address);
7161 if (fixup_label != nullptr) {
7162 __ Bind(fixup_label);
7163 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007164 // /* mirror::Object* */ root = root->Read()
7165 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
7166 }
7167 } else {
7168 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007169 // /* GcRoot<mirror::Object> */ root = *address
7170 __ movl(root_reg, address);
7171 if (fixup_label != nullptr) {
7172 __ Bind(fixup_label);
7173 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007174 // Note that GC roots are not affected by heap poisoning, thus we
7175 // do not have to unpoison `root_reg` here.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007176 }
7177}
7178
7179void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
7180 Location ref,
7181 Register obj,
7182 uint32_t offset,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007183 bool needs_null_check) {
7184 DCHECK(kEmitCompilerReadBarrier);
7185 DCHECK(kUseBakerReadBarrier);
7186
7187 // /* HeapReference<Object> */ ref = *(obj + offset)
7188 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007189 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007190}
7191
7192void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
7193 Location ref,
7194 Register obj,
7195 uint32_t data_offset,
7196 Location index,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007197 bool needs_null_check) {
7198 DCHECK(kEmitCompilerReadBarrier);
7199 DCHECK(kUseBakerReadBarrier);
7200
Roland Levillain3d312422016-06-23 13:53:42 +01007201 static_assert(
7202 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
7203 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00007204 // /* HeapReference<Object> */ ref =
7205 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007206 Address src = CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007207 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007208}
7209
7210void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
7211 Location ref,
7212 Register obj,
7213 const Address& src,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007214 bool needs_null_check,
7215 bool always_update_field,
7216 Register* temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007217 DCHECK(kEmitCompilerReadBarrier);
7218 DCHECK(kUseBakerReadBarrier);
7219
7220 // In slow path based read barriers, the read barrier call is
7221 // inserted after the original load. However, in fast path based
7222 // Baker's read barriers, we need to perform the load of
7223 // mirror::Object::monitor_ *before* the original reference load.
7224 // This load-load ordering is required by the read barrier.
7225 // The fast path/slow path (for Baker's algorithm) should look like:
7226 //
7227 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7228 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7229 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007230 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain7c1559a2015-12-15 10:55:36 +00007231 // if (is_gray) {
7232 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7233 // }
7234 //
7235 // Note: the original implementation in ReadBarrier::Barrier is
7236 // slightly more complex as:
7237 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007238 // the high-bits of rb_state, which are expected to be all zeroes
7239 // (we use CodeGeneratorX86::GenerateMemoryBarrier instead here,
7240 // which is a no-op thanks to the x86 memory model);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007241 // - it performs additional checks that we do not do here for
7242 // performance reasons.
7243
7244 Register ref_reg = ref.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +00007245 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7246
Vladimir Marko953437b2016-08-24 08:30:46 +00007247 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007248 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
7249 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko953437b2016-08-24 08:30:46 +00007250 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
7251 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
7252 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
7253
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007254 // if (rb_state == ReadBarrier::GrayState())
Vladimir Marko953437b2016-08-24 08:30:46 +00007255 // ref = ReadBarrier::Mark(ref);
7256 // At this point, just do the "if" and make sure that flags are preserved until the branch.
7257 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain7c1559a2015-12-15 10:55:36 +00007258 if (needs_null_check) {
7259 MaybeRecordImplicitNullCheck(instruction);
7260 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007261
7262 // Load fence to prevent load-load reordering.
7263 // Note that this is a no-op, thanks to the x86 memory model.
7264 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
7265
7266 // The actual reference load.
7267 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00007268 __ movl(ref_reg, src); // Flags are unaffected.
7269
7270 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
7271 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007272 SlowPathCode* slow_path;
7273 if (always_update_field) {
7274 DCHECK(temp != nullptr);
Vladimir Marko174b2e22017-10-12 13:34:49 +01007275 slow_path = new (GetScopedAllocator()) ReadBarrierMarkAndUpdateFieldSlowPathX86(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007276 instruction, ref, obj, src, /* unpoison_ref_before_marking */ true, *temp);
7277 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01007278 slow_path = new (GetScopedAllocator()) ReadBarrierMarkSlowPathX86(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007279 instruction, ref, /* unpoison_ref_before_marking */ true);
7280 }
Vladimir Marko953437b2016-08-24 08:30:46 +00007281 AddSlowPath(slow_path);
7282
7283 // We have done the "if" of the gray bit check above, now branch based on the flags.
7284 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain7c1559a2015-12-15 10:55:36 +00007285
7286 // Object* ref = ref_addr->AsMirrorPtr()
7287 __ MaybeUnpoisonHeapReference(ref_reg);
7288
Roland Levillain7c1559a2015-12-15 10:55:36 +00007289 __ Bind(slow_path->GetExitLabel());
7290}
7291
7292void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
7293 Location out,
7294 Location ref,
7295 Location obj,
7296 uint32_t offset,
7297 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007298 DCHECK(kEmitCompilerReadBarrier);
7299
Roland Levillain7c1559a2015-12-15 10:55:36 +00007300 // Insert a slow path based read barrier *after* the reference load.
7301 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007302 // If heap poisoning is enabled, the unpoisoning of the loaded
7303 // reference will be carried out by the runtime within the slow
7304 // path.
7305 //
7306 // Note that `ref` currently does not get unpoisoned (when heap
7307 // poisoning is enabled), which is alright as the `ref` argument is
7308 // not used by the artReadBarrierSlow entry point.
7309 //
7310 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01007311 SlowPathCode* slow_path = new (GetScopedAllocator())
Roland Levillain0d5a2812015-11-13 10:07:31 +00007312 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
7313 AddSlowPath(slow_path);
7314
Roland Levillain0d5a2812015-11-13 10:07:31 +00007315 __ jmp(slow_path->GetEntryLabel());
7316 __ Bind(slow_path->GetExitLabel());
7317}
7318
Roland Levillain7c1559a2015-12-15 10:55:36 +00007319void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7320 Location out,
7321 Location ref,
7322 Location obj,
7323 uint32_t offset,
7324 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007325 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007326 // Baker's read barriers shall be handled by the fast path
7327 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
7328 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007329 // If heap poisoning is enabled, unpoisoning will be taken care of
7330 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007331 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007332 } else if (kPoisonHeapReferences) {
7333 __ UnpoisonHeapReference(out.AsRegister<Register>());
7334 }
7335}
7336
Roland Levillain7c1559a2015-12-15 10:55:36 +00007337void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7338 Location out,
7339 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007340 DCHECK(kEmitCompilerReadBarrier);
7341
Roland Levillain7c1559a2015-12-15 10:55:36 +00007342 // Insert a slow path based read barrier *after* the GC root load.
7343 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007344 // Note that GC roots are not affected by heap poisoning, so we do
7345 // not need to do anything special for this here.
7346 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007347 new (GetScopedAllocator()) ReadBarrierForRootSlowPathX86(instruction, out, root);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007348 AddSlowPath(slow_path);
7349
Roland Levillain0d5a2812015-11-13 10:07:31 +00007350 __ jmp(slow_path->GetEntryLabel());
7351 __ Bind(slow_path->GetExitLabel());
7352}
7353
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007354void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007355 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007356 LOG(FATAL) << "Unreachable";
7357}
7358
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007359void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007360 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007361 LOG(FATAL) << "Unreachable";
7362}
7363
Mark Mendellfe57faa2015-09-18 09:26:15 -04007364// Simple implementation of packed switch - generate cascaded compare/jumps.
7365void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7366 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007367 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendellfe57faa2015-09-18 09:26:15 -04007368 locations->SetInAt(0, Location::RequiresRegister());
7369}
7370
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007371void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
7372 int32_t lower_bound,
7373 uint32_t num_entries,
7374 HBasicBlock* switch_block,
7375 HBasicBlock* default_block) {
7376 // Figure out the correct compare values and jump conditions.
7377 // Handle the first compare/branch as a special case because it might
7378 // jump to the default case.
7379 DCHECK_GT(num_entries, 2u);
7380 Condition first_condition;
7381 uint32_t index;
7382 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
7383 if (lower_bound != 0) {
7384 first_condition = kLess;
7385 __ cmpl(value_reg, Immediate(lower_bound));
7386 __ j(first_condition, codegen_->GetLabelOf(default_block));
7387 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007388
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007389 index = 1;
7390 } else {
7391 // Handle all the compare/jumps below.
7392 first_condition = kBelow;
7393 index = 0;
7394 }
7395
7396 // Handle the rest of the compare/jumps.
7397 for (; index + 1 < num_entries; index += 2) {
7398 int32_t compare_to_value = lower_bound + index + 1;
7399 __ cmpl(value_reg, Immediate(compare_to_value));
7400 // Jump to successors[index] if value < case_value[index].
7401 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
7402 // Jump to successors[index + 1] if value == case_value[index + 1].
7403 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
7404 }
7405
7406 if (index != num_entries) {
7407 // There are an odd number of entries. Handle the last one.
7408 DCHECK_EQ(index + 1, num_entries);
7409 __ cmpl(value_reg, Immediate(lower_bound + index));
7410 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007411 }
7412
7413 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007414 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
7415 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007416 }
7417}
7418
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007419void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7420 int32_t lower_bound = switch_instr->GetStartValue();
7421 uint32_t num_entries = switch_instr->GetNumEntries();
7422 LocationSummary* locations = switch_instr->GetLocations();
7423 Register value_reg = locations->InAt(0).AsRegister<Register>();
7424
7425 GenPackedSwitchWithCompares(value_reg,
7426 lower_bound,
7427 num_entries,
7428 switch_instr->GetBlock(),
7429 switch_instr->GetDefaultBlock());
7430}
7431
Mark Mendell805b3b52015-09-18 14:10:29 -04007432void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7433 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007434 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendell805b3b52015-09-18 14:10:29 -04007435 locations->SetInAt(0, Location::RequiresRegister());
7436
7437 // Constant area pointer.
7438 locations->SetInAt(1, Location::RequiresRegister());
7439
7440 // And the temporary we need.
7441 locations->AddTemp(Location::RequiresRegister());
7442}
7443
7444void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7445 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007446 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04007447 LocationSummary* locations = switch_instr->GetLocations();
7448 Register value_reg = locations->InAt(0).AsRegister<Register>();
7449 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7450
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007451 if (num_entries <= kPackedSwitchJumpTableThreshold) {
7452 GenPackedSwitchWithCompares(value_reg,
7453 lower_bound,
7454 num_entries,
7455 switch_instr->GetBlock(),
7456 default_block);
7457 return;
7458 }
7459
Mark Mendell805b3b52015-09-18 14:10:29 -04007460 // Optimizing has a jump area.
7461 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7462 Register constant_area = locations->InAt(1).AsRegister<Register>();
7463
7464 // Remove the bias, if needed.
7465 if (lower_bound != 0) {
7466 __ leal(temp_reg, Address(value_reg, -lower_bound));
7467 value_reg = temp_reg;
7468 }
7469
7470 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007471 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04007472 __ cmpl(value_reg, Immediate(num_entries - 1));
7473 __ j(kAbove, codegen_->GetLabelOf(default_block));
7474
7475 // We are in the range of the table.
7476 // Load (target-constant_area) from the jump table, indexing by the value.
7477 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
7478
7479 // Compute the actual target address by adding in constant_area.
7480 __ addl(temp_reg, constant_area);
7481
7482 // And jump.
7483 __ jmp(temp_reg);
7484}
7485
Mark Mendell0616ae02015-04-17 12:49:27 -04007486void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
7487 HX86ComputeBaseMethodAddress* insn) {
7488 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007489 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Mark Mendell0616ae02015-04-17 12:49:27 -04007490 locations->SetOut(Location::RequiresRegister());
7491}
7492
7493void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
7494 HX86ComputeBaseMethodAddress* insn) {
7495 LocationSummary* locations = insn->GetLocations();
7496 Register reg = locations->Out().AsRegister<Register>();
7497
7498 // Generate call to next instruction.
7499 Label next_instruction;
7500 __ call(&next_instruction);
7501 __ Bind(&next_instruction);
7502
7503 // Remember this offset for later use with constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007504 codegen_->AddMethodAddressOffset(insn, GetAssembler()->CodeSize());
Mark Mendell0616ae02015-04-17 12:49:27 -04007505
7506 // Grab the return address off the stack.
7507 __ popl(reg);
7508}
7509
7510void LocationsBuilderX86::VisitX86LoadFromConstantTable(
7511 HX86LoadFromConstantTable* insn) {
7512 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007513 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Mark Mendell0616ae02015-04-17 12:49:27 -04007514
7515 locations->SetInAt(0, Location::RequiresRegister());
7516 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
7517
7518 // If we don't need to be materialized, we only need the inputs to be set.
David Brazdilb3e773e2016-01-26 11:28:37 +00007519 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007520 return;
7521 }
7522
7523 switch (insn->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007524 case DataType::Type::kFloat32:
7525 case DataType::Type::kFloat64:
Mark Mendell0616ae02015-04-17 12:49:27 -04007526 locations->SetOut(Location::RequiresFpuRegister());
7527 break;
7528
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007529 case DataType::Type::kInt32:
Mark Mendell0616ae02015-04-17 12:49:27 -04007530 locations->SetOut(Location::RequiresRegister());
7531 break;
7532
7533 default:
7534 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7535 }
7536}
7537
7538void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
David Brazdilb3e773e2016-01-26 11:28:37 +00007539 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007540 return;
7541 }
7542
7543 LocationSummary* locations = insn->GetLocations();
7544 Location out = locations->Out();
7545 Register const_area = locations->InAt(0).AsRegister<Register>();
7546 HConstant *value = insn->GetConstant();
7547
7548 switch (insn->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007549 case DataType::Type::kFloat32:
Mark Mendell0616ae02015-04-17 12:49:27 -04007550 __ movss(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007551 codegen_->LiteralFloatAddress(
7552 value->AsFloatConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04007553 break;
7554
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007555 case DataType::Type::kFloat64:
Mark Mendell0616ae02015-04-17 12:49:27 -04007556 __ movsd(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007557 codegen_->LiteralDoubleAddress(
7558 value->AsDoubleConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04007559 break;
7560
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007561 case DataType::Type::kInt32:
Mark Mendell0616ae02015-04-17 12:49:27 -04007562 __ movl(out.AsRegister<Register>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007563 codegen_->LiteralInt32Address(
7564 value->AsIntConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04007565 break;
7566
7567 default:
7568 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7569 }
7570}
7571
Mark Mendell0616ae02015-04-17 12:49:27 -04007572/**
7573 * Class to handle late fixup of offsets into constant area.
7574 */
Vladimir Marko5233f932015-09-29 19:01:15 +01007575class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04007576 public:
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007577 RIPFixup(CodeGeneratorX86& codegen,
7578 HX86ComputeBaseMethodAddress* base_method_address,
7579 size_t offset)
7580 : codegen_(&codegen),
7581 base_method_address_(base_method_address),
7582 offset_into_constant_area_(offset) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04007583
7584 protected:
7585 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
7586
7587 CodeGeneratorX86* codegen_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007588 HX86ComputeBaseMethodAddress* base_method_address_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007589
7590 private:
7591 void Process(const MemoryRegion& region, int pos) OVERRIDE {
7592 // Patch the correct offset for the instruction. The place to patch is the
7593 // last 4 bytes of the instruction.
7594 // The value to patch is the distance from the offset in the constant area
7595 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04007596 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007597 int32_t relative_position =
7598 constant_offset - codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell0616ae02015-04-17 12:49:27 -04007599
7600 // Patch in the right value.
7601 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
7602 }
7603
Mark Mendell0616ae02015-04-17 12:49:27 -04007604 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04007605 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007606};
7607
Mark Mendell805b3b52015-09-18 14:10:29 -04007608/**
7609 * Class to handle late fixup of offsets to a jump table that will be created in the
7610 * constant area.
7611 */
7612class JumpTableRIPFixup : public RIPFixup {
7613 public:
7614 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007615 : RIPFixup(codegen, switch_instr->GetBaseMethodAddress(), static_cast<size_t>(-1)),
7616 switch_instr_(switch_instr) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04007617
7618 void CreateJumpTable() {
7619 X86Assembler* assembler = codegen_->GetAssembler();
7620
7621 // Ensure that the reference to the jump table has the correct offset.
7622 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
7623 SetOffset(offset_in_constant_table);
7624
7625 // The label values in the jump table are computed relative to the
7626 // instruction addressing the constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007627 const int32_t relative_offset = codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell805b3b52015-09-18 14:10:29 -04007628
7629 // Populate the jump table with the correct values for the jump table.
7630 int32_t num_entries = switch_instr_->GetNumEntries();
7631 HBasicBlock* block = switch_instr_->GetBlock();
7632 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
7633 // The value that we want is the target offset - the position of the table.
7634 for (int32_t i = 0; i < num_entries; i++) {
7635 HBasicBlock* b = successors[i];
7636 Label* l = codegen_->GetLabelOf(b);
7637 DCHECK(l->IsBound());
7638 int32_t offset_to_block = l->Position() - relative_offset;
7639 assembler->AppendInt32(offset_to_block);
7640 }
7641 }
7642
7643 private:
7644 const HX86PackedSwitch* switch_instr_;
7645};
7646
7647void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
7648 // Generate the constant area if needed.
7649 X86Assembler* assembler = GetAssembler();
7650 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
7651 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
7652 // byte values.
7653 assembler->Align(4, 0);
7654 constant_area_start_ = assembler->CodeSize();
7655
7656 // Populate any jump tables.
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007657 for (JumpTableRIPFixup* jump_table : fixups_to_jump_tables_) {
Mark Mendell805b3b52015-09-18 14:10:29 -04007658 jump_table->CreateJumpTable();
7659 }
7660
7661 // And now add the constant area to the generated code.
7662 assembler->AddConstantArea();
7663 }
7664
7665 // And finish up.
7666 CodeGenerator::Finalize(allocator);
7667}
7668
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007669Address CodeGeneratorX86::LiteralDoubleAddress(double v,
7670 HX86ComputeBaseMethodAddress* method_base,
7671 Register reg) {
7672 AssemblerFixup* fixup =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007673 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddDouble(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04007674 return Address(reg, kDummy32BitOffset, fixup);
7675}
7676
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007677Address CodeGeneratorX86::LiteralFloatAddress(float v,
7678 HX86ComputeBaseMethodAddress* method_base,
7679 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007680 AssemblerFixup* fixup =
7681 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddFloat(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04007682 return Address(reg, kDummy32BitOffset, fixup);
7683}
7684
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007685Address CodeGeneratorX86::LiteralInt32Address(int32_t v,
7686 HX86ComputeBaseMethodAddress* method_base,
7687 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007688 AssemblerFixup* fixup =
7689 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddInt32(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04007690 return Address(reg, kDummy32BitOffset, fixup);
7691}
7692
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007693Address CodeGeneratorX86::LiteralInt64Address(int64_t v,
7694 HX86ComputeBaseMethodAddress* method_base,
7695 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007696 AssemblerFixup* fixup =
7697 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddInt64(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04007698 return Address(reg, kDummy32BitOffset, fixup);
7699}
7700
Aart Bika19616e2016-02-01 18:57:58 -08007701void CodeGeneratorX86::Load32BitValue(Register dest, int32_t value) {
7702 if (value == 0) {
7703 __ xorl(dest, dest);
7704 } else {
7705 __ movl(dest, Immediate(value));
7706 }
7707}
7708
7709void CodeGeneratorX86::Compare32BitValue(Register dest, int32_t value) {
7710 if (value == 0) {
7711 __ testl(dest, dest);
7712 } else {
7713 __ cmpl(dest, Immediate(value));
7714 }
7715}
7716
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007717void CodeGeneratorX86::GenerateIntCompare(Location lhs, Location rhs) {
7718 Register lhs_reg = lhs.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07007719 GenerateIntCompare(lhs_reg, rhs);
7720}
7721
7722void CodeGeneratorX86::GenerateIntCompare(Register lhs, Location rhs) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007723 if (rhs.IsConstant()) {
7724 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
jessicahandojo4877b792016-09-08 19:49:13 -07007725 Compare32BitValue(lhs, value);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007726 } else if (rhs.IsStackSlot()) {
jessicahandojo4877b792016-09-08 19:49:13 -07007727 __ cmpl(lhs, Address(ESP, rhs.GetStackIndex()));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007728 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07007729 __ cmpl(lhs, rhs.AsRegister<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007730 }
7731}
7732
7733Address CodeGeneratorX86::ArrayAddress(Register obj,
7734 Location index,
7735 ScaleFactor scale,
7736 uint32_t data_offset) {
7737 return index.IsConstant() ?
7738 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << scale) + data_offset) :
7739 Address(obj, index.AsRegister<Register>(), scale, data_offset);
7740}
7741
Mark Mendell805b3b52015-09-18 14:10:29 -04007742Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
7743 Register reg,
7744 Register value) {
7745 // Create a fixup to be used to create and address the jump table.
7746 JumpTableRIPFixup* table_fixup =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007747 new (GetGraph()->GetAllocator()) JumpTableRIPFixup(*this, switch_instr);
Mark Mendell805b3b52015-09-18 14:10:29 -04007748
7749 // We have to populate the jump tables.
7750 fixups_to_jump_tables_.push_back(table_fixup);
7751
7752 // We want a scaled address, as we are extracting the correct offset from the table.
7753 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
7754}
7755
Andreas Gampe85b62f22015-09-09 13:15:38 -07007756// TODO: target as memory.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007757void CodeGeneratorX86::MoveFromReturnRegister(Location target, DataType::Type type) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07007758 if (!target.IsValid()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007759 DCHECK_EQ(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007760 return;
7761 }
7762
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007763 DCHECK_NE(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007764
7765 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
7766 if (target.Equals(return_loc)) {
7767 return;
7768 }
7769
7770 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7771 // with the else branch.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007772 if (type == DataType::Type::kInt64) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007773 HParallelMove parallel_move(GetGraph()->GetAllocator());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007774 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), DataType::Type::kInt32, nullptr);
7775 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), DataType::Type::kInt32, nullptr);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007776 GetMoveResolver()->EmitNativeCode(&parallel_move);
7777 } else {
7778 // Let the parallel move resolver take care of all of this.
Vladimir Markoca6fff82017-10-03 14:49:14 +01007779 HParallelMove parallel_move(GetGraph()->GetAllocator());
Andreas Gampe85b62f22015-09-09 13:15:38 -07007780 parallel_move.AddMove(return_loc, target, type, nullptr);
7781 GetMoveResolver()->EmitNativeCode(&parallel_move);
7782 }
7783}
7784
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007785void CodeGeneratorX86::PatchJitRootUse(uint8_t* code,
7786 const uint8_t* roots_data,
7787 const PatchInfo<Label>& info,
7788 uint64_t index_in_table) const {
7789 uint32_t code_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
7790 uintptr_t address =
7791 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
7792 typedef __attribute__((__aligned__(1))) uint32_t unaligned_uint32_t;
7793 reinterpret_cast<unaligned_uint32_t*>(code + code_offset)[0] =
7794 dchecked_integral_cast<uint32_t>(address);
7795}
7796
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007797void CodeGeneratorX86::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
7798 for (const PatchInfo<Label>& info : jit_string_patches_) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01007799 StringReference string_reference(&info.dex_file, dex::StringIndex(info.index));
7800 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007801 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007802 }
7803
7804 for (const PatchInfo<Label>& info : jit_class_patches_) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01007805 TypeReference type_reference(&info.dex_file, dex::TypeIndex(info.index));
7806 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007807 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007808 }
7809}
7810
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007811void LocationsBuilderX86::VisitIntermediateAddress(HIntermediateAddress* instruction
7812 ATTRIBUTE_UNUSED) {
7813 LOG(FATAL) << "Unreachable";
7814}
7815
7816void InstructionCodeGeneratorX86::VisitIntermediateAddress(HIntermediateAddress* instruction
7817 ATTRIBUTE_UNUSED) {
7818 LOG(FATAL) << "Unreachable";
7819}
7820
Roland Levillain4d027112015-07-01 15:41:14 +01007821#undef __
7822
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007823} // namespace x86
7824} // namespace art