blob: 70e270e74dadc85705c90f2f032b2a4609ad274a [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.
147 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
148 Location array_loc = array_length->GetLocations()->InAt(0);
149 Address array_len(array_loc.AsRegister<Register>(), len_offset);
150 length_loc = Location::RegisterLocation(calling_convention.GetRegisterAt(1));
151 // Check for conflicts with index.
152 if (length_loc.Equals(locations->InAt(0))) {
153 // We know we aren't using parameter 2.
154 length_loc = Location::RegisterLocation(calling_convention.GetRegisterAt(2));
155 }
156 __ movl(length_loc.AsRegister<Register>(), array_len);
jessicahandojo4877b792016-09-08 19:49:13 -0700157 if (mirror::kUseStringCompression) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +0100158 __ shrl(length_loc.AsRegister<Register>(), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -0700159 }
Mark Mendellee8d9712016-07-12 11:13:15 -0400160 }
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000161 x86_codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100162 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000163 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100164 DataType::Type::kInt32,
Mark Mendellee8d9712016-07-12 11:13:15 -0400165 length_loc,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100166 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100167 DataType::Type::kInt32);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100168 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
169 ? kQuickThrowStringBounds
170 : kQuickThrowArrayBounds;
171 x86_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100172 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Roland Levillain888d0672015-11-23 18:53:50 +0000173 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100174 }
175
Alexandre Rames8158f282015-08-07 10:26:17 +0100176 bool IsFatal() const OVERRIDE { return true; }
177
Alexandre Rames9931f312015-06-19 14:47:01 +0100178 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathX86"; }
179
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100180 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100181 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
182};
183
Andreas Gampe85b62f22015-09-09 13:15:38 -0700184class SuspendCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000185 public:
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000186 SuspendCheckSlowPathX86(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000187 : SlowPathCode(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000188
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000189 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bikb13c65b2017-03-21 20:14:07 -0700190 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100191 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000192 __ Bind(GetEntryLabel());
Aart Bik24b905f2017-04-06 09:59:06 -0700193 SaveLiveRegisters(codegen, locations); // Only saves full width XMM for SIMD.
Serban Constantinescuba45db02016-07-12 22:53:02 +0100194 x86_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000195 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Aart Bik24b905f2017-04-06 09:59:06 -0700196 RestoreLiveRegisters(codegen, locations); // Only restores full width XMM for SIMD.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100197 if (successor_ == nullptr) {
198 __ jmp(GetReturnLabel());
199 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100200 __ jmp(x86_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100201 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000202 }
203
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100204 Label* GetReturnLabel() {
205 DCHECK(successor_ == nullptr);
206 return &return_label_;
207 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000208
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100209 HBasicBlock* GetSuccessor() const {
210 return successor_;
211 }
212
Alexandre Rames9931f312015-06-19 14:47:01 +0100213 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathX86"; }
214
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000215 private:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100216 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000217 Label return_label_;
218
219 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
220};
221
Vladimir Markoaad75c62016-10-03 08:46:48 +0000222class LoadStringSlowPathX86 : public SlowPathCode {
223 public:
224 explicit LoadStringSlowPathX86(HLoadString* instruction): SlowPathCode(instruction) {}
225
226 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
227 LocationSummary* locations = instruction_->GetLocations();
228 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
229
230 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
231 __ Bind(GetEntryLabel());
232 SaveLiveRegisters(codegen, locations);
233
234 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000235 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
236 __ movl(calling_convention.GetRegisterAt(0), Immediate(string_index.index_));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000237 x86_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
238 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
239 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
240 RestoreLiveRegisters(codegen, locations);
241
242 // Store the resolved String to the BSS entry.
243 Register method_address = locations->InAt(0).AsRegister<Register>();
244 __ movl(Address(method_address, CodeGeneratorX86::kDummy32BitOffset),
245 locations->Out().AsRegister<Register>());
246 Label* fixup_label = x86_codegen->NewStringBssEntryPatch(instruction_->AsLoadString());
247 __ Bind(fixup_label);
248
249 __ jmp(GetExitLabel());
250 }
251
252 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathX86"; }
253
254 private:
255 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86);
256};
257
Andreas Gampe85b62f22015-09-09 13:15:38 -0700258class LoadClassSlowPathX86 : public SlowPathCode {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000259 public:
260 LoadClassSlowPathX86(HLoadClass* cls,
261 HInstruction* at,
262 uint32_t dex_pc,
263 bool do_clinit)
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000264 : SlowPathCode(at), cls_(cls), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000265 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
266 }
267
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000268 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000269 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000270 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
271 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000272 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000273
274 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000275 dex::TypeIndex type_index = cls_->GetTypeIndex();
276 __ movl(calling_convention.GetRegisterAt(0), Immediate(type_index.index_));
Serban Constantinescuba45db02016-07-12 22:53:02 +0100277 x86_codegen->InvokeRuntime(do_clinit_ ? kQuickInitializeStaticStorage
278 : kQuickInitializeType,
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000279 instruction_,
280 dex_pc_,
281 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000282 if (do_clinit_) {
283 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
284 } else {
285 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
286 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000287
288 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000289 Location out = locations->Out();
290 if (out.IsValid()) {
291 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
292 x86_codegen->Move32(out, Location::RegisterLocation(EAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000293 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000294 RestoreLiveRegisters(codegen, locations);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000295 // For HLoadClass/kBssEntry, store the resolved Class to the BSS entry.
296 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
297 if (cls_ == instruction_ && cls_->GetLoadKind() == HLoadClass::LoadKind::kBssEntry) {
298 DCHECK(out.IsValid());
299 Register method_address = locations->InAt(0).AsRegister<Register>();
300 __ movl(Address(method_address, CodeGeneratorX86::kDummy32BitOffset),
301 locations->Out().AsRegister<Register>());
302 Label* fixup_label = x86_codegen->NewTypeBssEntryPatch(cls_);
303 __ Bind(fixup_label);
304 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000305 __ jmp(GetExitLabel());
306 }
307
Alexandre Rames9931f312015-06-19 14:47:01 +0100308 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathX86"; }
309
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000310 private:
311 // The class this slow path will load.
312 HLoadClass* const cls_;
313
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000314 // The dex PC of `at_`.
315 const uint32_t dex_pc_;
316
317 // Whether to initialize the class.
318 const bool do_clinit_;
319
320 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
321};
322
Andreas Gampe85b62f22015-09-09 13:15:38 -0700323class TypeCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000324 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000325 TypeCheckSlowPathX86(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000326 : SlowPathCode(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000327
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000328 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000329 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000330 DCHECK(instruction_->IsCheckCast()
331 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000332
333 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
334 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000335
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000336 if (!is_fatal_) {
337 SaveLiveRegisters(codegen, locations);
338 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000339
340 // We're moving two locations to locations that could overlap, so we need a parallel
341 // move resolver.
342 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800343 x86_codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800344 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100345 DataType::Type::kReference,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800346 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800347 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100348 DataType::Type::kReference);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000349 if (instruction_->IsInstanceOf()) {
Serban Constantinescuba45db02016-07-12 22:53:02 +0100350 x86_codegen->InvokeRuntime(kQuickInstanceofNonTrivial,
Alexandre Rames8158f282015-08-07 10:26:17 +0100351 instruction_,
352 instruction_->GetDexPc(),
353 this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800354 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000355 } else {
356 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800357 x86_codegen->InvokeRuntime(kQuickCheckInstanceOf,
358 instruction_,
359 instruction_->GetDexPc(),
360 this);
361 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000362 }
363
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000364 if (!is_fatal_) {
365 if (instruction_->IsInstanceOf()) {
366 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
367 }
368 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray75374372015-09-17 17:12:19 +0000369
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000370 __ jmp(GetExitLabel());
371 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000372 }
373
Alexandre Rames9931f312015-06-19 14:47:01 +0100374 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathX86"; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000375 bool IsFatal() const OVERRIDE { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100376
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000377 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000378 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000379
380 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
381};
382
Andreas Gampe85b62f22015-09-09 13:15:38 -0700383class DeoptimizationSlowPathX86 : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700384 public:
Aart Bik42249c32016-01-07 15:33:50 -0800385 explicit DeoptimizationSlowPathX86(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000386 : SlowPathCode(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700387
388 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +0100389 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700390 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100391 LocationSummary* locations = instruction_->GetLocations();
392 SaveLiveRegisters(codegen, locations);
393 InvokeRuntimeCallingConvention calling_convention;
394 x86_codegen->Load32BitValue(
395 calling_convention.GetRegisterAt(0),
396 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescuba45db02016-07-12 22:53:02 +0100397 x86_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100398 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700399 }
400
Alexandre Rames9931f312015-06-19 14:47:01 +0100401 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86"; }
402
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700403 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700404 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86);
405};
406
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100407class ArraySetSlowPathX86 : public SlowPathCode {
408 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000409 explicit ArraySetSlowPathX86(HInstruction* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100410
411 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
412 LocationSummary* locations = instruction_->GetLocations();
413 __ Bind(GetEntryLabel());
414 SaveLiveRegisters(codegen, locations);
415
416 InvokeRuntimeCallingConvention calling_convention;
417 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
418 parallel_move.AddMove(
419 locations->InAt(0),
420 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100421 DataType::Type::kReference,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100422 nullptr);
423 parallel_move.AddMove(
424 locations->InAt(1),
425 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100426 DataType::Type::kInt32,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100427 nullptr);
428 parallel_move.AddMove(
429 locations->InAt(2),
430 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100431 DataType::Type::kReference,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100432 nullptr);
433 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
434
435 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100436 x86_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000437 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100438 RestoreLiveRegisters(codegen, locations);
439 __ jmp(GetExitLabel());
440 }
441
442 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathX86"; }
443
444 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100445 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathX86);
446};
447
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100448// Slow path marking an object reference `ref` during a read
449// barrier. The field `obj.field` in the object `obj` holding this
450// reference does not get updated by this slow path after marking (see
451// ReadBarrierMarkAndUpdateFieldSlowPathX86 below for that).
452//
453// This means that after the execution of this slow path, `ref` will
454// always be up-to-date, but `obj.field` may not; i.e., after the
455// flip, `ref` will be a to-space reference, but `obj.field` will
456// probably still be a from-space reference (unless it gets updated by
457// another thread, or if another thread installed another object
458// reference (different from `ref`) in `obj.field`).
Roland Levillain7c1559a2015-12-15 10:55:36 +0000459class ReadBarrierMarkSlowPathX86 : public SlowPathCode {
460 public:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100461 ReadBarrierMarkSlowPathX86(HInstruction* instruction,
462 Location ref,
463 bool unpoison_ref_before_marking)
464 : SlowPathCode(instruction),
465 ref_(ref),
466 unpoison_ref_before_marking_(unpoison_ref_before_marking) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000467 DCHECK(kEmitCompilerReadBarrier);
468 }
469
470 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathX86"; }
471
472 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
473 LocationSummary* locations = instruction_->GetLocations();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100474 Register ref_reg = ref_.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +0000475 DCHECK(locations->CanCall());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100476 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
Roland Levillain7c1559a2015-12-15 10:55:36 +0000477 DCHECK(instruction_->IsInstanceFieldGet() ||
478 instruction_->IsStaticFieldGet() ||
479 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100480 instruction_->IsArraySet() ||
Roland Levillain7c1559a2015-12-15 10:55:36 +0000481 instruction_->IsLoadClass() ||
482 instruction_->IsLoadString() ||
483 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100484 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100485 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
486 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain7c1559a2015-12-15 10:55:36 +0000487 << "Unexpected instruction in read barrier marking slow path: "
488 << instruction_->DebugName();
489
490 __ Bind(GetEntryLabel());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100491 if (unpoison_ref_before_marking_) {
Vladimir Marko953437b2016-08-24 08:30:46 +0000492 // Object* ref = ref_addr->AsMirrorPtr()
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100493 __ MaybeUnpoisonHeapReference(ref_reg);
Vladimir Marko953437b2016-08-24 08:30:46 +0000494 }
Roland Levillain4359e612016-07-20 11:32:19 +0100495 // No need to save live registers; it's taken care of by the
496 // entrypoint. Also, there is no need to update the stack mask,
497 // as this runtime call will not trigger a garbage collection.
Roland Levillain7c1559a2015-12-15 10:55:36 +0000498 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100499 DCHECK_NE(ref_reg, ESP);
500 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCpuRegisters) << ref_reg;
Roland Levillain02b75802016-07-13 11:54:35 +0100501 // "Compact" slow path, saving two moves.
502 //
503 // Instead of using the standard runtime calling convention (input
504 // and output in EAX):
505 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100506 // EAX <- ref
Roland Levillain02b75802016-07-13 11:54:35 +0100507 // EAX <- ReadBarrierMark(EAX)
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100508 // ref <- EAX
Roland Levillain02b75802016-07-13 11:54:35 +0100509 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100510 // we just use rX (the register containing `ref`) as input and output
Roland Levillain02b75802016-07-13 11:54:35 +0100511 // of a dedicated entrypoint:
512 //
513 // rX <- ReadBarrierMarkRegX(rX)
514 //
Roland Levillain97c46462017-05-11 14:04:03 +0100515 int32_t entry_point_offset = Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(ref_reg);
Roland Levillaindec8f632016-07-22 17:10:06 +0100516 // This runtime call does not require a stack map.
517 x86_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillain7c1559a2015-12-15 10:55:36 +0000518 __ jmp(GetExitLabel());
519 }
520
521 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100522 // The location (register) of the marked object reference.
523 const Location ref_;
524 // Should the reference in `ref_` be unpoisoned prior to marking it?
525 const bool unpoison_ref_before_marking_;
Roland Levillain7c1559a2015-12-15 10:55:36 +0000526
527 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86);
528};
529
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100530// Slow path marking an object reference `ref` during a read barrier,
531// and if needed, atomically updating the field `obj.field` in the
532// object `obj` holding this reference after marking (contrary to
533// ReadBarrierMarkSlowPathX86 above, which never tries to update
534// `obj.field`).
535//
536// This means that after the execution of this slow path, both `ref`
537// and `obj.field` will be up-to-date; i.e., after the flip, both will
538// hold the same to-space reference (unless another thread installed
539// another object reference (different from `ref`) in `obj.field`).
540class ReadBarrierMarkAndUpdateFieldSlowPathX86 : public SlowPathCode {
541 public:
542 ReadBarrierMarkAndUpdateFieldSlowPathX86(HInstruction* instruction,
543 Location ref,
544 Register obj,
545 const Address& field_addr,
546 bool unpoison_ref_before_marking,
547 Register temp)
548 : SlowPathCode(instruction),
549 ref_(ref),
550 obj_(obj),
551 field_addr_(field_addr),
552 unpoison_ref_before_marking_(unpoison_ref_before_marking),
553 temp_(temp) {
554 DCHECK(kEmitCompilerReadBarrier);
555 }
556
557 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkAndUpdateFieldSlowPathX86"; }
558
559 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
560 LocationSummary* locations = instruction_->GetLocations();
561 Register ref_reg = ref_.AsRegister<Register>();
562 DCHECK(locations->CanCall());
563 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
564 // This slow path is only used by the UnsafeCASObject intrinsic.
565 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
566 << "Unexpected instruction in read barrier marking and field updating slow path: "
567 << instruction_->DebugName();
568 DCHECK(instruction_->GetLocations()->Intrinsified());
569 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
570
571 __ Bind(GetEntryLabel());
572 if (unpoison_ref_before_marking_) {
573 // Object* ref = ref_addr->AsMirrorPtr()
574 __ MaybeUnpoisonHeapReference(ref_reg);
575 }
576
577 // Save the old (unpoisoned) reference.
578 __ movl(temp_, ref_reg);
579
580 // No need to save live registers; it's taken care of by the
581 // entrypoint. Also, there is no need to update the stack mask,
582 // as this runtime call will not trigger a garbage collection.
583 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
584 DCHECK_NE(ref_reg, ESP);
585 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCpuRegisters) << ref_reg;
586 // "Compact" slow path, saving two moves.
587 //
588 // Instead of using the standard runtime calling convention (input
589 // and output in EAX):
590 //
591 // EAX <- ref
592 // EAX <- ReadBarrierMark(EAX)
593 // ref <- EAX
594 //
595 // we just use rX (the register containing `ref`) as input and output
596 // of a dedicated entrypoint:
597 //
598 // rX <- ReadBarrierMarkRegX(rX)
599 //
Roland Levillain97c46462017-05-11 14:04:03 +0100600 int32_t entry_point_offset = Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(ref_reg);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100601 // This runtime call does not require a stack map.
602 x86_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
603
604 // If the new reference is different from the old reference,
605 // update the field in the holder (`*field_addr`).
606 //
607 // Note that this field could also hold a different object, if
608 // another thread had concurrently changed it. In that case, the
609 // LOCK CMPXCHGL instruction in the compare-and-set (CAS)
610 // operation below would abort the CAS, leaving the field as-is.
611 NearLabel done;
612 __ cmpl(temp_, ref_reg);
613 __ j(kEqual, &done);
614
615 // Update the the holder's field atomically. This may fail if
616 // mutator updates before us, but it's OK. This is achieved
617 // using a strong compare-and-set (CAS) operation with relaxed
618 // memory synchronization ordering, where the expected value is
619 // the old reference and the desired value is the new reference.
620 // This operation is implemented with a 32-bit LOCK CMPXLCHG
621 // instruction, which requires the expected value (the old
622 // reference) to be in EAX. Save EAX beforehand, and move the
623 // expected value (stored in `temp_`) into EAX.
624 __ pushl(EAX);
625 __ movl(EAX, temp_);
626
627 // Convenience aliases.
628 Register base = obj_;
629 Register expected = EAX;
630 Register value = ref_reg;
631
632 bool base_equals_value = (base == value);
633 if (kPoisonHeapReferences) {
634 if (base_equals_value) {
635 // If `base` and `value` are the same register location, move
636 // `value` to a temporary register. This way, poisoning
637 // `value` won't invalidate `base`.
638 value = temp_;
639 __ movl(value, base);
640 }
641
642 // Check that the register allocator did not assign the location
643 // of `expected` (EAX) to `value` nor to `base`, so that heap
644 // poisoning (when enabled) works as intended below.
645 // - If `value` were equal to `expected`, both references would
646 // be poisoned twice, meaning they would not be poisoned at
647 // all, as heap poisoning uses address negation.
648 // - If `base` were equal to `expected`, poisoning `expected`
649 // would invalidate `base`.
650 DCHECK_NE(value, expected);
651 DCHECK_NE(base, expected);
652
653 __ PoisonHeapReference(expected);
654 __ PoisonHeapReference(value);
655 }
656
657 __ LockCmpxchgl(field_addr_, value);
658
659 // If heap poisoning is enabled, we need to unpoison the values
660 // that were poisoned earlier.
661 if (kPoisonHeapReferences) {
662 if (base_equals_value) {
663 // `value` has been moved to a temporary register, no need
664 // to unpoison it.
665 } else {
666 __ UnpoisonHeapReference(value);
667 }
668 // No need to unpoison `expected` (EAX), as it is be overwritten below.
669 }
670
671 // Restore EAX.
672 __ popl(EAX);
673
674 __ Bind(&done);
675 __ jmp(GetExitLabel());
676 }
677
678 private:
679 // The location (register) of the marked object reference.
680 const Location ref_;
681 // The register containing the object holding the marked object reference field.
682 const Register obj_;
683 // The address of the marked reference field. The base of this address must be `obj_`.
684 const Address field_addr_;
685
686 // Should the reference in `ref_` be unpoisoned prior to marking it?
687 const bool unpoison_ref_before_marking_;
688
689 const Register temp_;
690
691 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathX86);
692};
693
Roland Levillain0d5a2812015-11-13 10:07:31 +0000694// Slow path generating a read barrier for a heap reference.
695class ReadBarrierForHeapReferenceSlowPathX86 : public SlowPathCode {
696 public:
697 ReadBarrierForHeapReferenceSlowPathX86(HInstruction* instruction,
698 Location out,
699 Location ref,
700 Location obj,
701 uint32_t offset,
702 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000703 : SlowPathCode(instruction),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000704 out_(out),
705 ref_(ref),
706 obj_(obj),
707 offset_(offset),
708 index_(index) {
709 DCHECK(kEmitCompilerReadBarrier);
710 // If `obj` is equal to `out` or `ref`, it means the initial object
711 // has been overwritten by (or after) the heap object reference load
712 // to be instrumented, e.g.:
713 //
714 // __ movl(out, Address(out, offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000715 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000716 //
717 // In that case, we have lost the information about the original
718 // object, and the emitted read barrier cannot work properly.
719 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
720 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
721 }
722
723 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
724 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
725 LocationSummary* locations = instruction_->GetLocations();
726 Register reg_out = out_.AsRegister<Register>();
727 DCHECK(locations->CanCall());
728 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain3d312422016-06-23 13:53:42 +0100729 DCHECK(instruction_->IsInstanceFieldGet() ||
730 instruction_->IsStaticFieldGet() ||
731 instruction_->IsArrayGet() ||
732 instruction_->IsInstanceOf() ||
733 instruction_->IsCheckCast() ||
Andreas Gamped9911ee2017-03-27 13:27:24 -0700734 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain7c1559a2015-12-15 10:55:36 +0000735 << "Unexpected instruction in read barrier for heap reference slow path: "
736 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000737
738 __ Bind(GetEntryLabel());
739 SaveLiveRegisters(codegen, locations);
740
741 // We may have to change the index's value, but as `index_` is a
742 // constant member (like other "inputs" of this slow path),
743 // introduce a copy of it, `index`.
744 Location index = index_;
745 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100746 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain0d5a2812015-11-13 10:07:31 +0000747 if (instruction_->IsArrayGet()) {
748 // Compute the actual memory offset and store it in `index`.
749 Register index_reg = index_.AsRegister<Register>();
750 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
751 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
752 // We are about to change the value of `index_reg` (see the
753 // calls to art::x86::X86Assembler::shll and
754 // art::x86::X86Assembler::AddImmediate below), but it has
755 // not been saved by the previous call to
756 // art::SlowPathCode::SaveLiveRegisters, as it is a
757 // callee-save register --
758 // art::SlowPathCode::SaveLiveRegisters does not consider
759 // callee-save registers, as it has been designed with the
760 // assumption that callee-save registers are supposed to be
761 // handled by the called function. So, as a callee-save
762 // register, `index_reg` _would_ eventually be saved onto
763 // the stack, but it would be too late: we would have
764 // changed its value earlier. Therefore, we manually save
765 // it here into another freely available register,
766 // `free_reg`, chosen of course among the caller-save
767 // registers (as a callee-save `free_reg` register would
768 // exhibit the same problem).
769 //
770 // Note we could have requested a temporary register from
771 // the register allocator instead; but we prefer not to, as
772 // this is a slow path, and we know we can find a
773 // caller-save register that is available.
774 Register free_reg = FindAvailableCallerSaveRegister(codegen);
775 __ movl(free_reg, index_reg);
776 index_reg = free_reg;
777 index = Location::RegisterLocation(index_reg);
778 } else {
779 // The initial register stored in `index_` has already been
780 // saved in the call to art::SlowPathCode::SaveLiveRegisters
781 // (as it is not a callee-save register), so we can freely
782 // use it.
783 }
784 // Shifting the index value contained in `index_reg` by the scale
785 // factor (2) cannot overflow in practice, as the runtime is
786 // unable to allocate object arrays with a size larger than
787 // 2^26 - 1 (that is, 2^28 - 4 bytes).
788 __ shll(index_reg, Immediate(TIMES_4));
789 static_assert(
790 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
791 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
792 __ AddImmediate(index_reg, Immediate(offset_));
793 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100794 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
795 // intrinsics, `index_` is not shifted by a scale factor of 2
796 // (as in the case of ArrayGet), as it is actually an offset
797 // to an object field within an object.
798 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000799 DCHECK(instruction_->GetLocations()->Intrinsified());
800 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
801 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
802 << instruction_->AsInvoke()->GetIntrinsic();
803 DCHECK_EQ(offset_, 0U);
804 DCHECK(index_.IsRegisterPair());
805 // UnsafeGet's offset location is a register pair, the low
806 // part contains the correct offset.
807 index = index_.ToLow();
808 }
809 }
810
811 // We're moving two or three locations to locations that could
812 // overlap, so we need a parallel move resolver.
813 InvokeRuntimeCallingConvention calling_convention;
814 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
815 parallel_move.AddMove(ref_,
816 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100817 DataType::Type::kReference,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000818 nullptr);
819 parallel_move.AddMove(obj_,
820 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100821 DataType::Type::kReference,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000822 nullptr);
823 if (index.IsValid()) {
824 parallel_move.AddMove(index,
825 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100826 DataType::Type::kInt32,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000827 nullptr);
828 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
829 } else {
830 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
831 __ movl(calling_convention.GetRegisterAt(2), Immediate(offset_));
832 }
Serban Constantinescuba45db02016-07-12 22:53:02 +0100833 x86_codegen->InvokeRuntime(kQuickReadBarrierSlow, instruction_, instruction_->GetDexPc(), this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000834 CheckEntrypointTypes<
835 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
836 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
837
838 RestoreLiveRegisters(codegen, locations);
839 __ jmp(GetExitLabel());
840 }
841
842 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathX86"; }
843
844 private:
845 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
846 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
847 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
848 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
849 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
850 return static_cast<Register>(i);
851 }
852 }
853 // We shall never fail to find a free caller-save register, as
854 // there are more than two core caller-save registers on x86
855 // (meaning it is possible to find one which is different from
856 // `ref` and `obj`).
857 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
858 LOG(FATAL) << "Could not find a free caller-save register";
859 UNREACHABLE();
860 }
861
Roland Levillain0d5a2812015-11-13 10:07:31 +0000862 const Location out_;
863 const Location ref_;
864 const Location obj_;
865 const uint32_t offset_;
866 // An additional location containing an index to an array.
867 // Only used for HArrayGet and the UnsafeGetObject &
868 // UnsafeGetObjectVolatile intrinsics.
869 const Location index_;
870
871 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86);
872};
873
874// Slow path generating a read barrier for a GC root.
875class ReadBarrierForRootSlowPathX86 : public SlowPathCode {
876 public:
877 ReadBarrierForRootSlowPathX86(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000878 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000879 DCHECK(kEmitCompilerReadBarrier);
880 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000881
882 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
883 LocationSummary* locations = instruction_->GetLocations();
884 Register reg_out = out_.AsRegister<Register>();
885 DCHECK(locations->CanCall());
886 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000887 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
888 << "Unexpected instruction in read barrier for GC root slow path: "
889 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000890
891 __ Bind(GetEntryLabel());
892 SaveLiveRegisters(codegen, locations);
893
894 InvokeRuntimeCallingConvention calling_convention;
895 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
896 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100897 x86_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000898 instruction_,
899 instruction_->GetDexPc(),
900 this);
901 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
902 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
903
904 RestoreLiveRegisters(codegen, locations);
905 __ jmp(GetExitLabel());
906 }
907
908 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathX86"; }
909
910 private:
Roland Levillain0d5a2812015-11-13 10:07:31 +0000911 const Location out_;
912 const Location root_;
913
914 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86);
915};
916
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100917#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100918// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
919#define __ down_cast<X86Assembler*>(GetAssembler())-> // NOLINT
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100920
Aart Bike9f37602015-10-09 11:15:55 -0700921inline Condition X86Condition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700922 switch (cond) {
923 case kCondEQ: return kEqual;
924 case kCondNE: return kNotEqual;
925 case kCondLT: return kLess;
926 case kCondLE: return kLessEqual;
927 case kCondGT: return kGreater;
928 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700929 case kCondB: return kBelow;
930 case kCondBE: return kBelowEqual;
931 case kCondA: return kAbove;
932 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700933 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100934 LOG(FATAL) << "Unreachable";
935 UNREACHABLE();
936}
937
Aart Bike9f37602015-10-09 11:15:55 -0700938// Maps signed condition to unsigned condition and FP condition to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100939inline Condition X86UnsignedOrFPCondition(IfCondition cond) {
940 switch (cond) {
941 case kCondEQ: return kEqual;
942 case kCondNE: return kNotEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700943 // Signed to unsigned, and FP to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100944 case kCondLT: return kBelow;
945 case kCondLE: return kBelowEqual;
946 case kCondGT: return kAbove;
947 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700948 // Unsigned remain unchanged.
949 case kCondB: return kBelow;
950 case kCondBE: return kBelowEqual;
951 case kCondA: return kAbove;
952 case kCondAE: return kAboveEqual;
Roland Levillain4fa13f62015-07-06 18:11:54 +0100953 }
954 LOG(FATAL) << "Unreachable";
955 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700956}
957
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100958void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100959 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100960}
961
962void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100963 stream << XmmRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100964}
965
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100966size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
967 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
968 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100969}
970
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100971size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
972 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
973 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100974}
975
Mark Mendell7c8d0092015-01-26 11:21:33 -0500976size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Aart Bikb13c65b2017-03-21 20:14:07 -0700977 if (GetGraph()->HasSIMD()) {
Aart Bik5576f372017-03-23 16:17:37 -0700978 __ movups(Address(ESP, stack_index), XmmRegister(reg_id));
Aart Bikb13c65b2017-03-21 20:14:07 -0700979 } else {
980 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
981 }
Mark Mendell7c8d0092015-01-26 11:21:33 -0500982 return GetFloatingPointSpillSlotSize();
983}
984
985size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Aart Bikb13c65b2017-03-21 20:14:07 -0700986 if (GetGraph()->HasSIMD()) {
Aart Bik5576f372017-03-23 16:17:37 -0700987 __ movups(XmmRegister(reg_id), Address(ESP, stack_index));
Aart Bikb13c65b2017-03-21 20:14:07 -0700988 } else {
989 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
990 }
Mark Mendell7c8d0092015-01-26 11:21:33 -0500991 return GetFloatingPointSpillSlotSize();
992}
993
Calin Juravle175dc732015-08-25 15:42:32 +0100994void CodeGeneratorX86::InvokeRuntime(QuickEntrypointEnum entrypoint,
995 HInstruction* instruction,
996 uint32_t dex_pc,
997 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +0100998 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100999 GenerateInvokeRuntime(GetThreadOffset<kX86PointerSize>(entrypoint).Int32Value());
1000 if (EntrypointRequiresStackMap(entrypoint)) {
1001 RecordPcInfo(instruction, dex_pc, slow_path);
1002 }
Alexandre Rames8158f282015-08-07 10:26:17 +01001003}
1004
Roland Levillaindec8f632016-07-22 17:10:06 +01001005void CodeGeneratorX86::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1006 HInstruction* instruction,
1007 SlowPathCode* slow_path) {
1008 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +01001009 GenerateInvokeRuntime(entry_point_offset);
1010}
1011
1012void CodeGeneratorX86::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001013 __ fs()->call(Address::Absolute(entry_point_offset));
1014}
1015
Mark Mendellfb8d2792015-03-31 22:16:59 -04001016CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +00001017 const X86InstructionSetFeatures& isa_features,
1018 const CompilerOptions& compiler_options,
1019 OptimizingCompilerStats* stats)
Mark Mendell5f874182015-03-04 15:42:45 -05001020 : CodeGenerator(graph,
1021 kNumberOfCpuRegisters,
1022 kNumberOfXmmRegisters,
1023 kNumberOfRegisterPairs,
1024 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1025 arraysize(kCoreCalleeSaves))
1026 | (1 << kFakeReturnRegister),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001027 0,
1028 compiler_options,
1029 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +01001030 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001031 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001032 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -04001033 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001034 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +00001035 isa_features_(isa_features),
Vladimir Marko65979462017-05-19 17:25:12 +01001036 boot_image_method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001037 method_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +00001038 boot_image_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1039 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko65979462017-05-19 17:25:12 +01001040 string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001041 string_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001042 jit_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001043 jit_class_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko93205e32016-04-13 11:59:46 +01001044 constant_area_start_(-1),
1045 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001046 method_address_offset_(std::less<uint32_t>(),
1047 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001048 // Use a fake return address register to mimic Quick.
1049 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001050}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001051
David Brazdil58282f42016-01-14 12:45:10 +00001052void CodeGeneratorX86::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001053 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001054 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001055}
1056
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001057InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001058 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001059 assembler_(codegen->GetAssembler()),
1060 codegen_(codegen) {}
1061
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001062static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001063 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001064}
1065
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001066void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001067 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001068 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +00001069 bool skip_overflow_check =
1070 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001071 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +00001072
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001073 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +01001074 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001075 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +01001076 }
1077
Mark Mendell5f874182015-03-04 15:42:45 -05001078 if (HasEmptyFrame()) {
1079 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001080 }
Mark Mendell5f874182015-03-04 15:42:45 -05001081
1082 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
1083 Register reg = kCoreCalleeSaves[i];
1084 if (allocated_registers_.ContainsCoreRegister(reg)) {
1085 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001086 __ cfi().AdjustCFAOffset(kX86WordSize);
1087 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -05001088 }
1089 }
1090
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001091 int adjust = GetFrameSize() - FrameEntrySpillSize();
1092 __ subl(ESP, Immediate(adjust));
1093 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001094 // Save the current method if we need it. Note that we do not
1095 // do this in HCurrentMethod, as the instruction might have been removed
1096 // in the SSA graph.
1097 if (RequiresCurrentMethod()) {
1098 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
1099 }
Nicolas Geoffrayf7893532017-06-15 12:34:36 +01001100
1101 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1102 // Initialize should_deoptimize flag to 0.
1103 __ movl(Address(ESP, GetStackOffsetOfShouldDeoptimizeFlag()), Immediate(0));
1104 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001105}
1106
1107void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001108 __ cfi().RememberState();
1109 if (!HasEmptyFrame()) {
1110 int adjust = GetFrameSize() - FrameEntrySpillSize();
1111 __ addl(ESP, Immediate(adjust));
1112 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -05001113
David Srbeckyc34dc932015-04-12 09:27:43 +01001114 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1115 Register reg = kCoreCalleeSaves[i];
1116 if (allocated_registers_.ContainsCoreRegister(reg)) {
1117 __ popl(reg);
1118 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
1119 __ cfi().Restore(DWARFReg(reg));
1120 }
Mark Mendell5f874182015-03-04 15:42:45 -05001121 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001122 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001123 __ ret();
1124 __ cfi().RestoreState();
1125 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001126}
1127
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001128void CodeGeneratorX86::Bind(HBasicBlock* block) {
1129 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001130}
1131
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001132Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(DataType::Type type) const {
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001133 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001134 case DataType::Type::kBool:
1135 case DataType::Type::kInt8:
1136 case DataType::Type::kUint16:
1137 case DataType::Type::kInt16:
1138 case DataType::Type::kInt32:
1139 case DataType::Type::kReference:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001140 return Location::RegisterLocation(EAX);
1141
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001142 case DataType::Type::kInt64:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001143 return Location::RegisterPairLocation(EAX, EDX);
1144
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001145 case DataType::Type::kVoid:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001146 return Location::NoLocation();
1147
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001148 case DataType::Type::kFloat64:
1149 case DataType::Type::kFloat32:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001150 return Location::FpuRegisterLocation(XMM0);
1151 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001152
1153 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001154}
1155
1156Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
1157 return Location::RegisterLocation(kMethodRegisterArgument);
1158}
1159
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001160Location InvokeDexCallingConventionVisitorX86::GetNextLocation(DataType::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001161 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001162 case DataType::Type::kBool:
1163 case DataType::Type::kInt8:
1164 case DataType::Type::kUint16:
1165 case DataType::Type::kInt16:
1166 case DataType::Type::kInt32:
1167 case DataType::Type::kReference: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001168 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001169 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001170 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001171 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001172 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001173 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001174 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001175 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001176
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001177 case DataType::Type::kInt64: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001178 uint32_t index = gp_index_;
1179 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001180 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001181 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001182 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
1183 calling_convention.GetRegisterPairAt(index));
1184 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001185 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001186 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1187 }
1188 }
1189
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001190 case DataType::Type::kFloat32: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001191 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001192 stack_index_++;
1193 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1194 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1195 } else {
1196 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1197 }
1198 }
1199
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001200 case DataType::Type::kFloat64: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001201 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001202 stack_index_ += 2;
1203 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1204 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1205 } else {
1206 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001207 }
1208 }
1209
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001210 case DataType::Type::kVoid:
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001211 LOG(FATAL) << "Unexpected parameter type " << type;
1212 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001213 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00001214 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001215}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001216
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001217void CodeGeneratorX86::Move32(Location destination, Location source) {
1218 if (source.Equals(destination)) {
1219 return;
1220 }
1221 if (destination.IsRegister()) {
1222 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001223 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001224 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001225 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001226 } else {
1227 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001228 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001229 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001230 } else if (destination.IsFpuRegister()) {
1231 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001232 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001233 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001234 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001235 } else {
1236 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001237 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001238 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001239 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001240 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001241 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001242 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001243 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001244 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05001245 } else if (source.IsConstant()) {
1246 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001247 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05001248 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001249 } else {
1250 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001251 __ pushl(Address(ESP, source.GetStackIndex()));
1252 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001253 }
1254 }
1255}
1256
1257void CodeGeneratorX86::Move64(Location destination, Location source) {
1258 if (source.Equals(destination)) {
1259 return;
1260 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001261 if (destination.IsRegisterPair()) {
1262 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001263 EmitParallelMoves(
1264 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1265 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001266 DataType::Type::kInt32,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001267 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001268 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001269 DataType::Type::kInt32);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001270 } else if (source.IsFpuRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001271 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
1272 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
1273 __ psrlq(src_reg, Immediate(32));
1274 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001275 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001276 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001277 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001278 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
1279 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001280 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
1281 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001282 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05001283 if (source.IsFpuRegister()) {
1284 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
1285 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001286 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001287 } else if (source.IsRegisterPair()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001288 size_t elem_size = DataType::Size(DataType::Type::kInt32);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001289 // Create stack space for 2 elements.
1290 __ subl(ESP, Immediate(2 * elem_size));
1291 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
1292 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
1293 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
1294 // And remove the temporary stack space we allocated.
1295 __ addl(ESP, Immediate(2 * elem_size));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001296 } else {
1297 LOG(FATAL) << "Unimplemented";
1298 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001299 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001300 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001301 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001302 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001303 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001304 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001305 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001306 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001307 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001308 } else if (source.IsConstant()) {
1309 HConstant* constant = source.GetConstant();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001310 DCHECK(constant->IsLongConstant() || constant->IsDoubleConstant());
1311 int64_t value = GetInt64ValueOf(constant);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001312 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001313 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
1314 Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001315 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001316 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001317 EmitParallelMoves(
1318 Location::StackSlot(source.GetStackIndex()),
1319 Location::StackSlot(destination.GetStackIndex()),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001320 DataType::Type::kInt32,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001321 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001322 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001323 DataType::Type::kInt32);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001324 }
1325 }
1326}
1327
Calin Juravle175dc732015-08-25 15:42:32 +01001328void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
1329 DCHECK(location.IsRegister());
1330 __ movl(location.AsRegister<Register>(), Immediate(value));
1331}
1332
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001333void CodeGeneratorX86::MoveLocation(Location dst, Location src, DataType::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001334 HParallelMove move(GetGraph()->GetArena());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001335 if (dst_type == DataType::Type::kInt64 && !src.IsConstant() && !src.IsFpuRegister()) {
1336 move.AddMove(src.ToLow(), dst.ToLow(), DataType::Type::kInt32, nullptr);
1337 move.AddMove(src.ToHigh(), dst.ToHigh(), DataType::Type::kInt32, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001338 } else {
David Brazdil74eb1b22015-12-14 11:44:01 +00001339 move.AddMove(src, dst, dst_type, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001340 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001341 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001342}
1343
1344void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1345 if (location.IsRegister()) {
1346 locations->AddTemp(location);
1347 } else if (location.IsRegisterPair()) {
1348 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1349 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1350 } else {
1351 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1352 }
1353}
1354
David Brazdilfc6a86a2015-06-26 10:33:45 +00001355void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001356 DCHECK(!successor->IsExitBlock());
1357
1358 HBasicBlock* block = got->GetBlock();
1359 HInstruction* previous = got->GetPrevious();
1360
1361 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001362 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001363 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1364 return;
1365 }
1366
1367 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1368 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1369 }
1370 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001371 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001372 }
1373}
1374
David Brazdilfc6a86a2015-06-26 10:33:45 +00001375void LocationsBuilderX86::VisitGoto(HGoto* got) {
1376 got->SetLocations(nullptr);
1377}
1378
1379void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1380 HandleGoto(got, got->GetSuccessor());
1381}
1382
1383void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1384 try_boundary->SetLocations(nullptr);
1385}
1386
1387void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1388 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1389 if (!successor->IsExitBlock()) {
1390 HandleGoto(try_boundary, successor);
1391 }
1392}
1393
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001394void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001395 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001396}
1397
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001398void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001399}
1400
Mark Mendell152408f2015-12-31 12:28:50 -05001401template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001402void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001403 LabelType* true_label,
1404 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001405 if (cond->IsFPConditionTrueIfNaN()) {
1406 __ j(kUnordered, true_label);
1407 } else if (cond->IsFPConditionFalseIfNaN()) {
1408 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001409 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001410 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001411}
1412
Mark Mendell152408f2015-12-31 12:28:50 -05001413template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001414void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001415 LabelType* true_label,
1416 LabelType* false_label) {
Mark Mendellc4701932015-04-10 13:18:51 -04001417 LocationSummary* locations = cond->GetLocations();
1418 Location left = locations->InAt(0);
1419 Location right = locations->InAt(1);
1420 IfCondition if_cond = cond->GetCondition();
1421
Mark Mendellc4701932015-04-10 13:18:51 -04001422 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001423 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001424 IfCondition true_high_cond = if_cond;
1425 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001426 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001427
1428 // Set the conditions for the test, remembering that == needs to be
1429 // decided using the low words.
1430 switch (if_cond) {
1431 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001432 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001433 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001434 break;
1435 case kCondLT:
1436 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001437 break;
1438 case kCondLE:
1439 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001440 break;
1441 case kCondGT:
1442 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001443 break;
1444 case kCondGE:
1445 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001446 break;
Aart Bike9f37602015-10-09 11:15:55 -07001447 case kCondB:
1448 false_high_cond = kCondA;
1449 break;
1450 case kCondBE:
1451 true_high_cond = kCondB;
1452 break;
1453 case kCondA:
1454 false_high_cond = kCondB;
1455 break;
1456 case kCondAE:
1457 true_high_cond = kCondA;
1458 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001459 }
1460
1461 if (right.IsConstant()) {
1462 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001463 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001464 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001465
Aart Bika19616e2016-02-01 18:57:58 -08001466 codegen_->Compare32BitValue(left_high, val_high);
Mark Mendellc4701932015-04-10 13:18:51 -04001467 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001468 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001469 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001470 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001471 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001472 __ j(X86Condition(true_high_cond), true_label);
1473 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001474 }
1475 // Must be equal high, so compare the lows.
Aart Bika19616e2016-02-01 18:57:58 -08001476 codegen_->Compare32BitValue(left_low, val_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001477 } else if (right.IsRegisterPair()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001478 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001479 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001480
1481 __ cmpl(left_high, right_high);
1482 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001483 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001484 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001485 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001486 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001487 __ j(X86Condition(true_high_cond), true_label);
1488 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001489 }
1490 // Must be equal high, so compare the lows.
1491 __ cmpl(left_low, right_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001492 } else {
1493 DCHECK(right.IsDoubleStackSlot());
1494 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
1495 if (if_cond == kCondNE) {
1496 __ j(X86Condition(true_high_cond), true_label);
1497 } else if (if_cond == kCondEQ) {
1498 __ j(X86Condition(false_high_cond), false_label);
1499 } else {
1500 __ j(X86Condition(true_high_cond), true_label);
1501 __ j(X86Condition(false_high_cond), false_label);
1502 }
1503 // Must be equal high, so compare the lows.
1504 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Mark Mendellc4701932015-04-10 13:18:51 -04001505 }
1506 // The last comparison might be unsigned.
1507 __ j(final_condition, true_label);
1508}
1509
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001510void InstructionCodeGeneratorX86::GenerateFPCompare(Location lhs,
1511 Location rhs,
1512 HInstruction* insn,
1513 bool is_double) {
1514 HX86LoadFromConstantTable* const_area = insn->InputAt(1)->AsX86LoadFromConstantTable();
1515 if (is_double) {
1516 if (rhs.IsFpuRegister()) {
1517 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1518 } else if (const_area != nullptr) {
1519 DCHECK(const_area->IsEmittedAtUseSite());
1520 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(),
1521 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001522 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
1523 const_area->GetBaseMethodAddress(),
1524 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001525 } else {
1526 DCHECK(rhs.IsDoubleStackSlot());
1527 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1528 }
1529 } else {
1530 if (rhs.IsFpuRegister()) {
1531 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1532 } else if (const_area != nullptr) {
1533 DCHECK(const_area->IsEmittedAtUseSite());
1534 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(),
1535 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001536 const_area->GetConstant()->AsFloatConstant()->GetValue(),
1537 const_area->GetBaseMethodAddress(),
1538 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001539 } else {
1540 DCHECK(rhs.IsStackSlot());
1541 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1542 }
1543 }
1544}
1545
Mark Mendell152408f2015-12-31 12:28:50 -05001546template<class LabelType>
David Brazdil0debae72015-11-12 18:37:00 +00001547void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendell152408f2015-12-31 12:28:50 -05001548 LabelType* true_target_in,
1549 LabelType* false_target_in) {
David Brazdil0debae72015-11-12 18:37:00 +00001550 // Generated branching requires both targets to be explicit. If either of the
1551 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Mark Mendell152408f2015-12-31 12:28:50 -05001552 LabelType fallthrough_target;
1553 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1554 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
David Brazdil0debae72015-11-12 18:37:00 +00001555
Mark Mendellc4701932015-04-10 13:18:51 -04001556 LocationSummary* locations = condition->GetLocations();
1557 Location left = locations->InAt(0);
1558 Location right = locations->InAt(1);
1559
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001560 DataType::Type type = condition->InputAt(0)->GetType();
Mark Mendellc4701932015-04-10 13:18:51 -04001561 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001562 case DataType::Type::kInt64:
Mark Mendellc4701932015-04-10 13:18:51 -04001563 GenerateLongComparesAndJumps(condition, true_target, false_target);
1564 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001565 case DataType::Type::kFloat32:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001566 GenerateFPCompare(left, right, condition, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001567 GenerateFPJumps(condition, true_target, false_target);
1568 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001569 case DataType::Type::kFloat64:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001570 GenerateFPCompare(left, right, condition, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001571 GenerateFPJumps(condition, true_target, false_target);
1572 break;
1573 default:
1574 LOG(FATAL) << "Unexpected compare type " << type;
1575 }
1576
David Brazdil0debae72015-11-12 18:37:00 +00001577 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001578 __ jmp(false_target);
1579 }
David Brazdil0debae72015-11-12 18:37:00 +00001580
1581 if (fallthrough_target.IsLinked()) {
1582 __ Bind(&fallthrough_target);
1583 }
Mark Mendellc4701932015-04-10 13:18:51 -04001584}
1585
David Brazdil0debae72015-11-12 18:37:00 +00001586static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1587 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1588 // are set only strictly before `branch`. We can't use the eflags on long/FP
1589 // conditions if they are materialized due to the complex branching.
1590 return cond->IsCondition() &&
1591 cond->GetNext() == branch &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001592 cond->InputAt(0)->GetType() != DataType::Type::kInt64 &&
1593 !DataType::IsFloatingPointType(cond->InputAt(0)->GetType());
David Brazdil0debae72015-11-12 18:37:00 +00001594}
1595
Mark Mendell152408f2015-12-31 12:28:50 -05001596template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001597void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001598 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001599 LabelType* true_target,
1600 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001601 HInstruction* cond = instruction->InputAt(condition_input_index);
1602
1603 if (true_target == nullptr && false_target == nullptr) {
1604 // Nothing to do. The code always falls through.
1605 return;
1606 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001607 // Constant condition, statically compared against "true" (integer value 1).
1608 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001609 if (true_target != nullptr) {
1610 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001611 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001612 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001613 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001614 if (false_target != nullptr) {
1615 __ jmp(false_target);
1616 }
1617 }
1618 return;
1619 }
1620
1621 // The following code generates these patterns:
1622 // (1) true_target == nullptr && false_target != nullptr
1623 // - opposite condition true => branch to false_target
1624 // (2) true_target != nullptr && false_target == nullptr
1625 // - condition true => branch to true_target
1626 // (3) true_target != nullptr && false_target != nullptr
1627 // - condition true => branch to true_target
1628 // - branch to false_target
1629 if (IsBooleanValueOrMaterializedCondition(cond)) {
1630 if (AreEflagsSetFrom(cond, instruction)) {
1631 if (true_target == nullptr) {
1632 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
1633 } else {
1634 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
1635 }
1636 } else {
1637 // Materialized condition, compare against 0.
1638 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1639 if (lhs.IsRegister()) {
1640 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1641 } else {
1642 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1643 }
1644 if (true_target == nullptr) {
1645 __ j(kEqual, false_target);
1646 } else {
1647 __ j(kNotEqual, true_target);
1648 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001649 }
1650 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001651 // Condition has not been materialized, use its inputs as the comparison and
1652 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001653 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001654
1655 // If this is a long or FP comparison that has been folded into
1656 // the HCondition, generate the comparison directly.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001657 DataType::Type type = condition->InputAt(0)->GetType();
1658 if (type == DataType::Type::kInt64 || DataType::IsFloatingPointType(type)) {
David Brazdil0debae72015-11-12 18:37:00 +00001659 GenerateCompareTestAndBranch(condition, true_target, false_target);
1660 return;
1661 }
1662
1663 Location lhs = condition->GetLocations()->InAt(0);
1664 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001665 // LHS is guaranteed to be in a register (see LocationsBuilderX86::HandleCondition).
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001666 codegen_->GenerateIntCompare(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00001667 if (true_target == nullptr) {
1668 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1669 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001670 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001671 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001672 }
David Brazdil0debae72015-11-12 18:37:00 +00001673
1674 // If neither branch falls through (case 3), the conditional branch to `true_target`
1675 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1676 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001677 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001678 }
1679}
1680
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001681void LocationsBuilderX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001682 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1683 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001684 locations->SetInAt(0, Location::Any());
1685 }
1686}
1687
1688void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001689 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1690 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1691 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1692 nullptr : codegen_->GetLabelOf(true_successor);
1693 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1694 nullptr : codegen_->GetLabelOf(false_successor);
1695 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001696}
1697
1698void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1699 LocationSummary* locations = new (GetGraph()->GetArena())
1700 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01001701 InvokeRuntimeCallingConvention calling_convention;
1702 RegisterSet caller_saves = RegisterSet::Empty();
1703 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1704 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00001705 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001706 locations->SetInAt(0, Location::Any());
1707 }
1708}
1709
1710void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001711 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001712 GenerateTestAndBranch<Label>(deoptimize,
1713 /* condition_input_index */ 0,
1714 slow_path->GetEntryLabel(),
1715 /* false_target */ nullptr);
1716}
1717
Mingyao Yang063fc772016-08-02 11:02:54 -07001718void LocationsBuilderX86::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
1719 LocationSummary* locations = new (GetGraph()->GetArena())
1720 LocationSummary(flag, LocationSummary::kNoCall);
1721 locations->SetOut(Location::RequiresRegister());
1722}
1723
1724void InstructionCodeGeneratorX86::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
1725 __ movl(flag->GetLocations()->Out().AsRegister<Register>(),
1726 Address(ESP, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
1727}
1728
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001729static bool SelectCanUseCMOV(HSelect* select) {
1730 // There are no conditional move instructions for XMMs.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001731 if (DataType::IsFloatingPointType(select->GetType())) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001732 return false;
1733 }
1734
1735 // A FP condition doesn't generate the single CC that we need.
1736 // In 32 bit mode, a long condition doesn't generate a single CC either.
1737 HInstruction* condition = select->GetCondition();
1738 if (condition->IsCondition()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001739 DataType::Type compare_type = condition->InputAt(0)->GetType();
1740 if (compare_type == DataType::Type::kInt64 ||
1741 DataType::IsFloatingPointType(compare_type)) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001742 return false;
1743 }
1744 }
1745
1746 // We can generate a CMOV for this Select.
1747 return true;
1748}
1749
David Brazdil74eb1b22015-12-14 11:44:01 +00001750void LocationsBuilderX86::VisitSelect(HSelect* select) {
1751 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001752 if (DataType::IsFloatingPointType(select->GetType())) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001753 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001754 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001755 } else {
1756 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001757 if (SelectCanUseCMOV(select)) {
1758 if (select->InputAt(1)->IsConstant()) {
1759 // Cmov can't handle a constant value.
1760 locations->SetInAt(1, Location::RequiresRegister());
1761 } else {
1762 locations->SetInAt(1, Location::Any());
1763 }
1764 } else {
1765 locations->SetInAt(1, Location::Any());
1766 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001767 }
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001768 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1769 locations->SetInAt(2, Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00001770 }
1771 locations->SetOut(Location::SameAsFirstInput());
1772}
1773
1774void InstructionCodeGeneratorX86::VisitSelect(HSelect* select) {
1775 LocationSummary* locations = select->GetLocations();
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001776 DCHECK(locations->InAt(0).Equals(locations->Out()));
1777 if (SelectCanUseCMOV(select)) {
1778 // If both the condition and the source types are integer, we can generate
1779 // a CMOV to implement Select.
1780
1781 HInstruction* select_condition = select->GetCondition();
1782 Condition cond = kNotEqual;
1783
1784 // Figure out how to test the 'condition'.
1785 if (select_condition->IsCondition()) {
1786 HCondition* condition = select_condition->AsCondition();
1787 if (!condition->IsEmittedAtUseSite()) {
1788 // This was a previously materialized condition.
1789 // Can we use the existing condition code?
1790 if (AreEflagsSetFrom(condition, select)) {
1791 // Materialization was the previous instruction. Condition codes are right.
1792 cond = X86Condition(condition->GetCondition());
1793 } else {
1794 // No, we have to recreate the condition code.
1795 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1796 __ testl(cond_reg, cond_reg);
1797 }
1798 } else {
1799 // We can't handle FP or long here.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001800 DCHECK_NE(condition->InputAt(0)->GetType(), DataType::Type::kInt64);
1801 DCHECK(!DataType::IsFloatingPointType(condition->InputAt(0)->GetType()));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001802 LocationSummary* cond_locations = condition->GetLocations();
Roland Levillain0b671c02016-08-19 12:02:34 +01001803 codegen_->GenerateIntCompare(cond_locations->InAt(0), cond_locations->InAt(1));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001804 cond = X86Condition(condition->GetCondition());
1805 }
1806 } else {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01001807 // Must be a Boolean condition, which needs to be compared to 0.
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001808 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1809 __ testl(cond_reg, cond_reg);
1810 }
1811
1812 // If the condition is true, overwrite the output, which already contains false.
1813 Location false_loc = locations->InAt(0);
1814 Location true_loc = locations->InAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001815 if (select->GetType() == DataType::Type::kInt64) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001816 // 64 bit conditional move.
1817 Register false_high = false_loc.AsRegisterPairHigh<Register>();
1818 Register false_low = false_loc.AsRegisterPairLow<Register>();
1819 if (true_loc.IsRegisterPair()) {
1820 __ cmovl(cond, false_high, true_loc.AsRegisterPairHigh<Register>());
1821 __ cmovl(cond, false_low, true_loc.AsRegisterPairLow<Register>());
1822 } else {
1823 __ cmovl(cond, false_high, Address(ESP, true_loc.GetHighStackIndex(kX86WordSize)));
1824 __ cmovl(cond, false_low, Address(ESP, true_loc.GetStackIndex()));
1825 }
1826 } else {
1827 // 32 bit conditional move.
1828 Register false_reg = false_loc.AsRegister<Register>();
1829 if (true_loc.IsRegister()) {
1830 __ cmovl(cond, false_reg, true_loc.AsRegister<Register>());
1831 } else {
1832 __ cmovl(cond, false_reg, Address(ESP, true_loc.GetStackIndex()));
1833 }
1834 }
1835 } else {
1836 NearLabel false_target;
1837 GenerateTestAndBranch<NearLabel>(
1838 select, /* condition_input_index */ 2, /* true_target */ nullptr, &false_target);
1839 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1840 __ Bind(&false_target);
1841 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001842}
1843
David Srbecky0cf44932015-12-09 14:09:59 +00001844void LocationsBuilderX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1845 new (GetGraph()->GetArena()) LocationSummary(info);
1846}
1847
David Srbeckyd28f4a02016-03-14 17:14:24 +00001848void InstructionCodeGeneratorX86::VisitNativeDebugInfo(HNativeDebugInfo*) {
1849 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001850}
1851
1852void CodeGeneratorX86::GenerateNop() {
1853 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001854}
1855
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001856void LocationsBuilderX86::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001857 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001858 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001859 // Handle the long/FP comparisons made in instruction simplification.
1860 switch (cond->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001861 case DataType::Type::kInt64: {
Mark Mendellc4701932015-04-10 13:18:51 -04001862 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell8659e842016-02-16 10:41:46 -05001863 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001864 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001865 locations->SetOut(Location::RequiresRegister());
1866 }
1867 break;
1868 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001869 case DataType::Type::kFloat32:
1870 case DataType::Type::kFloat64: {
Mark Mendellc4701932015-04-10 13:18:51 -04001871 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001872 if (cond->InputAt(1)->IsX86LoadFromConstantTable()) {
1873 DCHECK(cond->InputAt(1)->IsEmittedAtUseSite());
1874 } else if (cond->InputAt(1)->IsConstant()) {
1875 locations->SetInAt(1, Location::RequiresFpuRegister());
1876 } else {
1877 locations->SetInAt(1, Location::Any());
1878 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001879 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001880 locations->SetOut(Location::RequiresRegister());
1881 }
1882 break;
1883 }
1884 default:
1885 locations->SetInAt(0, Location::RequiresRegister());
1886 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001887 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001888 // We need a byte register.
1889 locations->SetOut(Location::RegisterLocation(ECX));
1890 }
1891 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001892 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001893}
1894
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001895void InstructionCodeGeneratorX86::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001896 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001897 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001898 }
Mark Mendellc4701932015-04-10 13:18:51 -04001899
1900 LocationSummary* locations = cond->GetLocations();
1901 Location lhs = locations->InAt(0);
1902 Location rhs = locations->InAt(1);
1903 Register reg = locations->Out().AsRegister<Register>();
Mark Mendell152408f2015-12-31 12:28:50 -05001904 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001905
1906 switch (cond->InputAt(0)->GetType()) {
1907 default: {
1908 // Integer case.
1909
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01001910 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04001911 __ xorl(reg, reg);
Roland Levillain0b671c02016-08-19 12:02:34 +01001912 codegen_->GenerateIntCompare(lhs, rhs);
Aart Bike9f37602015-10-09 11:15:55 -07001913 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001914 return;
1915 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001916 case DataType::Type::kInt64:
Mark Mendellc4701932015-04-10 13:18:51 -04001917 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1918 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001919 case DataType::Type::kFloat32:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001920 GenerateFPCompare(lhs, rhs, cond, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001921 GenerateFPJumps(cond, &true_label, &false_label);
1922 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001923 case DataType::Type::kFloat64:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001924 GenerateFPCompare(lhs, rhs, cond, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001925 GenerateFPJumps(cond, &true_label, &false_label);
1926 break;
1927 }
1928
1929 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001930 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001931
Roland Levillain4fa13f62015-07-06 18:11:54 +01001932 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001933 __ Bind(&false_label);
1934 __ xorl(reg, reg);
1935 __ jmp(&done_label);
1936
Roland Levillain4fa13f62015-07-06 18:11:54 +01001937 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001938 __ Bind(&true_label);
1939 __ movl(reg, Immediate(1));
1940 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001941}
1942
1943void LocationsBuilderX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001944 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001945}
1946
1947void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001948 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001949}
1950
1951void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001952 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001953}
1954
1955void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001956 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001957}
1958
1959void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001960 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001961}
1962
1963void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001964 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001965}
1966
1967void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001968 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001969}
1970
1971void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001972 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001973}
1974
1975void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001976 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001977}
1978
1979void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001980 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001981}
1982
1983void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001984 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001985}
1986
1987void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001988 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001989}
1990
Aart Bike9f37602015-10-09 11:15:55 -07001991void LocationsBuilderX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001992 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001993}
1994
1995void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001996 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001997}
1998
1999void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002000 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002001}
2002
2003void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002004 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002005}
2006
2007void LocationsBuilderX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002008 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002009}
2010
2011void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002012 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002013}
2014
2015void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002016 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002017}
2018
2019void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002020 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002021}
2022
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002023void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002024 LocationSummary* locations =
2025 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002026 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002027}
2028
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002029void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002030 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002031}
2032
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002033void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
2034 LocationSummary* locations =
2035 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2036 locations->SetOut(Location::ConstantLocation(constant));
2037}
2038
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002039void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002040 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002041}
2042
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002043void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002044 LocationSummary* locations =
2045 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002046 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002047}
2048
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002049void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002050 // Will be generated at use site.
2051}
2052
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002053void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
2054 LocationSummary* locations =
2055 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2056 locations->SetOut(Location::ConstantLocation(constant));
2057}
2058
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002059void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002060 // Will be generated at use site.
2061}
2062
2063void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
2064 LocationSummary* locations =
2065 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2066 locations->SetOut(Location::ConstantLocation(constant));
2067}
2068
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002069void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002070 // Will be generated at use site.
2071}
2072
Igor Murashkind01745e2017-04-05 16:40:31 -07002073void LocationsBuilderX86::VisitConstructorFence(HConstructorFence* constructor_fence) {
2074 constructor_fence->SetLocations(nullptr);
2075}
2076
2077void InstructionCodeGeneratorX86::VisitConstructorFence(
2078 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
2079 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
2080}
2081
Calin Juravle27df7582015-04-17 19:12:31 +01002082void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2083 memory_barrier->SetLocations(nullptr);
2084}
2085
2086void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00002087 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002088}
2089
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002090void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002091 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002092}
2093
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002094void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002095 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002096}
2097
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002098void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002099 LocationSummary* locations =
2100 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002101 switch (ret->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002102 case DataType::Type::kBool:
2103 case DataType::Type::kInt8:
2104 case DataType::Type::kUint16:
2105 case DataType::Type::kInt16:
2106 case DataType::Type::kInt32:
2107 case DataType::Type::kReference:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002108 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002109 break;
2110
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002111 case DataType::Type::kInt64:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002112 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002113 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002114 break;
2115
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002116 case DataType::Type::kFloat32:
2117 case DataType::Type::kFloat64:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002118 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002119 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002120 break;
2121
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002122 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002123 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002124 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002125}
2126
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002127void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002128 if (kIsDebugBuild) {
2129 switch (ret->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002130 case DataType::Type::kBool:
2131 case DataType::Type::kInt8:
2132 case DataType::Type::kUint16:
2133 case DataType::Type::kInt16:
2134 case DataType::Type::kInt32:
2135 case DataType::Type::kReference:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002136 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002137 break;
2138
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002139 case DataType::Type::kInt64:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002140 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
2141 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002142 break;
2143
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002144 case DataType::Type::kFloat32:
2145 case DataType::Type::kFloat64:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002146 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002147 break;
2148
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002149 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002150 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002151 }
2152 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002153 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002154}
2155
Calin Juravle175dc732015-08-25 15:42:32 +01002156void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2157 // The trampoline uses the same calling convention as dex calling conventions,
2158 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2159 // the method_idx.
2160 HandleInvoke(invoke);
2161}
2162
2163void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2164 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2165}
2166
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002167void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002168 // Explicit clinit checks triggered by static invokes must have been pruned by
2169 // art::PrepareForRegisterAllocation.
2170 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002171
Mark Mendellfb8d2792015-03-31 22:16:59 -04002172 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002173 if (intrinsic.TryDispatch(invoke)) {
Vladimir Marko65979462017-05-19 17:25:12 +01002174 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeMethodLoadKind()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00002175 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002176 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04002177 return;
2178 }
2179
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002180 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002181
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002182 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
Vladimir Marko65979462017-05-19 17:25:12 +01002183 if (invoke->HasPcRelativeMethodLoadKind()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00002184 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002185 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002186}
2187
Mark Mendell09ed1a32015-03-25 08:30:06 -04002188static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
2189 if (invoke->GetLocations()->Intrinsified()) {
2190 IntrinsicCodeGeneratorX86 intrinsic(codegen);
2191 intrinsic.Dispatch(invoke);
2192 return true;
2193 }
2194 return false;
2195}
2196
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002197void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002198 // Explicit clinit checks triggered by static invokes must have been pruned by
2199 // art::PrepareForRegisterAllocation.
2200 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002201
Mark Mendell09ed1a32015-03-25 08:30:06 -04002202 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2203 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002204 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002205
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002206 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04002207 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002208 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002209}
2210
2211void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00002212 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
2213 if (intrinsic.TryDispatch(invoke)) {
2214 return;
2215 }
2216
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002217 HandleInvoke(invoke);
2218}
2219
2220void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002221 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002222 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002223}
2224
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002225void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002226 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2227 return;
2228 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002229
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002230 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002231 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002232}
2233
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002234void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002235 // This call to HandleInvoke allocates a temporary (core) register
2236 // which is also used to transfer the hidden argument from FP to
2237 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002238 HandleInvoke(invoke);
2239 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002240 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002241}
2242
2243void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
2244 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002245 LocationSummary* locations = invoke->GetLocations();
2246 Register temp = locations->GetTemp(0).AsRegister<Register>();
2247 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002248 Location receiver = locations->InAt(0);
2249 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2250
Roland Levillain0d5a2812015-11-13 10:07:31 +00002251 // Set the hidden argument. This is safe to do this here, as XMM7
2252 // won't be modified thereafter, before the `call` instruction.
2253 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002254 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002255 __ movd(hidden_reg, temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002256
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002257 if (receiver.IsStackSlot()) {
2258 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002259 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002260 __ movl(temp, Address(temp, class_offset));
2261 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002262 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002263 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002264 }
Roland Levillain4d027112015-07-01 15:41:14 +01002265 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002266 // Instead of simply (possibly) unpoisoning `temp` here, we should
2267 // emit a read barrier for the previous class reference load.
2268 // However this is not required in practice, as this is an
2269 // intermediate/temporary reference and because the current
2270 // concurrent copying collector keeps the from-space memory
2271 // intact/accessible until the end of the marking phase (the
2272 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002273 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002274 // temp = temp->GetAddressOfIMT()
2275 __ movl(temp,
2276 Address(temp, mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002277 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002278 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002279 invoke->GetImtIndex(), kX86PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002280 __ movl(temp, Address(temp, method_offset));
2281 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002282 __ call(Address(temp,
Andreas Gampe542451c2016-07-26 09:02:02 -07002283 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002284
2285 DCHECK(!codegen_->IsLeafMethod());
2286 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2287}
2288
Orion Hodsonac141392017-01-13 11:53:47 +00002289void LocationsBuilderX86::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2290 HandleInvoke(invoke);
2291}
2292
2293void InstructionCodeGeneratorX86::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2294 codegen_->GenerateInvokePolymorphicCall(invoke);
2295}
2296
Roland Levillain88cb1752014-10-20 16:36:47 +01002297void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2298 LocationSummary* locations =
2299 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2300 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002301 case DataType::Type::kInt32:
2302 case DataType::Type::kInt64:
Roland Levillain88cb1752014-10-20 16:36:47 +01002303 locations->SetInAt(0, Location::RequiresRegister());
2304 locations->SetOut(Location::SameAsFirstInput());
2305 break;
2306
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002307 case DataType::Type::kFloat32:
Roland Levillain5368c212014-11-27 15:03:41 +00002308 locations->SetInAt(0, Location::RequiresFpuRegister());
2309 locations->SetOut(Location::SameAsFirstInput());
2310 locations->AddTemp(Location::RequiresRegister());
2311 locations->AddTemp(Location::RequiresFpuRegister());
2312 break;
2313
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002314 case DataType::Type::kFloat64:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002315 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002316 locations->SetOut(Location::SameAsFirstInput());
2317 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002318 break;
2319
2320 default:
2321 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2322 }
2323}
2324
2325void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2326 LocationSummary* locations = neg->GetLocations();
2327 Location out = locations->Out();
2328 Location in = locations->InAt(0);
2329 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002330 case DataType::Type::kInt32:
Roland Levillain88cb1752014-10-20 16:36:47 +01002331 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002332 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002333 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002334 break;
2335
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002336 case DataType::Type::kInt64:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002337 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002338 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002339 __ negl(out.AsRegisterPairLow<Register>());
2340 // Negation is similar to subtraction from zero. The least
2341 // significant byte triggers a borrow when it is different from
2342 // zero; to take it into account, add 1 to the most significant
2343 // byte if the carry flag (CF) is set to 1 after the first NEGL
2344 // operation.
2345 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2346 __ negl(out.AsRegisterPairHigh<Register>());
2347 break;
2348
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002349 case DataType::Type::kFloat32: {
Roland Levillain5368c212014-11-27 15:03:41 +00002350 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002351 Register constant = locations->GetTemp(0).AsRegister<Register>();
2352 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002353 // Implement float negation with an exclusive or with value
2354 // 0x80000000 (mask for bit 31, representing the sign of a
2355 // single-precision floating-point number).
2356 __ movl(constant, Immediate(INT32_C(0x80000000)));
2357 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002358 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002359 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002360 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002361
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002362 case DataType::Type::kFloat64: {
Roland Levillain5368c212014-11-27 15:03:41 +00002363 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002364 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002365 // Implement double negation with an exclusive or with value
2366 // 0x8000000000000000 (mask for bit 63, representing the sign of
2367 // a double-precision floating-point number).
2368 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002369 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002370 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002371 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002372
2373 default:
2374 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2375 }
2376}
2377
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002378void LocationsBuilderX86::VisitX86FPNeg(HX86FPNeg* neg) {
2379 LocationSummary* locations =
2380 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002381 DCHECK(DataType::IsFloatingPointType(neg->GetType()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002382 locations->SetInAt(0, Location::RequiresFpuRegister());
2383 locations->SetInAt(1, Location::RequiresRegister());
2384 locations->SetOut(Location::SameAsFirstInput());
2385 locations->AddTemp(Location::RequiresFpuRegister());
2386}
2387
2388void InstructionCodeGeneratorX86::VisitX86FPNeg(HX86FPNeg* neg) {
2389 LocationSummary* locations = neg->GetLocations();
2390 Location out = locations->Out();
2391 DCHECK(locations->InAt(0).Equals(out));
2392
2393 Register constant_area = locations->InAt(1).AsRegister<Register>();
2394 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002395 if (neg->GetType() == DataType::Type::kFloat32) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00002396 __ movss(mask, codegen_->LiteralInt32Address(INT32_C(0x80000000),
2397 neg->GetBaseMethodAddress(),
2398 constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002399 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
2400 } else {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00002401 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000),
2402 neg->GetBaseMethodAddress(),
2403 constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002404 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
2405 }
2406}
2407
Roland Levillaindff1f282014-11-05 14:15:05 +00002408void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002409 DataType::Type result_type = conversion->GetResultType();
2410 DataType::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002411 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002412
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002413 // The float-to-long and double-to-long type conversions rely on a
2414 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002415 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002416 ((input_type == DataType::Type::kFloat32 || input_type == DataType::Type::kFloat64)
2417 && result_type == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002418 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002419 : LocationSummary::kNoCall;
2420 LocationSummary* locations =
2421 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2422
David Brazdilb2bd1c52015-03-25 11:17:37 +00002423 // The Java language does not allow treating boolean as an integral type but
2424 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002425
Roland Levillaindff1f282014-11-05 14:15:05 +00002426 switch (result_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002427 case DataType::Type::kInt8:
Roland Levillain51d3fc42014-11-13 14:11:42 +00002428 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002429 case DataType::Type::kInt64: {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002430 // Type conversion from long to byte is a result of code transformations.
2431 HInstruction* input = conversion->InputAt(0);
2432 Location input_location = input->IsConstant()
2433 ? Location::ConstantLocation(input->AsConstant())
2434 : Location::RegisterPairLocation(EAX, EDX);
2435 locations->SetInAt(0, input_location);
2436 // Make the output overlap to please the register allocator. This greatly simplifies
2437 // the validation of the linear scan implementation
2438 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2439 break;
2440 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002441 case DataType::Type::kBool:
David Brazdil46e2a392015-03-16 17:31:52 +00002442 // Boolean input is a result of code transformations.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002443 case DataType::Type::kInt16:
2444 case DataType::Type::kInt32:
2445 case DataType::Type::kUint16:
Roland Levillain981e4542014-11-14 11:47:14 +00002446 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05002447 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2448 // Make the output overlap to please the register allocator. This greatly simplifies
2449 // the validation of the linear scan implementation
2450 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002451 break;
2452
2453 default:
2454 LOG(FATAL) << "Unexpected type conversion from " << input_type
2455 << " to " << result_type;
2456 }
2457 break;
2458
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002459 case DataType::Type::kInt16:
Roland Levillain01a8d712014-11-14 16:27:39 +00002460 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002461 case DataType::Type::kInt64:
Vladimir Markob52bbde2016-02-12 12:06:05 +00002462 // Type conversion from long to short is a result of code transformations.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002463 case DataType::Type::kBool:
David Brazdil46e2a392015-03-16 17:31:52 +00002464 // Boolean input is a result of code transformations.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002465 case DataType::Type::kInt8:
2466 case DataType::Type::kInt32:
2467 case DataType::Type::kUint16:
Roland Levillain01a8d712014-11-14 16:27:39 +00002468 // Processing a Dex `int-to-short' instruction.
2469 locations->SetInAt(0, Location::Any());
2470 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2471 break;
2472
2473 default:
2474 LOG(FATAL) << "Unexpected type conversion from " << input_type
2475 << " to " << result_type;
2476 }
2477 break;
2478
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002479 case DataType::Type::kInt32:
Roland Levillain946e1432014-11-11 17:35:19 +00002480 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002481 case DataType::Type::kInt64:
Roland Levillain981e4542014-11-14 11:47:14 +00002482 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002483 locations->SetInAt(0, Location::Any());
2484 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2485 break;
2486
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002487 case DataType::Type::kFloat32:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002488 // Processing a Dex `float-to-int' instruction.
2489 locations->SetInAt(0, Location::RequiresFpuRegister());
2490 locations->SetOut(Location::RequiresRegister());
2491 locations->AddTemp(Location::RequiresFpuRegister());
2492 break;
2493
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002494 case DataType::Type::kFloat64:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002495 // Processing a Dex `double-to-int' instruction.
2496 locations->SetInAt(0, Location::RequiresFpuRegister());
2497 locations->SetOut(Location::RequiresRegister());
2498 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002499 break;
2500
2501 default:
2502 LOG(FATAL) << "Unexpected type conversion from " << input_type
2503 << " to " << result_type;
2504 }
2505 break;
2506
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002507 case DataType::Type::kInt64:
Roland Levillaindff1f282014-11-05 14:15:05 +00002508 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002509 case DataType::Type::kBool:
David Brazdil46e2a392015-03-16 17:31:52 +00002510 // Boolean input is a result of code transformations.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002511 case DataType::Type::kInt8:
2512 case DataType::Type::kInt16:
2513 case DataType::Type::kInt32:
2514 case DataType::Type::kUint16:
Roland Levillain981e4542014-11-14 11:47:14 +00002515 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002516 locations->SetInAt(0, Location::RegisterLocation(EAX));
2517 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2518 break;
2519
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002520 case DataType::Type::kFloat32:
2521 case DataType::Type::kFloat64: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002522 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00002523 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002524 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2525 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2526
Vladimir Marko949c91f2015-01-27 10:48:44 +00002527 // The runtime helper puts the result in EAX, EDX.
2528 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002529 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002530 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002531
2532 default:
2533 LOG(FATAL) << "Unexpected type conversion from " << input_type
2534 << " to " << result_type;
2535 }
2536 break;
2537
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002538 case DataType::Type::kUint16:
Roland Levillain981e4542014-11-14 11:47:14 +00002539 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002540 case DataType::Type::kInt64:
Vladimir Markob52bbde2016-02-12 12:06:05 +00002541 // Type conversion from long to char is a result of code transformations.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002542 case DataType::Type::kBool:
David Brazdil46e2a392015-03-16 17:31:52 +00002543 // Boolean input is a result of code transformations.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002544 case DataType::Type::kInt8:
2545 case DataType::Type::kInt16:
2546 case DataType::Type::kInt32:
Roland Levillain981e4542014-11-14 11:47:14 +00002547 // Processing a Dex `int-to-char' instruction.
2548 locations->SetInAt(0, Location::Any());
2549 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2550 break;
2551
2552 default:
2553 LOG(FATAL) << "Unexpected type conversion from " << input_type
2554 << " to " << result_type;
2555 }
2556 break;
2557
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002558 case DataType::Type::kFloat32:
Roland Levillaincff13742014-11-17 14:32:17 +00002559 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002560 case DataType::Type::kBool:
David Brazdil46e2a392015-03-16 17:31:52 +00002561 // Boolean input is a result of code transformations.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002562 case DataType::Type::kInt8:
2563 case DataType::Type::kInt16:
2564 case DataType::Type::kInt32:
2565 case DataType::Type::kUint16:
Roland Levillaincff13742014-11-17 14:32:17 +00002566 // Processing a Dex `int-to-float' instruction.
2567 locations->SetInAt(0, Location::RequiresRegister());
2568 locations->SetOut(Location::RequiresFpuRegister());
2569 break;
2570
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002571 case DataType::Type::kInt64:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002572 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002573 locations->SetInAt(0, Location::Any());
2574 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002575 break;
2576
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002577 case DataType::Type::kFloat64:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002578 // Processing a Dex `double-to-float' instruction.
2579 locations->SetInAt(0, Location::RequiresFpuRegister());
2580 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002581 break;
2582
2583 default:
2584 LOG(FATAL) << "Unexpected type conversion from " << input_type
2585 << " to " << result_type;
2586 };
2587 break;
2588
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002589 case DataType::Type::kFloat64:
Roland Levillaincff13742014-11-17 14:32:17 +00002590 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002591 case DataType::Type::kBool:
David Brazdil46e2a392015-03-16 17:31:52 +00002592 // Boolean input is a result of code transformations.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002593 case DataType::Type::kInt8:
2594 case DataType::Type::kInt16:
2595 case DataType::Type::kInt32:
2596 case DataType::Type::kUint16:
Roland Levillaincff13742014-11-17 14:32:17 +00002597 // Processing a Dex `int-to-double' instruction.
2598 locations->SetInAt(0, Location::RequiresRegister());
2599 locations->SetOut(Location::RequiresFpuRegister());
2600 break;
2601
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002602 case DataType::Type::kInt64:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002603 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002604 locations->SetInAt(0, Location::Any());
2605 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002606 break;
2607
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002608 case DataType::Type::kFloat32:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002609 // Processing a Dex `float-to-double' instruction.
2610 locations->SetInAt(0, Location::RequiresFpuRegister());
2611 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002612 break;
2613
2614 default:
2615 LOG(FATAL) << "Unexpected type conversion from " << input_type
2616 << " to " << result_type;
2617 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002618 break;
2619
2620 default:
2621 LOG(FATAL) << "Unexpected type conversion from " << input_type
2622 << " to " << result_type;
2623 }
2624}
2625
2626void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2627 LocationSummary* locations = conversion->GetLocations();
2628 Location out = locations->Out();
2629 Location in = locations->InAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002630 DataType::Type result_type = conversion->GetResultType();
2631 DataType::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002632 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002633 switch (result_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002634 case DataType::Type::kInt8:
Roland Levillain51d3fc42014-11-13 14:11:42 +00002635 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002636 case DataType::Type::kInt64:
Vladimir Markob52bbde2016-02-12 12:06:05 +00002637 // Type conversion from long to byte is a result of code transformations.
2638 if (in.IsRegisterPair()) {
2639 __ movsxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2640 } else {
2641 DCHECK(in.GetConstant()->IsLongConstant());
2642 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2643 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2644 }
2645 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002646 case DataType::Type::kBool:
David Brazdil46e2a392015-03-16 17:31:52 +00002647 // Boolean input is a result of code transformations.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002648 case DataType::Type::kInt16:
2649 case DataType::Type::kInt32:
2650 case DataType::Type::kUint16:
Roland Levillain981e4542014-11-14 11:47:14 +00002651 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002652 if (in.IsRegister()) {
2653 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002654 } else {
2655 DCHECK(in.GetConstant()->IsIntConstant());
2656 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2657 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2658 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002659 break;
2660
2661 default:
2662 LOG(FATAL) << "Unexpected type conversion from " << input_type
2663 << " to " << result_type;
2664 }
2665 break;
2666
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002667 case DataType::Type::kInt16:
Roland Levillain01a8d712014-11-14 16:27:39 +00002668 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002669 case DataType::Type::kInt64:
Vladimir Markob52bbde2016-02-12 12:06:05 +00002670 // Type conversion from long to short is a result of code transformations.
2671 if (in.IsRegisterPair()) {
2672 __ movsxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2673 } else if (in.IsDoubleStackSlot()) {
2674 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2675 } else {
2676 DCHECK(in.GetConstant()->IsLongConstant());
2677 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2678 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
2679 }
2680 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002681 case DataType::Type::kBool:
David Brazdil46e2a392015-03-16 17:31:52 +00002682 // Boolean input is a result of code transformations.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002683 case DataType::Type::kInt8:
2684 case DataType::Type::kInt32:
2685 case DataType::Type::kUint16:
Roland Levillain01a8d712014-11-14 16:27:39 +00002686 // Processing a Dex `int-to-short' instruction.
2687 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002688 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002689 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002690 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002691 } else {
2692 DCHECK(in.GetConstant()->IsIntConstant());
2693 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002694 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002695 }
2696 break;
2697
2698 default:
2699 LOG(FATAL) << "Unexpected type conversion from " << input_type
2700 << " to " << result_type;
2701 }
2702 break;
2703
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002704 case DataType::Type::kInt32:
Roland Levillain946e1432014-11-11 17:35:19 +00002705 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002706 case DataType::Type::kInt64:
Roland Levillain981e4542014-11-14 11:47:14 +00002707 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002708 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002709 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002710 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002711 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002712 } else {
2713 DCHECK(in.IsConstant());
2714 DCHECK(in.GetConstant()->IsLongConstant());
2715 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002716 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002717 }
2718 break;
2719
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002720 case DataType::Type::kFloat32: {
Roland Levillain3f8f9362014-12-02 17:45:01 +00002721 // Processing a Dex `float-to-int' instruction.
2722 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2723 Register output = out.AsRegister<Register>();
2724 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002725 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002726
2727 __ movl(output, Immediate(kPrimIntMax));
2728 // temp = int-to-float(output)
2729 __ cvtsi2ss(temp, output);
2730 // if input >= temp goto done
2731 __ comiss(input, temp);
2732 __ j(kAboveEqual, &done);
2733 // if input == NaN goto nan
2734 __ j(kUnordered, &nan);
2735 // output = float-to-int-truncate(input)
2736 __ cvttss2si(output, input);
2737 __ jmp(&done);
2738 __ Bind(&nan);
2739 // output = 0
2740 __ xorl(output, output);
2741 __ Bind(&done);
2742 break;
2743 }
2744
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002745 case DataType::Type::kFloat64: {
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002746 // Processing a Dex `double-to-int' instruction.
2747 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2748 Register output = out.AsRegister<Register>();
2749 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002750 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002751
2752 __ movl(output, Immediate(kPrimIntMax));
2753 // temp = int-to-double(output)
2754 __ cvtsi2sd(temp, output);
2755 // if input >= temp goto done
2756 __ comisd(input, temp);
2757 __ j(kAboveEqual, &done);
2758 // if input == NaN goto nan
2759 __ j(kUnordered, &nan);
2760 // output = double-to-int-truncate(input)
2761 __ cvttsd2si(output, input);
2762 __ jmp(&done);
2763 __ Bind(&nan);
2764 // output = 0
2765 __ xorl(output, output);
2766 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002767 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002768 }
Roland Levillain946e1432014-11-11 17:35:19 +00002769
2770 default:
2771 LOG(FATAL) << "Unexpected type conversion from " << input_type
2772 << " to " << result_type;
2773 }
2774 break;
2775
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002776 case DataType::Type::kInt64:
Roland Levillaindff1f282014-11-05 14:15:05 +00002777 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002778 case DataType::Type::kBool:
David Brazdil46e2a392015-03-16 17:31:52 +00002779 // Boolean input is a result of code transformations.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002780 case DataType::Type::kInt8:
2781 case DataType::Type::kInt16:
2782 case DataType::Type::kInt32:
2783 case DataType::Type::kUint16:
Roland Levillain981e4542014-11-14 11:47:14 +00002784 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002785 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2786 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002787 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002788 __ cdq();
2789 break;
2790
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002791 case DataType::Type::kFloat32:
Roland Levillain624279f2014-12-04 11:54:28 +00002792 // Processing a Dex `float-to-long' instruction.
Serban Constantinescuba45db02016-07-12 22:53:02 +01002793 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002794 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002795 break;
2796
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002797 case DataType::Type::kFloat64:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002798 // Processing a Dex `double-to-long' instruction.
Serban Constantinescuba45db02016-07-12 22:53:02 +01002799 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002800 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002801 break;
2802
2803 default:
2804 LOG(FATAL) << "Unexpected type conversion from " << input_type
2805 << " to " << result_type;
2806 }
2807 break;
2808
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002809 case DataType::Type::kUint16:
Roland Levillain981e4542014-11-14 11:47:14 +00002810 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002811 case DataType::Type::kInt64:
Vladimir Markob52bbde2016-02-12 12:06:05 +00002812 // Type conversion from long to short is a result of code transformations.
2813 if (in.IsRegisterPair()) {
2814 __ movzxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2815 } else if (in.IsDoubleStackSlot()) {
2816 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2817 } else {
2818 DCHECK(in.GetConstant()->IsLongConstant());
2819 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2820 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
2821 }
2822 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002823 case DataType::Type::kBool:
David Brazdil46e2a392015-03-16 17:31:52 +00002824 // Boolean input is a result of code transformations.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002825 case DataType::Type::kInt8:
2826 case DataType::Type::kInt16:
2827 case DataType::Type::kInt32:
Roland Levillain981e4542014-11-14 11:47:14 +00002828 // Processing a Dex `Process a Dex `int-to-char'' instruction.
2829 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002830 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00002831 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002832 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00002833 } else {
2834 DCHECK(in.GetConstant()->IsIntConstant());
2835 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002836 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00002837 }
2838 break;
2839
2840 default:
2841 LOG(FATAL) << "Unexpected type conversion from " << input_type
2842 << " to " << result_type;
2843 }
2844 break;
2845
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002846 case DataType::Type::kFloat32:
Roland Levillaincff13742014-11-17 14:32:17 +00002847 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002848 case DataType::Type::kBool:
David Brazdil46e2a392015-03-16 17:31:52 +00002849 // Boolean input is a result of code transformations.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002850 case DataType::Type::kInt8:
2851 case DataType::Type::kInt16:
2852 case DataType::Type::kInt32:
2853 case DataType::Type::kUint16:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002854 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002855 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002856 break;
2857
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002858 case DataType::Type::kInt64: {
Roland Levillain6d0e4832014-11-27 18:31:21 +00002859 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002860 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002861
Roland Levillain232ade02015-04-20 15:14:36 +01002862 // Create stack space for the call to
2863 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2864 // TODO: enhance register allocator to ask for stack temporaries.
2865 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002866 adjustment = DataType::Size(DataType::Type::kInt64);
Roland Levillain232ade02015-04-20 15:14:36 +01002867 __ subl(ESP, Immediate(adjustment));
2868 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002869
Roland Levillain232ade02015-04-20 15:14:36 +01002870 // Load the value to the FP stack, using temporaries if needed.
2871 PushOntoFPStack(in, 0, adjustment, false, true);
2872
2873 if (out.IsStackSlot()) {
2874 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2875 } else {
2876 __ fstps(Address(ESP, 0));
2877 Location stack_temp = Location::StackSlot(0);
2878 codegen_->Move32(out, stack_temp);
2879 }
2880
2881 // Remove the temporary stack space we allocated.
2882 if (adjustment != 0) {
2883 __ addl(ESP, Immediate(adjustment));
2884 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002885 break;
2886 }
2887
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002888 case DataType::Type::kFloat64:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002889 // Processing a Dex `double-to-float' instruction.
2890 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002891 break;
2892
2893 default:
2894 LOG(FATAL) << "Unexpected type conversion from " << input_type
2895 << " to " << result_type;
2896 };
2897 break;
2898
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002899 case DataType::Type::kFloat64:
Roland Levillaincff13742014-11-17 14:32:17 +00002900 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002901 case DataType::Type::kBool:
David Brazdil46e2a392015-03-16 17:31:52 +00002902 // Boolean input is a result of code transformations.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002903 case DataType::Type::kInt8:
2904 case DataType::Type::kInt16:
2905 case DataType::Type::kInt32:
2906 case DataType::Type::kUint16:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002907 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002908 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002909 break;
2910
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002911 case DataType::Type::kInt64: {
Roland Levillain647b9ed2014-11-27 12:06:00 +00002912 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002913 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002914
Roland Levillain232ade02015-04-20 15:14:36 +01002915 // Create stack space for the call to
2916 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2917 // TODO: enhance register allocator to ask for stack temporaries.
2918 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002919 adjustment = DataType::Size(DataType::Type::kInt64);
Roland Levillain232ade02015-04-20 15:14:36 +01002920 __ subl(ESP, Immediate(adjustment));
2921 }
2922
2923 // Load the value to the FP stack, using temporaries if needed.
2924 PushOntoFPStack(in, 0, adjustment, false, true);
2925
2926 if (out.IsDoubleStackSlot()) {
2927 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2928 } else {
2929 __ fstpl(Address(ESP, 0));
2930 Location stack_temp = Location::DoubleStackSlot(0);
2931 codegen_->Move64(out, stack_temp);
2932 }
2933
2934 // Remove the temporary stack space we allocated.
2935 if (adjustment != 0) {
2936 __ addl(ESP, Immediate(adjustment));
2937 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002938 break;
2939 }
2940
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002941 case DataType::Type::kFloat32:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002942 // Processing a Dex `float-to-double' instruction.
2943 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002944 break;
2945
2946 default:
2947 LOG(FATAL) << "Unexpected type conversion from " << input_type
2948 << " to " << result_type;
2949 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002950 break;
2951
2952 default:
2953 LOG(FATAL) << "Unexpected type conversion from " << input_type
2954 << " to " << result_type;
2955 }
2956}
2957
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002958void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002959 LocationSummary* locations =
2960 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002961 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002962 case DataType::Type::kInt32: {
Mark Mendell09b84632015-02-13 17:48:38 -05002963 locations->SetInAt(0, Location::RequiresRegister());
2964 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2965 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2966 break;
2967 }
2968
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002969 case DataType::Type::kInt64: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002970 locations->SetInAt(0, Location::RequiresRegister());
2971 locations->SetInAt(1, Location::Any());
2972 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002973 break;
2974 }
2975
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002976 case DataType::Type::kFloat32:
2977 case DataType::Type::kFloat64: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002978 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002979 if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2980 DCHECK(add->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002981 } else if (add->InputAt(1)->IsConstant()) {
2982 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002983 } else {
2984 locations->SetInAt(1, Location::Any());
2985 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002986 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002987 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002988 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002989
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002990 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002991 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2992 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002993 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002994}
2995
2996void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2997 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002998 Location first = locations->InAt(0);
2999 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05003000 Location out = locations->Out();
3001
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003002 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003003 case DataType::Type::kInt32: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003004 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05003005 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3006 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04003007 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
3008 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05003009 } else {
3010 __ leal(out.AsRegister<Register>(), Address(
3011 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
3012 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003013 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05003014 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
3015 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3016 __ addl(out.AsRegister<Register>(), Immediate(value));
3017 } else {
3018 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
3019 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003020 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05003021 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003022 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003023 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003024 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003025 }
3026
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003027 case DataType::Type::kInt64: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003028 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003029 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3030 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003031 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003032 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3033 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003034 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003035 } else {
3036 DCHECK(second.IsConstant()) << second;
3037 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3038 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3039 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003040 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003041 break;
3042 }
3043
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003044 case DataType::Type::kFloat32: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003045 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003046 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04003047 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3048 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003049 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003050 __ addss(first.AsFpuRegister<XmmRegister>(),
3051 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003052 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3053 const_area->GetBaseMethodAddress(),
3054 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003055 } else {
3056 DCHECK(second.IsStackSlot());
3057 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003058 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003059 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003060 }
3061
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003062 case DataType::Type::kFloat64: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003063 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003064 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04003065 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3066 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003067 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003068 __ addsd(first.AsFpuRegister<XmmRegister>(),
3069 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003070 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3071 const_area->GetBaseMethodAddress(),
3072 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003073 } else {
3074 DCHECK(second.IsDoubleStackSlot());
3075 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003076 }
3077 break;
3078 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003079
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003080 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003081 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003082 }
3083}
3084
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003085void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003086 LocationSummary* locations =
3087 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003088 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003089 case DataType::Type::kInt32:
3090 case DataType::Type::kInt64: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003091 locations->SetInAt(0, Location::RequiresRegister());
3092 locations->SetInAt(1, Location::Any());
3093 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003094 break;
3095 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003096 case DataType::Type::kFloat32:
3097 case DataType::Type::kFloat64: {
Calin Juravle11351682014-10-23 15:38:15 +01003098 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003099 if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3100 DCHECK(sub->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003101 } else if (sub->InputAt(1)->IsConstant()) {
3102 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003103 } else {
3104 locations->SetInAt(1, Location::Any());
3105 }
Calin Juravle11351682014-10-23 15:38:15 +01003106 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003107 break;
Calin Juravle11351682014-10-23 15:38:15 +01003108 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003109
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003110 default:
Calin Juravle11351682014-10-23 15:38:15 +01003111 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003112 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003113}
3114
3115void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
3116 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003117 Location first = locations->InAt(0);
3118 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01003119 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003120 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003121 case DataType::Type::kInt32: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003122 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003123 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003124 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003125 __ subl(first.AsRegister<Register>(),
3126 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003127 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003128 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003129 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003130 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003131 }
3132
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003133 case DataType::Type::kInt64: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003134 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01003135 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3136 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003137 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01003138 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003139 __ sbbl(first.AsRegisterPairHigh<Register>(),
3140 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003141 } else {
3142 DCHECK(second.IsConstant()) << second;
3143 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3144 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3145 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003146 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003147 break;
3148 }
3149
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003150 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003151 if (second.IsFpuRegister()) {
3152 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3153 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3154 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003155 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003156 __ subss(first.AsFpuRegister<XmmRegister>(),
3157 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003158 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3159 const_area->GetBaseMethodAddress(),
3160 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003161 } else {
3162 DCHECK(second.IsStackSlot());
3163 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3164 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003165 break;
Calin Juravle11351682014-10-23 15:38:15 +01003166 }
3167
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003168 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003169 if (second.IsFpuRegister()) {
3170 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3171 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3172 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003173 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003174 __ subsd(first.AsFpuRegister<XmmRegister>(),
3175 codegen_->LiteralDoubleAddress(
3176 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003177 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003178 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3179 } else {
3180 DCHECK(second.IsDoubleStackSlot());
3181 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3182 }
Calin Juravle11351682014-10-23 15:38:15 +01003183 break;
3184 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003185
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003186 default:
Calin Juravle11351682014-10-23 15:38:15 +01003187 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003188 }
3189}
3190
Calin Juravle34bacdf2014-10-07 20:23:36 +01003191void LocationsBuilderX86::VisitMul(HMul* mul) {
3192 LocationSummary* locations =
3193 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3194 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003195 case DataType::Type::kInt32:
Calin Juravle34bacdf2014-10-07 20:23:36 +01003196 locations->SetInAt(0, Location::RequiresRegister());
3197 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003198 if (mul->InputAt(1)->IsIntConstant()) {
3199 // Can use 3 operand multiply.
3200 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3201 } else {
3202 locations->SetOut(Location::SameAsFirstInput());
3203 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003204 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003205 case DataType::Type::kInt64: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003206 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003207 locations->SetInAt(1, Location::Any());
3208 locations->SetOut(Location::SameAsFirstInput());
3209 // Needed for imul on 32bits with 64bits output.
3210 locations->AddTemp(Location::RegisterLocation(EAX));
3211 locations->AddTemp(Location::RegisterLocation(EDX));
3212 break;
3213 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003214 case DataType::Type::kFloat32:
3215 case DataType::Type::kFloat64: {
Calin Juravleb5bfa962014-10-21 18:02:24 +01003216 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003217 if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3218 DCHECK(mul->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003219 } else if (mul->InputAt(1)->IsConstant()) {
3220 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003221 } else {
3222 locations->SetInAt(1, Location::Any());
3223 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003224 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003225 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003226 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003227
3228 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003229 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003230 }
3231}
3232
3233void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
3234 LocationSummary* locations = mul->GetLocations();
3235 Location first = locations->InAt(0);
3236 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003237 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003238
3239 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003240 case DataType::Type::kInt32:
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003241 // The constant may have ended up in a register, so test explicitly to avoid
3242 // problems where the output may not be the same as the first operand.
3243 if (mul->InputAt(1)->IsIntConstant()) {
3244 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3245 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
3246 } else if (second.IsRegister()) {
3247 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003248 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003249 } else {
3250 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003251 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003252 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003253 }
3254 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003255
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003256 case DataType::Type::kInt64: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003257 Register in1_hi = first.AsRegisterPairHigh<Register>();
3258 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003259 Register eax = locations->GetTemp(0).AsRegister<Register>();
3260 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003261
3262 DCHECK_EQ(EAX, eax);
3263 DCHECK_EQ(EDX, edx);
3264
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003265 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01003266 // output: in1
3267 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3268 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3269 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003270 if (second.IsConstant()) {
3271 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003272
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003273 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3274 int32_t low_value = Low32Bits(value);
3275 int32_t high_value = High32Bits(value);
3276 Immediate low(low_value);
3277 Immediate high(high_value);
3278
3279 __ movl(eax, high);
3280 // eax <- in1.lo * in2.hi
3281 __ imull(eax, in1_lo);
3282 // in1.hi <- in1.hi * in2.lo
3283 __ imull(in1_hi, low);
3284 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3285 __ addl(in1_hi, eax);
3286 // move in2_lo to eax to prepare for double precision
3287 __ movl(eax, low);
3288 // edx:eax <- in1.lo * in2.lo
3289 __ mull(in1_lo);
3290 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3291 __ addl(in1_hi, edx);
3292 // in1.lo <- (in1.lo * in2.lo)[31:0];
3293 __ movl(in1_lo, eax);
3294 } else if (second.IsRegisterPair()) {
3295 Register in2_hi = second.AsRegisterPairHigh<Register>();
3296 Register in2_lo = second.AsRegisterPairLow<Register>();
3297
3298 __ movl(eax, in2_hi);
3299 // eax <- in1.lo * in2.hi
3300 __ imull(eax, in1_lo);
3301 // in1.hi <- in1.hi * in2.lo
3302 __ imull(in1_hi, in2_lo);
3303 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3304 __ addl(in1_hi, eax);
3305 // move in1_lo to eax to prepare for double precision
3306 __ movl(eax, in1_lo);
3307 // edx:eax <- in1.lo * in2.lo
3308 __ mull(in2_lo);
3309 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3310 __ addl(in1_hi, edx);
3311 // in1.lo <- (in1.lo * in2.lo)[31:0];
3312 __ movl(in1_lo, eax);
3313 } else {
3314 DCHECK(second.IsDoubleStackSlot()) << second;
3315 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
3316 Address in2_lo(ESP, second.GetStackIndex());
3317
3318 __ movl(eax, in2_hi);
3319 // eax <- in1.lo * in2.hi
3320 __ imull(eax, in1_lo);
3321 // in1.hi <- in1.hi * in2.lo
3322 __ imull(in1_hi, in2_lo);
3323 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3324 __ addl(in1_hi, eax);
3325 // move in1_lo to eax to prepare for double precision
3326 __ movl(eax, in1_lo);
3327 // edx:eax <- in1.lo * in2.lo
3328 __ mull(in2_lo);
3329 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3330 __ addl(in1_hi, edx);
3331 // in1.lo <- (in1.lo * in2.lo)[31:0];
3332 __ movl(in1_lo, eax);
3333 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003334
3335 break;
3336 }
3337
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003338 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003339 DCHECK(first.Equals(locations->Out()));
3340 if (second.IsFpuRegister()) {
3341 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3342 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3343 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003344 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003345 __ mulss(first.AsFpuRegister<XmmRegister>(),
3346 codegen_->LiteralFloatAddress(
3347 const_area->GetConstant()->AsFloatConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003348 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003349 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3350 } else {
3351 DCHECK(second.IsStackSlot());
3352 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3353 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003354 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003355 }
3356
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003357 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003358 DCHECK(first.Equals(locations->Out()));
3359 if (second.IsFpuRegister()) {
3360 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3361 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3362 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003363 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003364 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3365 codegen_->LiteralDoubleAddress(
3366 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003367 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003368 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3369 } else {
3370 DCHECK(second.IsDoubleStackSlot());
3371 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3372 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003373 break;
3374 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003375
3376 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003377 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003378 }
3379}
3380
Roland Levillain232ade02015-04-20 15:14:36 +01003381void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3382 uint32_t temp_offset,
3383 uint32_t stack_adjustment,
3384 bool is_fp,
3385 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003386 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003387 DCHECK(!is_wide);
3388 if (is_fp) {
3389 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3390 } else {
3391 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3392 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003393 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003394 DCHECK(is_wide);
3395 if (is_fp) {
3396 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3397 } else {
3398 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3399 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003400 } else {
3401 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003402 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003403 Location stack_temp = Location::StackSlot(temp_offset);
3404 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003405 if (is_fp) {
3406 __ flds(Address(ESP, temp_offset));
3407 } else {
3408 __ filds(Address(ESP, temp_offset));
3409 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003410 } else {
3411 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3412 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003413 if (is_fp) {
3414 __ fldl(Address(ESP, temp_offset));
3415 } else {
3416 __ fildl(Address(ESP, temp_offset));
3417 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003418 }
3419 }
3420}
3421
3422void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003423 DataType::Type type = rem->GetResultType();
3424 bool is_float = type == DataType::Type::kFloat32;
3425 size_t elem_size = DataType::Size(type);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003426 LocationSummary* locations = rem->GetLocations();
3427 Location first = locations->InAt(0);
3428 Location second = locations->InAt(1);
3429 Location out = locations->Out();
3430
3431 // Create stack space for 2 elements.
3432 // TODO: enhance register allocator to ask for stack temporaries.
3433 __ subl(ESP, Immediate(2 * elem_size));
3434
3435 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003436 const bool is_wide = !is_float;
3437 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
3438 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003439
3440 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003441 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003442 __ Bind(&retry);
3443 __ fprem();
3444
3445 // Move FP status to AX.
3446 __ fstsw();
3447
3448 // And see if the argument reduction is complete. This is signaled by the
3449 // C2 FPU flag bit set to 0.
3450 __ andl(EAX, Immediate(kC2ConditionMask));
3451 __ j(kNotEqual, &retry);
3452
3453 // We have settled on the final value. Retrieve it into an XMM register.
3454 // Store FP top of stack to real stack.
3455 if (is_float) {
3456 __ fsts(Address(ESP, 0));
3457 } else {
3458 __ fstl(Address(ESP, 0));
3459 }
3460
3461 // Pop the 2 items from the FP stack.
3462 __ fucompp();
3463
3464 // Load the value from the stack into an XMM register.
3465 DCHECK(out.IsFpuRegister()) << out;
3466 if (is_float) {
3467 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3468 } else {
3469 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3470 }
3471
3472 // And remove the temporary stack space we allocated.
3473 __ addl(ESP, Immediate(2 * elem_size));
3474}
3475
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003476
3477void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3478 DCHECK(instruction->IsDiv() || instruction->IsRem());
3479
3480 LocationSummary* locations = instruction->GetLocations();
3481 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003482 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003483
3484 Register out_register = locations->Out().AsRegister<Register>();
3485 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003486 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003487
3488 DCHECK(imm == 1 || imm == -1);
3489
3490 if (instruction->IsRem()) {
3491 __ xorl(out_register, out_register);
3492 } else {
3493 __ movl(out_register, input_register);
3494 if (imm == -1) {
3495 __ negl(out_register);
3496 }
3497 }
3498}
3499
3500
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003501void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003502 LocationSummary* locations = instruction->GetLocations();
3503
3504 Register out_register = locations->Out().AsRegister<Register>();
3505 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003506 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003507 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3508 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003509
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003510 Register num = locations->GetTemp(0).AsRegister<Register>();
3511
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003512 __ leal(num, Address(input_register, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003513 __ testl(input_register, input_register);
3514 __ cmovl(kGreaterEqual, num, input_register);
3515 int shift = CTZ(imm);
3516 __ sarl(num, Immediate(shift));
3517
3518 if (imm < 0) {
3519 __ negl(num);
3520 }
3521
3522 __ movl(out_register, num);
3523}
3524
3525void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3526 DCHECK(instruction->IsDiv() || instruction->IsRem());
3527
3528 LocationSummary* locations = instruction->GetLocations();
3529 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3530
3531 Register eax = locations->InAt(0).AsRegister<Register>();
3532 Register out = locations->Out().AsRegister<Register>();
3533 Register num;
3534 Register edx;
3535
3536 if (instruction->IsDiv()) {
3537 edx = locations->GetTemp(0).AsRegister<Register>();
3538 num = locations->GetTemp(1).AsRegister<Register>();
3539 } else {
3540 edx = locations->Out().AsRegister<Register>();
3541 num = locations->GetTemp(0).AsRegister<Register>();
3542 }
3543
3544 DCHECK_EQ(EAX, eax);
3545 DCHECK_EQ(EDX, edx);
3546 if (instruction->IsDiv()) {
3547 DCHECK_EQ(EAX, out);
3548 } else {
3549 DCHECK_EQ(EDX, out);
3550 }
3551
3552 int64_t magic;
3553 int shift;
3554 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3555
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003556 // Save the numerator.
3557 __ movl(num, eax);
3558
3559 // EAX = magic
3560 __ movl(eax, Immediate(magic));
3561
3562 // EDX:EAX = magic * numerator
3563 __ imull(num);
3564
3565 if (imm > 0 && magic < 0) {
3566 // EDX += num
3567 __ addl(edx, num);
3568 } else if (imm < 0 && magic > 0) {
3569 __ subl(edx, num);
3570 }
3571
3572 // Shift if needed.
3573 if (shift != 0) {
3574 __ sarl(edx, Immediate(shift));
3575 }
3576
3577 // EDX += 1 if EDX < 0
3578 __ movl(eax, edx);
3579 __ shrl(edx, Immediate(31));
3580 __ addl(edx, eax);
3581
3582 if (instruction->IsRem()) {
3583 __ movl(eax, num);
3584 __ imull(edx, Immediate(imm));
3585 __ subl(eax, edx);
3586 __ movl(edx, eax);
3587 } else {
3588 __ movl(eax, edx);
3589 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003590}
3591
Calin Juravlebacfec32014-11-14 15:54:36 +00003592void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3593 DCHECK(instruction->IsDiv() || instruction->IsRem());
3594
3595 LocationSummary* locations = instruction->GetLocations();
3596 Location out = locations->Out();
3597 Location first = locations->InAt(0);
3598 Location second = locations->InAt(1);
3599 bool is_div = instruction->IsDiv();
3600
3601 switch (instruction->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003602 case DataType::Type::kInt32: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003603 DCHECK_EQ(EAX, first.AsRegister<Register>());
3604 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003605
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003606 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003607 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003608
3609 if (imm == 0) {
3610 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3611 } else if (imm == 1 || imm == -1) {
3612 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003613 } else if (is_div && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003614 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003615 } else {
3616 DCHECK(imm <= -2 || imm >= 2);
3617 GenerateDivRemWithAnyConstant(instruction);
3618 }
3619 } else {
David Srbecky9cd6d372016-02-09 15:24:47 +00003620 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(
3621 instruction, out.AsRegister<Register>(), is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003622 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003623
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003624 Register second_reg = second.AsRegister<Register>();
3625 // 0x80000000/-1 triggers an arithmetic exception!
3626 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3627 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003628
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003629 __ cmpl(second_reg, Immediate(-1));
3630 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003631
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003632 // edx:eax <- sign-extended of eax
3633 __ cdq();
3634 // eax = quotient, edx = remainder
3635 __ idivl(second_reg);
3636 __ Bind(slow_path->GetExitLabel());
3637 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003638 break;
3639 }
3640
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003641 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003642 InvokeRuntimeCallingConvention calling_convention;
3643 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3644 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3645 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3646 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3647 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3648 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3649
3650 if (is_div) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003651 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003652 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003653 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003654 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003655 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003656 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003657 break;
3658 }
3659
3660 default:
3661 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3662 }
3663}
3664
Calin Juravle7c4954d2014-10-28 16:57:40 +00003665void LocationsBuilderX86::VisitDiv(HDiv* div) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003666 LocationSummary::CallKind call_kind = (div->GetResultType() == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003667 ? LocationSummary::kCallOnMainOnly
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003668 : LocationSummary::kNoCall;
3669 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3670
Calin Juravle7c4954d2014-10-28 16:57:40 +00003671 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003672 case DataType::Type::kInt32: {
Calin Juravled0d48522014-11-04 16:40:20 +00003673 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003674 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003675 locations->SetOut(Location::SameAsFirstInput());
3676 // Intel uses edx:eax as the dividend.
3677 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003678 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3679 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3680 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003681 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003682 locations->AddTemp(Location::RequiresRegister());
3683 }
Calin Juravled0d48522014-11-04 16:40:20 +00003684 break;
3685 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003686 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003687 InvokeRuntimeCallingConvention calling_convention;
3688 locations->SetInAt(0, Location::RegisterPairLocation(
3689 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3690 locations->SetInAt(1, Location::RegisterPairLocation(
3691 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3692 // Runtime helper puts the result in EAX, EDX.
3693 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003694 break;
3695 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003696 case DataType::Type::kFloat32:
3697 case DataType::Type::kFloat64: {
Calin Juravle7c4954d2014-10-28 16:57:40 +00003698 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003699 if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3700 DCHECK(div->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003701 } else if (div->InputAt(1)->IsConstant()) {
3702 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003703 } else {
3704 locations->SetInAt(1, Location::Any());
3705 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003706 locations->SetOut(Location::SameAsFirstInput());
3707 break;
3708 }
3709
3710 default:
3711 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3712 }
3713}
3714
3715void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3716 LocationSummary* locations = div->GetLocations();
3717 Location first = locations->InAt(0);
3718 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003719
3720 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003721 case DataType::Type::kInt32:
3722 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003723 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003724 break;
3725 }
3726
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003727 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003728 if (second.IsFpuRegister()) {
3729 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3730 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3731 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003732 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003733 __ divss(first.AsFpuRegister<XmmRegister>(),
3734 codegen_->LiteralFloatAddress(
3735 const_area->GetConstant()->AsFloatConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003736 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003737 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3738 } else {
3739 DCHECK(second.IsStackSlot());
3740 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3741 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003742 break;
3743 }
3744
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003745 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003746 if (second.IsFpuRegister()) {
3747 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3748 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3749 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003750 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003751 __ divsd(first.AsFpuRegister<XmmRegister>(),
3752 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003753 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3754 const_area->GetBaseMethodAddress(),
3755 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003756 } else {
3757 DCHECK(second.IsDoubleStackSlot());
3758 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3759 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003760 break;
3761 }
3762
3763 default:
3764 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3765 }
3766}
3767
Calin Juravlebacfec32014-11-14 15:54:36 +00003768void LocationsBuilderX86::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003769 DataType::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003770
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003771 LocationSummary::CallKind call_kind = (rem->GetResultType() == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003772 ? LocationSummary::kCallOnMainOnly
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003773 : LocationSummary::kNoCall;
3774 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003775
Calin Juravled2ec87d2014-12-08 14:24:46 +00003776 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003777 case DataType::Type::kInt32: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003778 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003779 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003780 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003781 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3782 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3783 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003784 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003785 locations->AddTemp(Location::RequiresRegister());
3786 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003787 break;
3788 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003789 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003790 InvokeRuntimeCallingConvention calling_convention;
3791 locations->SetInAt(0, Location::RegisterPairLocation(
3792 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3793 locations->SetInAt(1, Location::RegisterPairLocation(
3794 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3795 // Runtime helper puts the result in EAX, EDX.
3796 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3797 break;
3798 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003799 case DataType::Type::kFloat64:
3800 case DataType::Type::kFloat32: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003801 locations->SetInAt(0, Location::Any());
3802 locations->SetInAt(1, Location::Any());
3803 locations->SetOut(Location::RequiresFpuRegister());
3804 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003805 break;
3806 }
3807
3808 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003809 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003810 }
3811}
3812
3813void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003814 DataType::Type type = rem->GetResultType();
Calin Juravlebacfec32014-11-14 15:54:36 +00003815 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003816 case DataType::Type::kInt32:
3817 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003818 GenerateDivRemIntegral(rem);
3819 break;
3820 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003821 case DataType::Type::kFloat32:
3822 case DataType::Type::kFloat64: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003823 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003824 break;
3825 }
3826 default:
3827 LOG(FATAL) << "Unexpected rem type " << type;
3828 }
3829}
3830
Calin Juravled0d48522014-11-04 16:40:20 +00003831void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003832 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003833 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003834 case DataType::Type::kBool:
3835 case DataType::Type::kInt8:
3836 case DataType::Type::kUint16:
3837 case DataType::Type::kInt16:
3838 case DataType::Type::kInt32: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003839 locations->SetInAt(0, Location::Any());
3840 break;
3841 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003842 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003843 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
3844 if (!instruction->IsConstant()) {
3845 locations->AddTemp(Location::RequiresRegister());
3846 }
3847 break;
3848 }
3849 default:
3850 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3851 }
Calin Juravled0d48522014-11-04 16:40:20 +00003852}
3853
3854void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003855 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003856 codegen_->AddSlowPath(slow_path);
3857
3858 LocationSummary* locations = instruction->GetLocations();
3859 Location value = locations->InAt(0);
3860
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003861 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003862 case DataType::Type::kBool:
3863 case DataType::Type::kInt8:
3864 case DataType::Type::kUint16:
3865 case DataType::Type::kInt16:
3866 case DataType::Type::kInt32: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003867 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003868 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003869 __ j(kEqual, slow_path->GetEntryLabel());
3870 } else if (value.IsStackSlot()) {
3871 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
3872 __ j(kEqual, slow_path->GetEntryLabel());
3873 } else {
3874 DCHECK(value.IsConstant()) << value;
3875 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01003876 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003877 }
3878 }
3879 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003880 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003881 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003882 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003883 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003884 __ movl(temp, value.AsRegisterPairLow<Register>());
3885 __ orl(temp, value.AsRegisterPairHigh<Register>());
3886 __ j(kEqual, slow_path->GetEntryLabel());
3887 } else {
3888 DCHECK(value.IsConstant()) << value;
3889 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3890 __ jmp(slow_path->GetEntryLabel());
3891 }
3892 }
3893 break;
3894 }
3895 default:
3896 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003897 }
Calin Juravled0d48522014-11-04 16:40:20 +00003898}
3899
Calin Juravle9aec02f2014-11-18 23:06:35 +00003900void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3901 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3902
3903 LocationSummary* locations =
3904 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3905
3906 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003907 case DataType::Type::kInt32:
3908 case DataType::Type::kInt64: {
Mark P Mendell73945692015-04-29 14:56:17 +00003909 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003910 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003911 // The shift count needs to be in CL or a constant.
3912 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003913 locations->SetOut(Location::SameAsFirstInput());
3914 break;
3915 }
3916 default:
3917 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3918 }
3919}
3920
3921void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3922 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3923
3924 LocationSummary* locations = op->GetLocations();
3925 Location first = locations->InAt(0);
3926 Location second = locations->InAt(1);
3927 DCHECK(first.Equals(locations->Out()));
3928
3929 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003930 case DataType::Type::kInt32: {
Mark P Mendell73945692015-04-29 14:56:17 +00003931 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003932 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003933 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003934 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003935 DCHECK_EQ(ECX, second_reg);
3936 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003937 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003938 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003939 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003940 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003941 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003942 }
3943 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003944 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003945 if (shift == 0) {
3946 return;
3947 }
3948 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003949 if (op->IsShl()) {
3950 __ shll(first_reg, imm);
3951 } else if (op->IsShr()) {
3952 __ sarl(first_reg, imm);
3953 } else {
3954 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003955 }
3956 }
3957 break;
3958 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003959 case DataType::Type::kInt64: {
Mark P Mendell73945692015-04-29 14:56:17 +00003960 if (second.IsRegister()) {
3961 Register second_reg = second.AsRegister<Register>();
3962 DCHECK_EQ(ECX, second_reg);
3963 if (op->IsShl()) {
3964 GenerateShlLong(first, second_reg);
3965 } else if (op->IsShr()) {
3966 GenerateShrLong(first, second_reg);
3967 } else {
3968 GenerateUShrLong(first, second_reg);
3969 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003970 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003971 // Shift by a constant.
Roland Levillain5b5b9312016-03-22 14:57:31 +00003972 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003973 // Nothing to do if the shift is 0, as the input is already the output.
3974 if (shift != 0) {
3975 if (op->IsShl()) {
3976 GenerateShlLong(first, shift);
3977 } else if (op->IsShr()) {
3978 GenerateShrLong(first, shift);
3979 } else {
3980 GenerateUShrLong(first, shift);
3981 }
3982 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003983 }
3984 break;
3985 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003986 default:
3987 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3988 }
3989}
3990
Mark P Mendell73945692015-04-29 14:56:17 +00003991void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3992 Register low = loc.AsRegisterPairLow<Register>();
3993 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003994 if (shift == 1) {
3995 // This is just an addition.
3996 __ addl(low, low);
3997 __ adcl(high, high);
3998 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003999 // Shift by 32 is easy. High gets low, and low gets 0.
4000 codegen_->EmitParallelMoves(
4001 loc.ToLow(),
4002 loc.ToHigh(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004003 DataType::Type::kInt32,
Mark P Mendell73945692015-04-29 14:56:17 +00004004 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
4005 loc.ToLow(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004006 DataType::Type::kInt32);
Mark P Mendell73945692015-04-29 14:56:17 +00004007 } else if (shift > 32) {
4008 // Low part becomes 0. High part is low part << (shift-32).
4009 __ movl(high, low);
4010 __ shll(high, Immediate(shift - 32));
4011 __ xorl(low, low);
4012 } else {
4013 // Between 1 and 31.
4014 __ shld(high, low, Immediate(shift));
4015 __ shll(low, Immediate(shift));
4016 }
4017}
4018
Calin Juravle9aec02f2014-11-18 23:06:35 +00004019void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004020 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004021 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
4022 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
4023 __ testl(shifter, Immediate(32));
4024 __ j(kEqual, &done);
4025 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
4026 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
4027 __ Bind(&done);
4028}
4029
Mark P Mendell73945692015-04-29 14:56:17 +00004030void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
4031 Register low = loc.AsRegisterPairLow<Register>();
4032 Register high = loc.AsRegisterPairHigh<Register>();
4033 if (shift == 32) {
4034 // Need to copy the sign.
4035 DCHECK_NE(low, high);
4036 __ movl(low, high);
4037 __ sarl(high, Immediate(31));
4038 } else if (shift > 32) {
4039 DCHECK_NE(low, high);
4040 // High part becomes sign. Low part is shifted by shift - 32.
4041 __ movl(low, high);
4042 __ sarl(high, Immediate(31));
4043 __ sarl(low, Immediate(shift - 32));
4044 } else {
4045 // Between 1 and 31.
4046 __ shrd(low, high, Immediate(shift));
4047 __ sarl(high, Immediate(shift));
4048 }
4049}
4050
Calin Juravle9aec02f2014-11-18 23:06:35 +00004051void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004052 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004053 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4054 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
4055 __ testl(shifter, Immediate(32));
4056 __ j(kEqual, &done);
4057 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4058 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
4059 __ Bind(&done);
4060}
4061
Mark P Mendell73945692015-04-29 14:56:17 +00004062void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
4063 Register low = loc.AsRegisterPairLow<Register>();
4064 Register high = loc.AsRegisterPairHigh<Register>();
4065 if (shift == 32) {
4066 // Shift by 32 is easy. Low gets high, and high gets 0.
4067 codegen_->EmitParallelMoves(
4068 loc.ToHigh(),
4069 loc.ToLow(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004070 DataType::Type::kInt32,
Mark P Mendell73945692015-04-29 14:56:17 +00004071 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
4072 loc.ToHigh(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004073 DataType::Type::kInt32);
Mark P Mendell73945692015-04-29 14:56:17 +00004074 } else if (shift > 32) {
4075 // Low part is high >> (shift - 32). High part becomes 0.
4076 __ movl(low, high);
4077 __ shrl(low, Immediate(shift - 32));
4078 __ xorl(high, high);
4079 } else {
4080 // Between 1 and 31.
4081 __ shrd(low, high, Immediate(shift));
4082 __ shrl(high, Immediate(shift));
4083 }
4084}
4085
Calin Juravle9aec02f2014-11-18 23:06:35 +00004086void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004087 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004088 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4089 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
4090 __ testl(shifter, Immediate(32));
4091 __ j(kEqual, &done);
4092 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4093 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
4094 __ Bind(&done);
4095}
4096
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004097void LocationsBuilderX86::VisitRor(HRor* ror) {
4098 LocationSummary* locations =
4099 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
4100
4101 switch (ror->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004102 case DataType::Type::kInt64:
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004103 // Add the temporary needed.
4104 locations->AddTemp(Location::RequiresRegister());
4105 FALLTHROUGH_INTENDED;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004106 case DataType::Type::kInt32:
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004107 locations->SetInAt(0, Location::RequiresRegister());
4108 // The shift count needs to be in CL (unless it is a constant).
4109 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
4110 locations->SetOut(Location::SameAsFirstInput());
4111 break;
4112 default:
4113 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4114 UNREACHABLE();
4115 }
4116}
4117
4118void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
4119 LocationSummary* locations = ror->GetLocations();
4120 Location first = locations->InAt(0);
4121 Location second = locations->InAt(1);
4122
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004123 if (ror->GetResultType() == DataType::Type::kInt32) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004124 Register first_reg = first.AsRegister<Register>();
4125 if (second.IsRegister()) {
4126 Register second_reg = second.AsRegister<Register>();
4127 __ rorl(first_reg, second_reg);
4128 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004129 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004130 __ rorl(first_reg, imm);
4131 }
4132 return;
4133 }
4134
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004135 DCHECK_EQ(ror->GetResultType(), DataType::Type::kInt64);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004136 Register first_reg_lo = first.AsRegisterPairLow<Register>();
4137 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
4138 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
4139 if (second.IsRegister()) {
4140 Register second_reg = second.AsRegister<Register>();
4141 DCHECK_EQ(second_reg, ECX);
4142 __ movl(temp_reg, first_reg_hi);
4143 __ shrd(first_reg_hi, first_reg_lo, second_reg);
4144 __ shrd(first_reg_lo, temp_reg, second_reg);
4145 __ movl(temp_reg, first_reg_hi);
4146 __ testl(second_reg, Immediate(32));
4147 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
4148 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
4149 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004150 int32_t shift_amt = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004151 if (shift_amt == 0) {
4152 // Already fine.
4153 return;
4154 }
4155 if (shift_amt == 32) {
4156 // Just swap.
4157 __ movl(temp_reg, first_reg_lo);
4158 __ movl(first_reg_lo, first_reg_hi);
4159 __ movl(first_reg_hi, temp_reg);
4160 return;
4161 }
4162
4163 Immediate imm(shift_amt);
4164 // Save the constents of the low value.
4165 __ movl(temp_reg, first_reg_lo);
4166
4167 // Shift right into low, feeding bits from high.
4168 __ shrd(first_reg_lo, first_reg_hi, imm);
4169
4170 // Shift right into high, feeding bits from the original low.
4171 __ shrd(first_reg_hi, temp_reg, imm);
4172
4173 // Swap if needed.
4174 if (shift_amt > 32) {
4175 __ movl(temp_reg, first_reg_lo);
4176 __ movl(first_reg_lo, first_reg_hi);
4177 __ movl(first_reg_hi, temp_reg);
4178 }
4179 }
4180}
4181
Calin Juravle9aec02f2014-11-18 23:06:35 +00004182void LocationsBuilderX86::VisitShl(HShl* shl) {
4183 HandleShift(shl);
4184}
4185
4186void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
4187 HandleShift(shl);
4188}
4189
4190void LocationsBuilderX86::VisitShr(HShr* shr) {
4191 HandleShift(shr);
4192}
4193
4194void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
4195 HandleShift(shr);
4196}
4197
4198void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
4199 HandleShift(ushr);
4200}
4201
4202void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
4203 HandleShift(ushr);
4204}
4205
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004206void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004207 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004208 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004209 locations->SetOut(Location::RegisterLocation(EAX));
David Brazdil6de19382016-01-08 17:37:10 +00004210 if (instruction->IsStringAlloc()) {
4211 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4212 } else {
4213 InvokeRuntimeCallingConvention calling_convention;
4214 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00004215 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004216}
4217
4218void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004219 // Note: if heap poisoning is enabled, the entry point takes cares
4220 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004221 if (instruction->IsStringAlloc()) {
4222 // String is allocated through StringFactory. Call NewEmptyString entry point.
4223 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07004224 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004225 __ fs()->movl(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString)));
4226 __ call(Address(temp, code_offset.Int32Value()));
4227 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4228 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01004229 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00004230 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00004231 DCHECK(!codegen_->IsLeafMethod());
4232 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004233}
4234
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004235void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
4236 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004237 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004238 locations->SetOut(Location::RegisterLocation(EAX));
4239 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004240 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4241 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004242}
4243
4244void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004245 // Note: if heap poisoning is enabled, the entry point takes cares
4246 // of poisoning the reference.
Nicolas Geoffrayd0958442017-01-30 14:57:16 +00004247 QuickEntrypointEnum entrypoint =
4248 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
4249 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004250 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004251 DCHECK(!codegen_->IsLeafMethod());
4252}
4253
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004254void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004255 LocationSummary* locations =
4256 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004257 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4258 if (location.IsStackSlot()) {
4259 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4260 } else if (location.IsDoubleStackSlot()) {
4261 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004262 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004263 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004264}
4265
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004266void InstructionCodeGeneratorX86::VisitParameterValue(
4267 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4268}
4269
4270void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
4271 LocationSummary* locations =
4272 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4273 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4274}
4275
4276void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004277}
4278
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004279void LocationsBuilderX86::VisitClassTableGet(HClassTableGet* instruction) {
4280 LocationSummary* locations =
4281 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4282 locations->SetInAt(0, Location::RequiresRegister());
4283 locations->SetOut(Location::RequiresRegister());
4284}
4285
4286void InstructionCodeGeneratorX86::VisitClassTableGet(HClassTableGet* instruction) {
4287 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004288 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004289 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004290 instruction->GetIndex(), kX86PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004291 __ movl(locations->Out().AsRegister<Register>(),
4292 Address(locations->InAt(0).AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004293 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004294 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004295 instruction->GetIndex(), kX86PointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004296 __ movl(locations->Out().AsRegister<Register>(),
4297 Address(locations->InAt(0).AsRegister<Register>(),
4298 mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
4299 // temp = temp->GetImtEntryAt(method_offset);
4300 __ movl(locations->Out().AsRegister<Register>(),
4301 Address(locations->Out().AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004302 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004303}
4304
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004305void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004306 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004307 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004308 locations->SetInAt(0, Location::RequiresRegister());
4309 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004310}
4311
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004312void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
4313 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01004314 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004315 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01004316 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004317 switch (not_->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004318 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004319 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004320 break;
4321
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004322 case DataType::Type::kInt64:
Roland Levillain70566432014-10-24 16:20:17 +01004323 __ notl(out.AsRegisterPairLow<Register>());
4324 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004325 break;
4326
4327 default:
4328 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4329 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004330}
4331
David Brazdil66d126e2015-04-03 16:02:44 +01004332void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
4333 LocationSummary* locations =
4334 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4335 locations->SetInAt(0, Location::RequiresRegister());
4336 locations->SetOut(Location::SameAsFirstInput());
4337}
4338
4339void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004340 LocationSummary* locations = bool_not->GetLocations();
4341 Location in = locations->InAt(0);
4342 Location out = locations->Out();
4343 DCHECK(in.Equals(out));
4344 __ xorl(out.AsRegister<Register>(), Immediate(1));
4345}
4346
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004347void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004348 LocationSummary* locations =
4349 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004350 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004351 case DataType::Type::kBool:
4352 case DataType::Type::kInt8:
4353 case DataType::Type::kInt16:
4354 case DataType::Type::kUint16:
4355 case DataType::Type::kInt32:
4356 case DataType::Type::kInt64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00004357 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00004358 locations->SetInAt(1, Location::Any());
4359 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4360 break;
4361 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004362 case DataType::Type::kFloat32:
4363 case DataType::Type::kFloat64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00004364 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004365 if (compare->InputAt(1)->IsX86LoadFromConstantTable()) {
4366 DCHECK(compare->InputAt(1)->IsEmittedAtUseSite());
4367 } else if (compare->InputAt(1)->IsConstant()) {
4368 locations->SetInAt(1, Location::RequiresFpuRegister());
4369 } else {
4370 locations->SetInAt(1, Location::Any());
4371 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004372 locations->SetOut(Location::RequiresRegister());
4373 break;
4374 }
4375 default:
4376 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4377 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004378}
4379
4380void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004381 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004382 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004383 Location left = locations->InAt(0);
4384 Location right = locations->InAt(1);
4385
Mark Mendell0c9497d2015-08-21 09:30:05 -04004386 NearLabel less, greater, done;
Aart Bika19616e2016-02-01 18:57:58 -08004387 Condition less_cond = kLess;
4388
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004389 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004390 case DataType::Type::kBool:
4391 case DataType::Type::kInt8:
4392 case DataType::Type::kInt16:
4393 case DataType::Type::kUint16:
4394 case DataType::Type::kInt32: {
Roland Levillain0b671c02016-08-19 12:02:34 +01004395 codegen_->GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08004396 break;
4397 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004398 case DataType::Type::kInt64: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004399 Register left_low = left.AsRegisterPairLow<Register>();
4400 Register left_high = left.AsRegisterPairHigh<Register>();
4401 int32_t val_low = 0;
4402 int32_t val_high = 0;
4403 bool right_is_const = false;
4404
4405 if (right.IsConstant()) {
4406 DCHECK(right.GetConstant()->IsLongConstant());
4407 right_is_const = true;
4408 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
4409 val_low = Low32Bits(val);
4410 val_high = High32Bits(val);
4411 }
4412
Calin Juravleddb7df22014-11-25 20:56:51 +00004413 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004414 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004415 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004416 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004417 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004418 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004419 codegen_->Compare32BitValue(left_high, val_high);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004420 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004421 __ j(kLess, &less); // Signed compare.
4422 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004423 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004424 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004425 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004426 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004427 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004428 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004429 codegen_->Compare32BitValue(left_low, val_low);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004430 }
Aart Bika19616e2016-02-01 18:57:58 -08004431 less_cond = kBelow; // for CF (unsigned).
Calin Juravleddb7df22014-11-25 20:56:51 +00004432 break;
4433 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004434 case DataType::Type::kFloat32: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004435 GenerateFPCompare(left, right, compare, false);
Calin Juravleddb7df22014-11-25 20:56:51 +00004436 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004437 less_cond = kBelow; // for CF (floats).
Calin Juravleddb7df22014-11-25 20:56:51 +00004438 break;
4439 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004440 case DataType::Type::kFloat64: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004441 GenerateFPCompare(left, right, compare, true);
Calin Juravleddb7df22014-11-25 20:56:51 +00004442 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004443 less_cond = kBelow; // for CF (floats).
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004444 break;
4445 }
4446 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004447 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004448 }
Aart Bika19616e2016-02-01 18:57:58 -08004449
Calin Juravleddb7df22014-11-25 20:56:51 +00004450 __ movl(out, Immediate(0));
4451 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08004452 __ j(less_cond, &less);
Calin Juravleddb7df22014-11-25 20:56:51 +00004453
4454 __ Bind(&greater);
4455 __ movl(out, Immediate(1));
4456 __ jmp(&done);
4457
4458 __ Bind(&less);
4459 __ movl(out, Immediate(-1));
4460
4461 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004462}
4463
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004464void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004465 LocationSummary* locations =
4466 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004467 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004468 locations->SetInAt(i, Location::Any());
4469 }
4470 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004471}
4472
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004473void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004474 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004475}
4476
Roland Levillain7c1559a2015-12-15 10:55:36 +00004477void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004478 /*
4479 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
4480 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
4481 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4482 */
4483 switch (kind) {
4484 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004485 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004486 break;
4487 }
4488 case MemBarrierKind::kAnyStore:
4489 case MemBarrierKind::kLoadAny:
4490 case MemBarrierKind::kStoreStore: {
4491 // nop
4492 break;
4493 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004494 case MemBarrierKind::kNTStoreStore:
4495 // Non-Temporal Store/Store needs an explicit fence.
4496 MemoryFence(/* non-temporal */ true);
4497 break;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004498 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004499}
4500
Vladimir Markodc151b22015-10-15 18:02:30 +01004501HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
4502 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004503 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004504 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004505}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004506
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004507Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4508 Register temp) {
4509 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Markoc53c0792015-11-19 15:48:33 +00004510 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004511 if (!invoke->GetLocations()->Intrinsified()) {
4512 return location.AsRegister<Register>();
4513 }
4514 // For intrinsics we allow any location, so it may be on the stack.
4515 if (!location.IsRegister()) {
4516 __ movl(temp, Address(ESP, location.GetStackIndex()));
4517 return temp;
4518 }
4519 // For register locations, check if the register was saved. If so, get it from the stack.
4520 // Note: There is a chance that the register was saved but not overwritten, so we could
4521 // save one load. However, since this is just an intrinsic slow path we prefer this
4522 // simple and more robust approach rather that trying to determine if that's the case.
4523 SlowPathCode* slow_path = GetCurrentSlowPath();
Vladimir Marko4ee8e292017-06-02 15:39:30 +00004524 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
4525 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4526 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4527 __ movl(temp, Address(ESP, stack_offset));
4528 return temp;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004529 }
4530 return location.AsRegister<Register>();
4531}
4532
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004533void CodeGeneratorX86::GenerateStaticOrDirectCall(
4534 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Vladimir Marko58155012015-08-19 12:49:41 +00004535 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4536 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004537 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00004538 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004539 uint32_t offset =
4540 GetThreadOffset<kX86PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
4541 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004542 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004543 }
Vladimir Marko58155012015-08-19 12:49:41 +00004544 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004545 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004546 break;
Vladimir Marko65979462017-05-19 17:25:12 +01004547 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
4548 DCHECK(GetCompilerOptions().IsBootImage());
4549 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4550 temp.AsRegister<Register>());
4551 __ leal(temp.AsRegister<Register>(), Address(base_reg, CodeGeneratorX86::kDummy32BitOffset));
4552 RecordBootMethodPatch(invoke);
4553 break;
4554 }
Vladimir Marko58155012015-08-19 12:49:41 +00004555 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4556 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
4557 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004558 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004559 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4560 temp.AsRegister<Register>());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004561 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004562 // Bind a new fixup label at the end of the "movl" insn.
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004563 __ Bind(NewMethodBssEntryPatch(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004564 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress(),
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004565 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex())));
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004566 break;
4567 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004568 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
4569 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
4570 return; // No code pointer retrieval; the runtime performs the call directly.
Vladimir Marko9b688a02015-05-06 14:12:42 +01004571 }
Vladimir Marko58155012015-08-19 12:49:41 +00004572 }
4573
4574 switch (invoke->GetCodePtrLocation()) {
4575 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4576 __ call(GetFrameEntryLabel());
4577 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004578 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4579 // (callee_method + offset_of_quick_compiled_code)()
4580 __ call(Address(callee_method.AsRegister<Register>(),
4581 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07004582 kX86PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004583 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04004584 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004585 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Mark Mendell09ed1a32015-03-25 08:30:06 -04004586
4587 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04004588}
4589
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004590void CodeGeneratorX86::GenerateVirtualCall(
4591 HInvokeVirtual* invoke, Location temp_in, SlowPathCode* slow_path) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004592 Register temp = temp_in.AsRegister<Register>();
4593 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4594 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004595
4596 // Use the calling convention instead of the location of the receiver, as
4597 // intrinsics may have put the receiver in a different register. In the intrinsics
4598 // slow path, the arguments have been moved to the right place, so here we are
4599 // guaranteed that the receiver is the first register of the calling convention.
4600 InvokeDexCallingConvention calling_convention;
4601 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004602 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004603 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004604 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004605 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004606 // Instead of simply (possibly) unpoisoning `temp` here, we should
4607 // emit a read barrier for the previous class reference load.
4608 // However this is not required in practice, as this is an
4609 // intermediate/temporary reference and because the current
4610 // concurrent copying collector keeps the from-space memory
4611 // intact/accessible until the end of the marking phase (the
4612 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004613 __ MaybeUnpoisonHeapReference(temp);
4614 // temp = temp->GetMethodAt(method_offset);
4615 __ movl(temp, Address(temp, method_offset));
4616 // call temp->GetEntryPoint();
4617 __ call(Address(
Andreas Gampe542451c2016-07-26 09:02:02 -07004618 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004619 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004620}
4621
Vladimir Marko65979462017-05-19 17:25:12 +01004622void CodeGeneratorX86::RecordBootMethodPatch(HInvokeStaticOrDirect* invoke) {
4623 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
4624 HX86ComputeBaseMethodAddress* address =
4625 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
4626 boot_image_method_patches_.emplace_back(address,
4627 *invoke->GetTargetMethod().dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07004628 invoke->GetTargetMethod().index);
Vladimir Marko65979462017-05-19 17:25:12 +01004629 __ Bind(&boot_image_method_patches_.back().label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004630}
4631
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004632Label* CodeGeneratorX86::NewMethodBssEntryPatch(
4633 HX86ComputeBaseMethodAddress* method_address,
4634 MethodReference target_method) {
4635 // Add the patch entry and bind its label at the end of the instruction.
4636 method_bss_entry_patches_.emplace_back(method_address,
4637 *target_method.dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07004638 target_method.index);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004639 return &method_bss_entry_patches_.back().label;
4640}
4641
Vladimir Marko1998cd02017-01-13 13:02:58 +00004642void CodeGeneratorX86::RecordBootTypePatch(HLoadClass* load_class) {
Vladimir Marko00916b92017-05-17 17:45:45 +01004643 HX86ComputeBaseMethodAddress* address = load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004644 boot_image_type_patches_.emplace_back(address,
4645 load_class->GetDexFile(),
Vladimir Marko1998cd02017-01-13 13:02:58 +00004646 load_class->GetTypeIndex().index_);
4647 __ Bind(&boot_image_type_patches_.back().label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004648}
4649
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004650Label* CodeGeneratorX86::NewTypeBssEntryPatch(HLoadClass* load_class) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004651 HX86ComputeBaseMethodAddress* address =
4652 load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
4653 type_bss_entry_patches_.emplace_back(
4654 address, load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00004655 return &type_bss_entry_patches_.back().label;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004656}
4657
Vladimir Marko65979462017-05-19 17:25:12 +01004658void CodeGeneratorX86::RecordBootStringPatch(HLoadString* load_string) {
Vladimir Marko65979462017-05-19 17:25:12 +01004659 HX86ComputeBaseMethodAddress* address = load_string->InputAt(0)->AsX86ComputeBaseMethodAddress();
4660 string_patches_.emplace_back(address,
4661 load_string->GetDexFile(),
4662 load_string->GetStringIndex().index_);
4663 __ Bind(&string_patches_.back().label);
4664}
4665
Vladimir Markoaad75c62016-10-03 08:46:48 +00004666Label* CodeGeneratorX86::NewStringBssEntryPatch(HLoadString* load_string) {
4667 DCHECK(!GetCompilerOptions().IsBootImage());
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004668 HX86ComputeBaseMethodAddress* address =
4669 load_string->InputAt(0)->AsX86ComputeBaseMethodAddress();
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004670 string_bss_entry_patches_.emplace_back(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004671 address, load_string->GetDexFile(), load_string->GetStringIndex().index_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004672 return &string_bss_entry_patches_.back().label;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004673}
4674
Vladimir Markoaad75c62016-10-03 08:46:48 +00004675// The label points to the end of the "movl" or another instruction but the literal offset
4676// for method patch needs to point to the embedded constant which occupies the last 4 bytes.
4677constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
4678
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004679template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +00004680inline void CodeGeneratorX86::EmitPcRelativeLinkerPatches(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004681 const ArenaDeque<X86PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004682 ArenaVector<linker::LinkerPatch>* linker_patches) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004683 for (const X86PcRelativePatchInfo& info : infos) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00004684 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004685 linker_patches->push_back(Factory(
4686 literal_offset, &info.dex_file, GetMethodAddressOffset(info.method_address), info.index));
Vladimir Markoaad75c62016-10-03 08:46:48 +00004687 }
4688}
4689
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004690void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Marko58155012015-08-19 12:49:41 +00004691 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004692 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01004693 boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004694 method_bss_entry_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +00004695 boot_image_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01004696 type_bss_entry_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004697 string_patches_.size() +
4698 string_bss_entry_patches_.size();
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004699 linker_patches->reserve(size);
Vladimir Marko764d4542017-05-16 10:31:41 +01004700 if (GetCompilerOptions().IsBootImage()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004701 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
4702 boot_image_method_patches_, linker_patches);
4703 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
4704 boot_image_type_patches_, linker_patches);
4705 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
4706 string_patches_, linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004707 } else {
Vladimir Marko65979462017-05-19 17:25:12 +01004708 DCHECK(boot_image_method_patches_.empty());
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004709 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeClassTablePatch>(
4710 boot_image_type_patches_, linker_patches);
4711 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringInternTablePatch>(
4712 string_patches_, linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004713 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004714 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
4715 method_bss_entry_patches_, linker_patches);
4716 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
4717 type_bss_entry_patches_, linker_patches);
4718 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
4719 string_bss_entry_patches_, linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00004720 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00004721}
4722
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004723void CodeGeneratorX86::MarkGCCard(Register temp,
4724 Register card,
4725 Register object,
4726 Register value,
4727 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004728 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004729 if (value_can_be_null) {
4730 __ testl(value, value);
4731 __ j(kEqual, &is_null);
4732 }
Andreas Gampe542451c2016-07-26 09:02:02 -07004733 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86PointerSize>().Int32Value()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004734 __ movl(temp, object);
4735 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004736 __ movb(Address(temp, card, TIMES_1, 0),
4737 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004738 if (value_can_be_null) {
4739 __ Bind(&is_null);
4740 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004741}
4742
Calin Juravle52c48962014-12-16 17:02:57 +00004743void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4744 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00004745
4746 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004747 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004748 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004749 new (GetGraph()->GetArena()) LocationSummary(instruction,
4750 kEmitCompilerReadBarrier ?
4751 LocationSummary::kCallOnSlowPath :
4752 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004753 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004754 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004755 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004756 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004757
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004758 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004759 locations->SetOut(Location::RequiresFpuRegister());
4760 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004761 // The output overlaps in case of long: we don't want the low move
4762 // to overwrite the object's location. Likewise, in the case of
4763 // an object field get with read barriers enabled, we do not want
4764 // the move to overwrite the object's location, as we need it to emit
4765 // the read barrier.
4766 locations->SetOut(
4767 Location::RequiresRegister(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004768 (object_field_get_with_read_barrier || instruction->GetType() == DataType::Type::kInt64) ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00004769 Location::kOutputOverlap :
4770 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004771 }
Calin Juravle52c48962014-12-16 17:02:57 +00004772
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004773 if (field_info.IsVolatile() && (field_info.GetFieldType() == DataType::Type::kInt64)) {
Calin Juravle52c48962014-12-16 17:02:57 +00004774 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00004775 // So we use an XMM register as a temp to achieve atomicity (first
4776 // load the temp into the XMM and then copy the XMM into the
4777 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00004778 locations->AddTemp(Location::RequiresFpuRegister());
4779 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004780}
4781
Calin Juravle52c48962014-12-16 17:02:57 +00004782void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
4783 const FieldInfo& field_info) {
4784 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004785
Calin Juravle52c48962014-12-16 17:02:57 +00004786 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004787 Location base_loc = locations->InAt(0);
4788 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004789 Location out = locations->Out();
4790 bool is_volatile = field_info.IsVolatile();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004791 DataType::Type field_type = field_info.GetFieldType();
Calin Juravle52c48962014-12-16 17:02:57 +00004792 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4793
4794 switch (field_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004795 case DataType::Type::kBool: {
Calin Juravle52c48962014-12-16 17:02:57 +00004796 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004797 break;
4798 }
4799
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004800 case DataType::Type::kInt8: {
Calin Juravle52c48962014-12-16 17:02:57 +00004801 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004802 break;
4803 }
4804
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004805 case DataType::Type::kInt16: {
Calin Juravle52c48962014-12-16 17:02:57 +00004806 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004807 break;
4808 }
4809
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004810 case DataType::Type::kUint16: {
Calin Juravle52c48962014-12-16 17:02:57 +00004811 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004812 break;
4813 }
4814
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004815 case DataType::Type::kInt32:
Calin Juravle52c48962014-12-16 17:02:57 +00004816 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004817 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00004818
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004819 case DataType::Type::kReference: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004820 // /* HeapReference<Object> */ out = *(base + offset)
4821 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004822 // Note that a potential implicit null check is handled in this
4823 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4824 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00004825 instruction, out, base, offset, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00004826 if (is_volatile) {
4827 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4828 }
4829 } else {
4830 __ movl(out.AsRegister<Register>(), Address(base, offset));
4831 codegen_->MaybeRecordImplicitNullCheck(instruction);
4832 if (is_volatile) {
4833 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4834 }
4835 // If read barriers are enabled, emit read barriers other than
4836 // Baker's using a slow path (and also unpoison the loaded
4837 // reference, if heap poisoning is enabled).
4838 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4839 }
4840 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004841 }
4842
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004843 case DataType::Type::kInt64: {
Calin Juravle52c48962014-12-16 17:02:57 +00004844 if (is_volatile) {
4845 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4846 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004847 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004848 __ movd(out.AsRegisterPairLow<Register>(), temp);
4849 __ psrlq(temp, Immediate(32));
4850 __ movd(out.AsRegisterPairHigh<Register>(), temp);
4851 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004852 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00004853 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004854 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004855 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
4856 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004857 break;
4858 }
4859
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004860 case DataType::Type::kFloat32: {
Calin Juravle52c48962014-12-16 17:02:57 +00004861 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004862 break;
4863 }
4864
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004865 case DataType::Type::kFloat64: {
Calin Juravle52c48962014-12-16 17:02:57 +00004866 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004867 break;
4868 }
4869
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004870 case DataType::Type::kVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004871 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004872 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004873 }
Calin Juravle52c48962014-12-16 17:02:57 +00004874
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004875 if (field_type == DataType::Type::kReference || field_type == DataType::Type::kInt64) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004876 // Potential implicit null checks, in the case of reference or
4877 // long fields, are handled in the previous switch statement.
4878 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004879 codegen_->MaybeRecordImplicitNullCheck(instruction);
4880 }
4881
Calin Juravle52c48962014-12-16 17:02:57 +00004882 if (is_volatile) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004883 if (field_type == DataType::Type::kReference) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004884 // Memory barriers, in the case of references, are also handled
4885 // in the previous switch statement.
4886 } else {
4887 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4888 }
Roland Levillain4d027112015-07-01 15:41:14 +01004889 }
Calin Juravle52c48962014-12-16 17:02:57 +00004890}
4891
4892void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4893 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4894
4895 LocationSummary* locations =
4896 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4897 locations->SetInAt(0, Location::RequiresRegister());
4898 bool is_volatile = field_info.IsVolatile();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004899 DataType::Type field_type = field_info.GetFieldType();
4900 bool is_byte_type = (field_type == DataType::Type::kBool)
4901 || (field_type == DataType::Type::kInt8);
Calin Juravle52c48962014-12-16 17:02:57 +00004902
4903 // The register allocator does not support multiple
4904 // inputs that die at entry with one in a specific register.
4905 if (is_byte_type) {
4906 // Ensure the value is in a byte register.
4907 locations->SetInAt(1, Location::RegisterLocation(EAX));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004908 } else if (DataType::IsFloatingPointType(field_type)) {
4909 if (is_volatile && field_type == DataType::Type::kFloat64) {
Mark Mendell81489372015-11-04 11:30:41 -05004910 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4911 locations->SetInAt(1, Location::RequiresFpuRegister());
4912 } else {
4913 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4914 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004915 } else if (is_volatile && field_type == DataType::Type::kInt64) {
Mark Mendell81489372015-11-04 11:30:41 -05004916 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00004917 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004918
Calin Juravle52c48962014-12-16 17:02:57 +00004919 // 64bits value can be atomically written to an address with movsd and an XMM register.
4920 // We need two XMM registers because there's no easier way to (bit) copy a register pair
4921 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
4922 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
4923 // isolated cases when we need this it isn't worth adding the extra complexity.
4924 locations->AddTemp(Location::RequiresFpuRegister());
4925 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004926 } else {
4927 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4928
4929 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4930 // Temporary registers for the write barrier.
4931 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
4932 // Ensure the card is in a byte register.
4933 locations->AddTemp(Location::RegisterLocation(ECX));
4934 }
Calin Juravle52c48962014-12-16 17:02:57 +00004935 }
4936}
4937
4938void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004939 const FieldInfo& field_info,
4940 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004941 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4942
4943 LocationSummary* locations = instruction->GetLocations();
4944 Register base = locations->InAt(0).AsRegister<Register>();
4945 Location value = locations->InAt(1);
4946 bool is_volatile = field_info.IsVolatile();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004947 DataType::Type field_type = field_info.GetFieldType();
Calin Juravle52c48962014-12-16 17:02:57 +00004948 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004949 bool needs_write_barrier =
4950 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004951
4952 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004953 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004954 }
4955
Mark Mendell81489372015-11-04 11:30:41 -05004956 bool maybe_record_implicit_null_check_done = false;
4957
Calin Juravle52c48962014-12-16 17:02:57 +00004958 switch (field_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004959 case DataType::Type::kBool:
4960 case DataType::Type::kInt8: {
Calin Juravle52c48962014-12-16 17:02:57 +00004961 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
4962 break;
4963 }
4964
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004965 case DataType::Type::kInt16:
4966 case DataType::Type::kUint16: {
Mark Mendell81489372015-11-04 11:30:41 -05004967 if (value.IsConstant()) {
Nicolas Geoffray78612082017-07-24 14:18:53 +01004968 __ movw(Address(base, offset),
4969 Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Mark Mendell81489372015-11-04 11:30:41 -05004970 } else {
4971 __ movw(Address(base, offset), value.AsRegister<Register>());
4972 }
Calin Juravle52c48962014-12-16 17:02:57 +00004973 break;
4974 }
4975
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004976 case DataType::Type::kInt32:
4977 case DataType::Type::kReference: {
Roland Levillain4d027112015-07-01 15:41:14 +01004978 if (kPoisonHeapReferences && needs_write_barrier) {
4979 // Note that in the case where `value` is a null reference,
4980 // we do not enter this block, as the reference does not
4981 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004982 DCHECK_EQ(field_type, DataType::Type::kReference);
Roland Levillain4d027112015-07-01 15:41:14 +01004983 Register temp = locations->GetTemp(0).AsRegister<Register>();
4984 __ movl(temp, value.AsRegister<Register>());
4985 __ PoisonHeapReference(temp);
4986 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05004987 } else if (value.IsConstant()) {
4988 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4989 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01004990 } else {
Nicolas Geoffray03971632016-03-17 10:44:24 +00004991 DCHECK(value.IsRegister()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01004992 __ movl(Address(base, offset), value.AsRegister<Register>());
4993 }
Calin Juravle52c48962014-12-16 17:02:57 +00004994 break;
4995 }
4996
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004997 case DataType::Type::kInt64: {
Calin Juravle52c48962014-12-16 17:02:57 +00004998 if (is_volatile) {
4999 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
5000 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
5001 __ movd(temp1, value.AsRegisterPairLow<Register>());
5002 __ movd(temp2, value.AsRegisterPairHigh<Register>());
5003 __ punpckldq(temp1, temp2);
5004 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00005005 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05005006 } else if (value.IsConstant()) {
5007 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
5008 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
5009 codegen_->MaybeRecordImplicitNullCheck(instruction);
5010 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00005011 } else {
5012 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005013 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005014 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
5015 }
Mark Mendell81489372015-11-04 11:30:41 -05005016 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00005017 break;
5018 }
5019
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005020 case DataType::Type::kFloat32: {
Mark Mendell81489372015-11-04 11:30:41 -05005021 if (value.IsConstant()) {
5022 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5023 __ movl(Address(base, offset), Immediate(v));
5024 } else {
5025 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
5026 }
Calin Juravle52c48962014-12-16 17:02:57 +00005027 break;
5028 }
5029
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005030 case DataType::Type::kFloat64: {
Mark Mendell81489372015-11-04 11:30:41 -05005031 if (value.IsConstant()) {
5032 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
5033 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
5034 codegen_->MaybeRecordImplicitNullCheck(instruction);
5035 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
5036 maybe_record_implicit_null_check_done = true;
5037 } else {
5038 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
5039 }
Calin Juravle52c48962014-12-16 17:02:57 +00005040 break;
5041 }
5042
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005043 case DataType::Type::kVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00005044 LOG(FATAL) << "Unreachable type " << field_type;
5045 UNREACHABLE();
5046 }
5047
Mark Mendell81489372015-11-04 11:30:41 -05005048 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005049 codegen_->MaybeRecordImplicitNullCheck(instruction);
5050 }
5051
Roland Levillain4d027112015-07-01 15:41:14 +01005052 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005053 Register temp = locations->GetTemp(0).AsRegister<Register>();
5054 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005055 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00005056 }
5057
Calin Juravle52c48962014-12-16 17:02:57 +00005058 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005059 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00005060 }
5061}
5062
5063void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5064 HandleFieldGet(instruction, instruction->GetFieldInfo());
5065}
5066
5067void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5068 HandleFieldGet(instruction, instruction->GetFieldInfo());
5069}
5070
5071void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5072 HandleFieldSet(instruction, instruction->GetFieldInfo());
5073}
5074
5075void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005076 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005077}
5078
5079void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5080 HandleFieldSet(instruction, instruction->GetFieldInfo());
5081}
5082
5083void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005084 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005085}
5086
5087void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5088 HandleFieldGet(instruction, instruction->GetFieldInfo());
5089}
5090
5091void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5092 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005093}
5094
Calin Juravlee460d1d2015-09-29 04:52:17 +01005095void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
5096 HUnresolvedInstanceFieldGet* instruction) {
5097 FieldAccessCallingConventionX86 calling_convention;
5098 codegen_->CreateUnresolvedFieldLocationSummary(
5099 instruction, instruction->GetFieldType(), calling_convention);
5100}
5101
5102void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
5103 HUnresolvedInstanceFieldGet* instruction) {
5104 FieldAccessCallingConventionX86 calling_convention;
5105 codegen_->GenerateUnresolvedFieldAccess(instruction,
5106 instruction->GetFieldType(),
5107 instruction->GetFieldIndex(),
5108 instruction->GetDexPc(),
5109 calling_convention);
5110}
5111
5112void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
5113 HUnresolvedInstanceFieldSet* instruction) {
5114 FieldAccessCallingConventionX86 calling_convention;
5115 codegen_->CreateUnresolvedFieldLocationSummary(
5116 instruction, instruction->GetFieldType(), calling_convention);
5117}
5118
5119void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
5120 HUnresolvedInstanceFieldSet* instruction) {
5121 FieldAccessCallingConventionX86 calling_convention;
5122 codegen_->GenerateUnresolvedFieldAccess(instruction,
5123 instruction->GetFieldType(),
5124 instruction->GetFieldIndex(),
5125 instruction->GetDexPc(),
5126 calling_convention);
5127}
5128
5129void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
5130 HUnresolvedStaticFieldGet* instruction) {
5131 FieldAccessCallingConventionX86 calling_convention;
5132 codegen_->CreateUnresolvedFieldLocationSummary(
5133 instruction, instruction->GetFieldType(), calling_convention);
5134}
5135
5136void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
5137 HUnresolvedStaticFieldGet* instruction) {
5138 FieldAccessCallingConventionX86 calling_convention;
5139 codegen_->GenerateUnresolvedFieldAccess(instruction,
5140 instruction->GetFieldType(),
5141 instruction->GetFieldIndex(),
5142 instruction->GetDexPc(),
5143 calling_convention);
5144}
5145
5146void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
5147 HUnresolvedStaticFieldSet* instruction) {
5148 FieldAccessCallingConventionX86 calling_convention;
5149 codegen_->CreateUnresolvedFieldLocationSummary(
5150 instruction, instruction->GetFieldType(), calling_convention);
5151}
5152
5153void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
5154 HUnresolvedStaticFieldSet* instruction) {
5155 FieldAccessCallingConventionX86 calling_convention;
5156 codegen_->GenerateUnresolvedFieldAccess(instruction,
5157 instruction->GetFieldType(),
5158 instruction->GetFieldIndex(),
5159 instruction->GetDexPc(),
5160 calling_convention);
5161}
5162
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005163void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005164 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5165 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
5166 ? Location::RequiresRegister()
5167 : Location::Any();
5168 locations->SetInAt(0, loc);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005169}
5170
Calin Juravle2ae48182016-03-16 14:05:09 +00005171void CodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
5172 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005173 return;
5174 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005175 LocationSummary* locations = instruction->GetLocations();
5176 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005177
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005178 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00005179 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005180}
5181
Calin Juravle2ae48182016-03-16 14:05:09 +00005182void CodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07005183 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005184 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005185
5186 LocationSummary* locations = instruction->GetLocations();
5187 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005188
5189 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04005190 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005191 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005192 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005193 } else {
5194 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00005195 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005196 __ jmp(slow_path->GetEntryLabel());
5197 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005198 }
5199 __ j(kEqual, slow_path->GetEntryLabel());
5200}
5201
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005202void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005203 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005204}
5205
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005206void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005207 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005208 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005209 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00005210 new (GetGraph()->GetArena()) LocationSummary(instruction,
5211 object_array_get_with_read_barrier ?
5212 LocationSummary::kCallOnSlowPath :
5213 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005214 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005215 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005216 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005217 locations->SetInAt(0, Location::RequiresRegister());
5218 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005219 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005220 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5221 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005222 // The output overlaps in case of long: we don't want the low move
5223 // to overwrite the array's location. Likewise, in the case of an
5224 // object array get with read barriers enabled, we do not want the
5225 // move to overwrite the array's location, as we need it to emit
5226 // the read barrier.
5227 locations->SetOut(
5228 Location::RequiresRegister(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005229 (instruction->GetType() == DataType::Type::kInt64 || object_array_get_with_read_barrier)
5230 ? Location::kOutputOverlap
5231 : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005232 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005233}
5234
5235void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
5236 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005237 Location obj_loc = locations->InAt(0);
5238 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005239 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005240 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005241 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005242
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005243 DataType::Type type = instruction->GetType();
Calin Juravle77520bc2015-01-12 18:45:46 +00005244 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005245 case DataType::Type::kBool: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005246 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005247 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005248 break;
5249 }
5250
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005251 case DataType::Type::kInt8: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005252 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005253 __ movsxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005254 break;
5255 }
5256
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005257 case DataType::Type::kInt16: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005258 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005259 __ movsxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005260 break;
5261 }
5262
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005263 case DataType::Type::kUint16: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005264 Register out = out_loc.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07005265 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5266 // Branch cases into compressed and uncompressed for each index's type.
5267 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
5268 NearLabel done, not_compressed;
Vladimir Marko3c89d422017-02-17 11:30:23 +00005269 __ testb(Address(obj, count_offset), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005270 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005271 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
5272 "Expecting 0=compressed, 1=uncompressed");
5273 __ j(kNotZero, &not_compressed);
jessicahandojo4877b792016-09-08 19:49:13 -07005274 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
5275 __ jmp(&done);
5276 __ Bind(&not_compressed);
5277 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5278 __ Bind(&done);
5279 } else {
5280 // Common case for charAt of array of char or when string compression's
5281 // feature is turned off.
5282 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5283 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005284 break;
5285 }
5286
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005287 case DataType::Type::kInt32: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005288 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005289 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005290 break;
5291 }
5292
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005293 case DataType::Type::kReference: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005294 static_assert(
5295 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5296 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00005297 // /* HeapReference<Object> */ out =
5298 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5299 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005300 // Note that a potential implicit null check is handled in this
5301 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
5302 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00005303 instruction, out_loc, obj, data_offset, index, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005304 } else {
5305 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005306 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
5307 codegen_->MaybeRecordImplicitNullCheck(instruction);
5308 // If read barriers are enabled, emit read barriers other than
5309 // Baker's using a slow path (and also unpoison the loaded
5310 // reference, if heap poisoning is enabled).
Roland Levillain7c1559a2015-12-15 10:55:36 +00005311 if (index.IsConstant()) {
5312 uint32_t offset =
5313 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005314 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5315 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005316 codegen_->MaybeGenerateReadBarrierSlow(
5317 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5318 }
5319 }
5320 break;
5321 }
5322
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005323 case DataType::Type::kInt64: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005324 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005325 __ movl(out_loc.AsRegisterPairLow<Register>(),
5326 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
5327 codegen_->MaybeRecordImplicitNullCheck(instruction);
5328 __ movl(out_loc.AsRegisterPairHigh<Register>(),
5329 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005330 break;
5331 }
5332
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005333 case DataType::Type::kFloat32: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005334 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005335 __ movss(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005336 break;
5337 }
5338
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005339 case DataType::Type::kFloat64: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005340 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005341 __ movsd(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005342 break;
5343 }
5344
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005345 case DataType::Type::kVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00005346 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005347 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005348 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005349
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005350 if (type == DataType::Type::kReference || type == DataType::Type::kInt64) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005351 // Potential implicit null checks, in the case of reference or
5352 // long arrays, are handled in the previous switch statement.
5353 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005354 codegen_->MaybeRecordImplicitNullCheck(instruction);
5355 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005356}
5357
5358void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005359 DataType::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005360
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005361 bool needs_write_barrier =
5362 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005363 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005364
Nicolas Geoffray39468442014-09-02 15:17:15 +01005365 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
5366 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01005367 may_need_runtime_call_for_type_check ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00005368 LocationSummary::kCallOnSlowPath :
5369 LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005370
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005371 bool is_byte_type = (value_type == DataType::Type::kBool)
5372 || (value_type == DataType::Type::kInt8);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005373 // We need the inputs to be different than the output in case of long operation.
5374 // In case of a byte operation, the register allocator does not support multiple
5375 // inputs that die at entry with one in a specific register.
5376 locations->SetInAt(0, Location::RequiresRegister());
5377 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5378 if (is_byte_type) {
5379 // Ensure the value is in a byte register.
5380 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005381 } else if (DataType::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05005382 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005383 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005384 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
5385 }
5386 if (needs_write_barrier) {
5387 // Temporary registers for the write barrier.
5388 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5389 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005390 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005391 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005392}
5393
5394void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
5395 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005396 Location array_loc = locations->InAt(0);
5397 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005398 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005399 Location value = locations->InAt(2);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005400 DataType::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005401 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5402 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5403 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005404 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005405 bool needs_write_barrier =
5406 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005407
5408 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005409 case DataType::Type::kBool:
5410 case DataType::Type::kInt8: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005411 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005412 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_1, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005413 if (value.IsRegister()) {
5414 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005415 } else {
Nicolas Geoffray78612082017-07-24 14:18:53 +01005416 __ movb(address, Immediate(CodeGenerator::GetInt8ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005417 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005418 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005419 break;
5420 }
5421
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005422 case DataType::Type::kInt16:
5423 case DataType::Type::kUint16: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005424 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005425 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_2, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005426 if (value.IsRegister()) {
5427 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005428 } else {
Nicolas Geoffray78612082017-07-24 14:18:53 +01005429 __ movw(address, Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005430 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005431 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005432 break;
5433 }
5434
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005435 case DataType::Type::kReference: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005436 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005437 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005438
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005439 if (!value.IsRegister()) {
5440 // Just setting null.
5441 DCHECK(instruction->InputAt(2)->IsNullConstant());
5442 DCHECK(value.IsConstant()) << value;
5443 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00005444 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005445 DCHECK(!needs_write_barrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005446 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005447 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005448 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005449
5450 DCHECK(needs_write_barrier);
5451 Register register_value = value.AsRegister<Register>();
Roland Levillain16d9f942016-08-25 17:27:56 +01005452 // We cannot use a NearLabel for `done`, as its range may be too
5453 // short when Baker read barriers are enabled.
5454 Label done;
5455 NearLabel not_null, do_put;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005456 SlowPathCode* slow_path = nullptr;
Roland Levillain16d9f942016-08-25 17:27:56 +01005457 Location temp_loc = locations->GetTemp(0);
5458 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005459 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005460 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86(instruction);
5461 codegen_->AddSlowPath(slow_path);
5462 if (instruction->GetValueCanBeNull()) {
5463 __ testl(register_value, register_value);
5464 __ j(kNotEqual, &not_null);
5465 __ movl(address, Immediate(0));
5466 codegen_->MaybeRecordImplicitNullCheck(instruction);
5467 __ jmp(&done);
5468 __ Bind(&not_null);
5469 }
5470
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005471 // Note that when Baker read barriers are enabled, the type
5472 // checks are performed without read barriers. This is fine,
5473 // even in the case where a class object is in the from-space
5474 // after the flip, as a comparison involving such a type would
5475 // not produce a false positive; it may of course produce a
5476 // false negative, in which case we would take the ArraySet
5477 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01005478
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005479 // /* HeapReference<Class> */ temp = array->klass_
5480 __ movl(temp, Address(array, class_offset));
5481 codegen_->MaybeRecordImplicitNullCheck(instruction);
5482 __ MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01005483
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005484 // /* HeapReference<Class> */ temp = temp->component_type_
5485 __ movl(temp, Address(temp, component_offset));
5486 // If heap poisoning is enabled, no need to unpoison `temp`
5487 // nor the object reference in `register_value->klass`, as
5488 // we are comparing two poisoned references.
5489 __ cmpl(temp, Address(register_value, class_offset));
Roland Levillain16d9f942016-08-25 17:27:56 +01005490
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005491 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5492 __ j(kEqual, &do_put);
5493 // If heap poisoning is enabled, the `temp` reference has
5494 // not been unpoisoned yet; unpoison it now.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005495 __ MaybeUnpoisonHeapReference(temp);
5496
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005497 // If heap poisoning is enabled, no need to unpoison the
5498 // heap reference loaded below, as it is only used for a
5499 // comparison with null.
5500 __ cmpl(Address(temp, super_offset), Immediate(0));
5501 __ j(kNotEqual, slow_path->GetEntryLabel());
5502 __ Bind(&do_put);
5503 } else {
5504 __ j(kNotEqual, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005505 }
5506 }
5507
5508 if (kPoisonHeapReferences) {
5509 __ movl(temp, register_value);
5510 __ PoisonHeapReference(temp);
5511 __ movl(address, temp);
5512 } else {
5513 __ movl(address, register_value);
5514 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005515 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005516 codegen_->MaybeRecordImplicitNullCheck(instruction);
5517 }
5518
5519 Register card = locations->GetTemp(1).AsRegister<Register>();
5520 codegen_->MarkGCCard(
5521 temp, card, array, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
5522 __ Bind(&done);
5523
5524 if (slow_path != nullptr) {
5525 __ Bind(slow_path->GetExitLabel());
5526 }
5527
5528 break;
5529 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005530
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005531 case DataType::Type::kInt32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005532 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005533 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005534 if (value.IsRegister()) {
5535 __ movl(address, value.AsRegister<Register>());
5536 } else {
5537 DCHECK(value.IsConstant()) << value;
5538 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5539 __ movl(address, Immediate(v));
5540 }
5541 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005542 break;
5543 }
5544
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005545 case DataType::Type::kInt64: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005546 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005547 if (value.IsRegisterPair()) {
5548 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
5549 value.AsRegisterPairLow<Register>());
5550 codegen_->MaybeRecordImplicitNullCheck(instruction);
5551 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
5552 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005553 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005554 DCHECK(value.IsConstant());
5555 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
5556 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
5557 Immediate(Low32Bits(val)));
5558 codegen_->MaybeRecordImplicitNullCheck(instruction);
5559 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
5560 Immediate(High32Bits(val)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005561 }
5562 break;
5563 }
5564
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005565 case DataType::Type::kFloat32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005566 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005567 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005568 if (value.IsFpuRegister()) {
5569 __ movss(address, value.AsFpuRegister<XmmRegister>());
5570 } else {
5571 DCHECK(value.IsConstant());
5572 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
5573 __ movl(address, Immediate(v));
5574 }
5575 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005576 break;
5577 }
5578
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005579 case DataType::Type::kFloat64: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005580 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005581 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005582 if (value.IsFpuRegister()) {
5583 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5584 } else {
5585 DCHECK(value.IsConstant());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005586 Address address_hi =
5587 CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset + kX86WordSize);
Mark Mendell81489372015-11-04 11:30:41 -05005588 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5589 __ movl(address, Immediate(Low32Bits(v)));
5590 codegen_->MaybeRecordImplicitNullCheck(instruction);
5591 __ movl(address_hi, Immediate(High32Bits(v)));
5592 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005593 break;
5594 }
5595
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005596 case DataType::Type::kVoid:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005597 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005598 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005599 }
5600}
5601
5602void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
5603 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005604 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04005605 if (!instruction->IsEmittedAtUseSite()) {
5606 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5607 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005608}
5609
5610void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04005611 if (instruction->IsEmittedAtUseSite()) {
5612 return;
5613 }
5614
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005615 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005616 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005617 Register obj = locations->InAt(0).AsRegister<Register>();
5618 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005619 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005620 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo4877b792016-09-08 19:49:13 -07005621 // Mask out most significant bit in case the array is String's array of char.
5622 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005623 __ shrl(out, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005624 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005625}
5626
5627void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005628 RegisterSet caller_saves = RegisterSet::Empty();
5629 InvokeRuntimeCallingConvention calling_convention;
5630 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5631 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5632 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005633 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04005634 HInstruction* length = instruction->InputAt(1);
5635 if (!length->IsEmittedAtUseSite()) {
5636 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5637 }
jessicahandojo4877b792016-09-08 19:49:13 -07005638 // Need register to see array's length.
5639 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5640 locations->AddTemp(Location::RequiresRegister());
5641 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005642}
5643
5644void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
jessicahandojo4877b792016-09-08 19:49:13 -07005645 const bool is_string_compressed_char_at =
5646 mirror::kUseStringCompression && instruction->IsStringCharAt();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005647 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005648 Location index_loc = locations->InAt(0);
5649 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07005650 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005651 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005652
Mark Mendell99dbd682015-04-22 16:18:52 -04005653 if (length_loc.IsConstant()) {
5654 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5655 if (index_loc.IsConstant()) {
5656 // BCE will remove the bounds check if we are guarenteed to pass.
5657 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5658 if (index < 0 || index >= length) {
5659 codegen_->AddSlowPath(slow_path);
5660 __ jmp(slow_path->GetEntryLabel());
5661 } else {
5662 // Some optimization after BCE may have generated this, and we should not
5663 // generate a bounds check if it is a valid range.
5664 }
5665 return;
5666 }
5667
5668 // We have to reverse the jump condition because the length is the constant.
5669 Register index_reg = index_loc.AsRegister<Register>();
5670 __ cmpl(index_reg, Immediate(length));
5671 codegen_->AddSlowPath(slow_path);
5672 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005673 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04005674 HInstruction* array_length = instruction->InputAt(1);
5675 if (array_length->IsEmittedAtUseSite()) {
5676 // Address the length field in the array.
5677 DCHECK(array_length->IsArrayLength());
5678 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
5679 Location array_loc = array_length->GetLocations()->InAt(0);
5680 Address array_len(array_loc.AsRegister<Register>(), len_offset);
jessicahandojo4877b792016-09-08 19:49:13 -07005681 if (is_string_compressed_char_at) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005682 // TODO: if index_loc.IsConstant(), compare twice the index (to compensate for
5683 // the string compression flag) with the in-memory length and avoid the temporary.
jessicahandojo4877b792016-09-08 19:49:13 -07005684 Register length_reg = locations->GetTemp(0).AsRegister<Register>();
5685 __ movl(length_reg, array_len);
5686 codegen_->MaybeRecordImplicitNullCheck(array_length);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005687 __ shrl(length_reg, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005688 codegen_->GenerateIntCompare(length_reg, index_loc);
Mark Mendellee8d9712016-07-12 11:13:15 -04005689 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07005690 // Checking bounds for general case:
5691 // Array of char or string's array with feature compression off.
5692 if (index_loc.IsConstant()) {
5693 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5694 __ cmpl(array_len, Immediate(value));
5695 } else {
5696 __ cmpl(array_len, index_loc.AsRegister<Register>());
5697 }
5698 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendellee8d9712016-07-12 11:13:15 -04005699 }
Mark Mendell99dbd682015-04-22 16:18:52 -04005700 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005701 codegen_->GenerateIntCompare(length_loc, index_loc);
Mark Mendell99dbd682015-04-22 16:18:52 -04005702 }
5703 codegen_->AddSlowPath(slow_path);
5704 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005705 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005706}
5707
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005708void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005709 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005710}
5711
5712void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005713 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5714}
5715
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005716void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005717 LocationSummary* locations =
5718 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Aart Bikb13c65b2017-03-21 20:14:07 -07005719 // In suspend check slow path, usually there are no caller-save registers at all.
5720 // If SIMD instructions are present, however, we force spilling all live SIMD
5721 // registers in full width (since the runtime only saves/restores lower part).
Aart Bik5576f372017-03-23 16:17:37 -07005722 locations->SetCustomSlowPathCallerSaves(
5723 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005724}
5725
5726void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005727 HBasicBlock* block = instruction->GetBlock();
5728 if (block->GetLoopInformation() != nullptr) {
5729 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5730 // The back edge will generate the suspend check.
5731 return;
5732 }
5733 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5734 // The goto will generate the suspend check.
5735 return;
5736 }
5737 GenerateSuspendCheck(instruction, nullptr);
5738}
5739
5740void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
5741 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005742 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005743 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
5744 if (slow_path == nullptr) {
5745 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
5746 instruction->SetSlowPath(slow_path);
5747 codegen_->AddSlowPath(slow_path);
5748 if (successor != nullptr) {
5749 DCHECK(successor->IsLoopHeader());
5750 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5751 }
5752 } else {
5753 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5754 }
5755
Andreas Gampe542451c2016-07-26 09:02:02 -07005756 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86PointerSize>().Int32Value()),
Roland Levillain7c1559a2015-12-15 10:55:36 +00005757 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005758 if (successor == nullptr) {
5759 __ j(kNotEqual, slow_path->GetEntryLabel());
5760 __ Bind(slow_path->GetReturnLabel());
5761 } else {
5762 __ j(kEqual, codegen_->GetLabelOf(successor));
5763 __ jmp(slow_path->GetEntryLabel());
5764 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005765}
5766
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005767X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
5768 return codegen_->GetAssembler();
5769}
5770
Mark Mendell7c8d0092015-01-26 11:21:33 -05005771void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005772 ScratchRegisterScope ensure_scratch(
5773 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5774 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5775 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5776 __ movl(temp_reg, Address(ESP, src + stack_offset));
5777 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005778}
5779
5780void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005781 ScratchRegisterScope ensure_scratch(
5782 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5783 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5784 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5785 __ movl(temp_reg, Address(ESP, src + stack_offset));
5786 __ movl(Address(ESP, dst + stack_offset), temp_reg);
5787 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
5788 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005789}
5790
5791void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005792 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005793 Location source = move->GetSource();
5794 Location destination = move->GetDestination();
5795
5796 if (source.IsRegister()) {
5797 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005798 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005799 } else if (destination.IsFpuRegister()) {
5800 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005801 } else {
5802 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005803 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005804 }
David Brazdil74eb1b22015-12-14 11:44:01 +00005805 } else if (source.IsRegisterPair()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005806 size_t elem_size = DataType::Size(DataType::Type::kInt32);
David Brazdil74eb1b22015-12-14 11:44:01 +00005807 // Create stack space for 2 elements.
5808 __ subl(ESP, Immediate(2 * elem_size));
5809 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
5810 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
5811 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
5812 // And remove the temporary stack space we allocated.
5813 __ addl(ESP, Immediate(2 * elem_size));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005814 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005815 if (destination.IsRegister()) {
5816 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
5817 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005818 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005819 } else if (destination.IsRegisterPair()) {
5820 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
5821 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
5822 __ psrlq(src_reg, Immediate(32));
5823 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005824 } else if (destination.IsStackSlot()) {
5825 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07005826 } else if (destination.IsDoubleStackSlot()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005827 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07005828 } else {
5829 DCHECK(destination.IsSIMDStackSlot());
5830 __ movups(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005831 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005832 } else if (source.IsStackSlot()) {
5833 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005834 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005835 } else if (destination.IsFpuRegister()) {
5836 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005837 } else {
5838 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005839 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
5840 }
5841 } else if (source.IsDoubleStackSlot()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005842 if (destination.IsRegisterPair()) {
5843 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
5844 __ movl(destination.AsRegisterPairHigh<Register>(),
5845 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
5846 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005847 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
5848 } else {
5849 DCHECK(destination.IsDoubleStackSlot()) << destination;
5850 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005851 }
Aart Bik5576f372017-03-23 16:17:37 -07005852 } else if (source.IsSIMDStackSlot()) {
5853 DCHECK(destination.IsFpuRegister());
5854 __ movups(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005855 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005856 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005857 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005858 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005859 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005860 if (value == 0) {
5861 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
5862 } else {
5863 __ movl(destination.AsRegister<Register>(), Immediate(value));
5864 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005865 } else {
5866 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05005867 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005868 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005869 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005870 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005871 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005872 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005873 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005874 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5875 if (value == 0) {
5876 // Easy handling of 0.0.
5877 __ xorps(dest, dest);
5878 } else {
5879 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005880 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5881 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
5882 __ movl(temp, Immediate(value));
5883 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005884 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005885 } else {
5886 DCHECK(destination.IsStackSlot()) << destination;
5887 __ movl(Address(ESP, destination.GetStackIndex()), imm);
5888 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005889 } else if (constant->IsLongConstant()) {
5890 int64_t value = constant->AsLongConstant()->GetValue();
5891 int32_t low_value = Low32Bits(value);
5892 int32_t high_value = High32Bits(value);
5893 Immediate low(low_value);
5894 Immediate high(high_value);
5895 if (destination.IsDoubleStackSlot()) {
5896 __ movl(Address(ESP, destination.GetStackIndex()), low);
5897 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5898 } else {
5899 __ movl(destination.AsRegisterPairLow<Register>(), low);
5900 __ movl(destination.AsRegisterPairHigh<Register>(), high);
5901 }
5902 } else {
5903 DCHECK(constant->IsDoubleConstant());
5904 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005905 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005906 int32_t low_value = Low32Bits(value);
5907 int32_t high_value = High32Bits(value);
5908 Immediate low(low_value);
5909 Immediate high(high_value);
5910 if (destination.IsFpuRegister()) {
5911 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5912 if (value == 0) {
5913 // Easy handling of 0.0.
5914 __ xorpd(dest, dest);
5915 } else {
5916 __ pushl(high);
5917 __ pushl(low);
5918 __ movsd(dest, Address(ESP, 0));
5919 __ addl(ESP, Immediate(8));
5920 }
5921 } else {
5922 DCHECK(destination.IsDoubleStackSlot()) << destination;
5923 __ movl(Address(ESP, destination.GetStackIndex()), low);
5924 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5925 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005926 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005927 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005928 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005929 }
5930}
5931
Mark Mendella5c19ce2015-04-01 12:51:05 -04005932void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005933 Register suggested_scratch = reg == EAX ? EBX : EAX;
5934 ScratchRegisterScope ensure_scratch(
5935 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
5936
5937 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5938 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
5939 __ movl(Address(ESP, mem + stack_offset), reg);
5940 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005941}
5942
Mark Mendell7c8d0092015-01-26 11:21:33 -05005943void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005944 ScratchRegisterScope ensure_scratch(
5945 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5946
5947 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5948 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5949 __ movl(temp_reg, Address(ESP, mem + stack_offset));
5950 __ movss(Address(ESP, mem + stack_offset), reg);
5951 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005952}
5953
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005954void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005955 ScratchRegisterScope ensure_scratch1(
5956 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005957
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005958 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
5959 ScratchRegisterScope ensure_scratch2(
5960 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005961
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005962 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
5963 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
5964 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
5965 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
5966 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
5967 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005968}
5969
5970void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005971 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005972 Location source = move->GetSource();
5973 Location destination = move->GetDestination();
5974
5975 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04005976 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
5977 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
5978 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
5979 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
5980 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005981 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005982 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005983 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005984 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005985 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5986 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005987 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
5988 // Use XOR Swap algorithm to avoid a temporary.
5989 DCHECK_NE(source.reg(), destination.reg());
5990 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5991 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5992 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5993 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
5994 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
5995 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
5996 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005997 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
5998 // Take advantage of the 16 bytes in the XMM register.
5999 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
6000 Address stack(ESP, destination.GetStackIndex());
6001 // Load the double into the high doubleword.
6002 __ movhpd(reg, stack);
6003
6004 // Store the low double into the destination.
6005 __ movsd(stack, reg);
6006
6007 // Move the high double to the low double.
6008 __ psrldq(reg, Immediate(8));
6009 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
6010 // Take advantage of the 16 bytes in the XMM register.
6011 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
6012 Address stack(ESP, source.GetStackIndex());
6013 // Load the double into the high doubleword.
6014 __ movhpd(reg, stack);
6015
6016 // Store the low double into the destination.
6017 __ movsd(stack, reg);
6018
6019 // Move the high double to the low double.
6020 __ psrldq(reg, Immediate(8));
6021 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
6022 Exchange(destination.GetStackIndex(), source.GetStackIndex());
6023 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006024 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006025 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006026 }
6027}
6028
6029void ParallelMoveResolverX86::SpillScratch(int reg) {
6030 __ pushl(static_cast<Register>(reg));
6031}
6032
6033void ParallelMoveResolverX86::RestoreScratch(int reg) {
6034 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006035}
6036
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006037HLoadClass::LoadKind CodeGeneratorX86::GetSupportedLoadClassKind(
6038 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006039 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006040 case HLoadClass::LoadKind::kInvalid:
6041 LOG(FATAL) << "UNREACHABLE";
6042 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006043 case HLoadClass::LoadKind::kReferrersClass:
6044 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006045 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006046 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006047 case HLoadClass::LoadKind::kBssEntry:
Vladimir Marko764d4542017-05-16 10:31:41 +01006048 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006049 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006050 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006051 DCHECK(Runtime::Current()->UseJitCompilation());
6052 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01006053 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006054 case HLoadClass::LoadKind::kRuntimeCall:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006055 break;
6056 }
6057 return desired_class_load_kind;
6058}
6059
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006060void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00006061 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006062 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006063 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00006064 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006065 cls,
6066 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00006067 Location::RegisterLocation(EAX));
Vladimir Markoea4c1262017-02-06 19:59:33 +00006068 DCHECK_EQ(calling_convention.GetRegisterAt(0), EAX);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006069 return;
6070 }
Vladimir Marko41559982017-01-06 14:04:23 +00006071 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006072
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006073 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
6074 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006075 ? LocationSummary::kCallOnSlowPath
6076 : LocationSummary::kNoCall;
6077 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006078 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006079 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006080 }
6081
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006082 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006083 load_kind == HLoadClass::LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006084 load_kind == HLoadClass::LoadKind::kBootImageClassTable ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006085 load_kind == HLoadClass::LoadKind::kBssEntry) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006086 locations->SetInAt(0, Location::RequiresRegister());
6087 }
6088 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006089 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
6090 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6091 // Rely on the type resolution and/or initialization to save everything.
6092 RegisterSet caller_saves = RegisterSet::Empty();
6093 InvokeRuntimeCallingConvention calling_convention;
6094 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6095 locations->SetCustomSlowPathCallerSaves(caller_saves);
6096 } else {
6097 // For non-Baker read barrier we have a temp-clobbering call.
6098 }
6099 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006100}
6101
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006102Label* CodeGeneratorX86::NewJitRootClassPatch(const DexFile& dex_file,
6103 dex::TypeIndex dex_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006104 Handle<mirror::Class> handle) {
6105 jit_class_roots_.Overwrite(TypeReference(&dex_file, dex_index),
6106 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006107 // Add a patch entry and return the label.
6108 jit_class_patches_.emplace_back(dex_file, dex_index.index_);
6109 PatchInfo<Label>* info = &jit_class_patches_.back();
6110 return &info->label;
6111}
6112
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006113// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6114// move.
6115void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00006116 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006117 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00006118 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01006119 return;
6120 }
Vladimir Marko41559982017-01-06 14:04:23 +00006121 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01006122
Vladimir Marko41559982017-01-06 14:04:23 +00006123 LocationSummary* locations = cls->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006124 Location out_loc = locations->Out();
6125 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006126
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006127 bool generate_null_check = false;
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006128 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
6129 ? kWithoutReadBarrier
6130 : kCompilerReadBarrierOption;
Vladimir Marko41559982017-01-06 14:04:23 +00006131 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006132 case HLoadClass::LoadKind::kReferrersClass: {
6133 DCHECK(!cls->CanCallRuntime());
6134 DCHECK(!cls->MustGenerateClinitCheck());
6135 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6136 Register current_method = locations->InAt(0).AsRegister<Register>();
6137 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006138 cls,
6139 out_loc,
6140 Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()),
Roland Levillain00468f32016-10-27 18:02:48 +01006141 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006142 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006143 break;
6144 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006145 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006146 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006147 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006148 Register method_address = locations->InAt(0).AsRegister<Register>();
6149 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Marko1998cd02017-01-13 13:02:58 +00006150 codegen_->RecordBootTypePatch(cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006151 break;
6152 }
6153 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006154 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006155 uint32_t address = dchecked_integral_cast<uint32_t>(
6156 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
6157 DCHECK_NE(address, 0u);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006158 __ movl(out, Immediate(address));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006159 break;
6160 }
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006161 case HLoadClass::LoadKind::kBootImageClassTable: {
6162 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6163 Register method_address = locations->InAt(0).AsRegister<Register>();
6164 __ movl(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
6165 codegen_->RecordBootTypePatch(cls);
6166 // Extract the reference from the slot data, i.e. clear the hash bits.
6167 int32_t masked_hash = ClassTable::TableSlot::MaskHash(
6168 ComputeModifiedUtf8Hash(cls->GetDexFile().StringByTypeIdx(cls->GetTypeIndex())));
6169 if (masked_hash != 0) {
6170 __ subl(out, Immediate(masked_hash));
6171 }
6172 break;
6173 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006174 case HLoadClass::LoadKind::kBssEntry: {
6175 Register method_address = locations->InAt(0).AsRegister<Register>();
6176 Address address(method_address, CodeGeneratorX86::kDummy32BitOffset);
6177 Label* fixup_label = codegen_->NewTypeBssEntryPatch(cls);
6178 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
6179 generate_null_check = true;
6180 break;
6181 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006182 case HLoadClass::LoadKind::kJitTableAddress: {
6183 Address address = Address::Absolute(CodeGeneratorX86::kDummy32BitOffset);
6184 Label* fixup_label = codegen_->NewJitRootClassPatch(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006185 cls->GetDexFile(), cls->GetTypeIndex(), cls->GetClass());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006186 // /* GcRoot<mirror::Class> */ out = *address
Vladimir Markoea4c1262017-02-06 19:59:33 +00006187 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006188 break;
6189 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006190 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006191 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00006192 LOG(FATAL) << "UNREACHABLE";
6193 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006194 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006195
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006196 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6197 DCHECK(cls->CanCallRuntime());
6198 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
6199 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
6200 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006201
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006202 if (generate_null_check) {
6203 __ testl(out, out);
6204 __ j(kEqual, slow_path->GetEntryLabel());
6205 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006206
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006207 if (cls->MustGenerateClinitCheck()) {
6208 GenerateClassInitializationCheck(slow_path, out);
6209 } else {
6210 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006211 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006212 }
6213}
6214
6215void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
6216 LocationSummary* locations =
6217 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
6218 locations->SetInAt(0, Location::RequiresRegister());
6219 if (check->HasUses()) {
6220 locations->SetOut(Location::SameAsFirstInput());
6221 }
6222}
6223
6224void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006225 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07006226 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006227 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006228 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00006229 GenerateClassInitializationCheck(slow_path,
6230 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006231}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006232
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006233void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07006234 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006235 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
6236 Immediate(mirror::Class::kStatusInitialized));
6237 __ j(kLess, slow_path->GetEntryLabel());
6238 __ Bind(slow_path->GetExitLabel());
6239 // No need for memory fence, thanks to the X86 memory model.
6240}
6241
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006242HLoadString::LoadKind CodeGeneratorX86::GetSupportedLoadStringKind(
6243 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006244 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006245 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006246 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00006247 case HLoadString::LoadKind::kBssEntry:
Vladimir Marko764d4542017-05-16 10:31:41 +01006248 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006249 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006250 case HLoadString::LoadKind::kJitTableAddress:
6251 DCHECK(Runtime::Current()->UseJitCompilation());
6252 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01006253 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006254 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006255 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006256 }
6257 return desired_string_load_kind;
6258}
6259
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006260void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006261 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006262 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006263 HLoadString::LoadKind load_kind = load->GetLoadKind();
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006264 if (load_kind == HLoadString::LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006265 load_kind == HLoadString::LoadKind::kBootImageInternTable ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00006266 load_kind == HLoadString::LoadKind::kBssEntry) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006267 locations->SetInAt(0, Location::RequiresRegister());
6268 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006269 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006270 locations->SetOut(Location::RegisterLocation(EAX));
6271 } else {
6272 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006273 if (load_kind == HLoadString::LoadKind::kBssEntry) {
6274 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00006275 // Rely on the pResolveString to save everything.
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006276 RegisterSet caller_saves = RegisterSet::Empty();
6277 InvokeRuntimeCallingConvention calling_convention;
6278 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6279 locations->SetCustomSlowPathCallerSaves(caller_saves);
6280 } else {
6281 // For non-Baker read barrier we have a temp-clobbering call.
6282 }
6283 }
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006284 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006285}
6286
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006287Label* CodeGeneratorX86::NewJitRootStringPatch(const DexFile& dex_file,
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006288 dex::StringIndex dex_index,
6289 Handle<mirror::String> handle) {
6290 jit_string_roots_.Overwrite(
6291 StringReference(&dex_file, dex_index), reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006292 // Add a patch entry and return the label.
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006293 jit_string_patches_.emplace_back(dex_file, dex_index.index_);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006294 PatchInfo<Label>* info = &jit_string_patches_.back();
6295 return &info->label;
6296}
6297
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006298// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6299// move.
6300void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006301 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006302 Location out_loc = locations->Out();
6303 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006304
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006305 switch (load->GetLoadKind()) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006306 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006307 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006308 Register method_address = locations->InAt(0).AsRegister<Register>();
6309 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Markoaad75c62016-10-03 08:46:48 +00006310 codegen_->RecordBootStringPatch(load);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006311 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006312 }
6313 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006314 uint32_t address = dchecked_integral_cast<uint32_t>(
6315 reinterpret_cast<uintptr_t>(load->GetString().Get()));
6316 DCHECK_NE(address, 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006317 __ movl(out, Immediate(address));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006318 return;
6319 }
6320 case HLoadString::LoadKind::kBootImageInternTable: {
6321 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6322 Register method_address = locations->InAt(0).AsRegister<Register>();
6323 __ movl(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
6324 codegen_->RecordBootStringPatch(load);
6325 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006326 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00006327 case HLoadString::LoadKind::kBssEntry: {
6328 Register method_address = locations->InAt(0).AsRegister<Register>();
6329 Address address = Address(method_address, CodeGeneratorX86::kDummy32BitOffset);
6330 Label* fixup_label = codegen_->NewStringBssEntryPatch(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006331 // /* GcRoot<mirror::String> */ out = *address /* PC-relative */
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006332 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
Vladimir Markoaad75c62016-10-03 08:46:48 +00006333 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
6334 codegen_->AddSlowPath(slow_path);
6335 __ testl(out, out);
6336 __ j(kEqual, slow_path->GetEntryLabel());
6337 __ Bind(slow_path->GetExitLabel());
6338 return;
6339 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006340 case HLoadString::LoadKind::kJitTableAddress: {
6341 Address address = Address::Absolute(CodeGeneratorX86::kDummy32BitOffset);
6342 Label* fixup_label = codegen_->NewJitRootStringPatch(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006343 load->GetDexFile(), load->GetStringIndex(), load->GetString());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006344 // /* GcRoot<mirror::String> */ out = *address
6345 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
6346 return;
6347 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006348 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006349 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006350 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006351
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006352 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006353 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006354 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006355 __ movl(calling_convention.GetRegisterAt(0), Immediate(load->GetStringIndex().index_));
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006356 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6357 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006358}
6359
David Brazdilcb1c0552015-08-04 16:22:25 +01006360static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006361 return Address::Absolute(Thread::ExceptionOffset<kX86PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01006362}
6363
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006364void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
6365 LocationSummary* locations =
6366 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
6367 locations->SetOut(Location::RequiresRegister());
6368}
6369
6370void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01006371 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
6372}
6373
6374void LocationsBuilderX86::VisitClearException(HClearException* clear) {
6375 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
6376}
6377
6378void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6379 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006380}
6381
6382void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
6383 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006384 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006385 InvokeRuntimeCallingConvention calling_convention;
6386 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6387}
6388
6389void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006390 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006391 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006392}
6393
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006394// Temp is used for read barrier.
6395static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
6396 if (kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00006397 !kUseBakerReadBarrier &&
6398 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain7c1559a2015-12-15 10:55:36 +00006399 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006400 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
6401 return 1;
6402 }
6403 return 0;
6404}
6405
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006406// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006407// interface pointer, one for loading the current interface.
6408// The other checks have one temp for loading the object's class.
6409static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
6410 if (type_check_kind == TypeCheckKind::kInterfaceCheck && !kPoisonHeapReferences) {
6411 return 2;
6412 }
6413 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006414}
6415
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006416void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006417 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006418 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01006419 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006420 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006421 case TypeCheckKind::kExactCheck:
6422 case TypeCheckKind::kAbstractClassCheck:
6423 case TypeCheckKind::kClassHierarchyCheck:
6424 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006425 call_kind =
6426 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01006427 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006428 break;
6429 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006430 case TypeCheckKind::kUnresolvedCheck:
6431 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006432 call_kind = LocationSummary::kCallOnSlowPath;
6433 break;
6434 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006435
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006436 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01006437 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006438 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006439 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006440 locations->SetInAt(0, Location::RequiresRegister());
6441 locations->SetInAt(1, Location::Any());
6442 // Note that TypeCheckSlowPathX86 uses this "out" register too.
6443 locations->SetOut(Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006444 // When read barriers are enabled, we need a temporary register for some cases.
6445 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006446}
6447
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006448void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006449 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006450 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006451 Location obj_loc = locations->InAt(0);
6452 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006453 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006454 Location out_loc = locations->Out();
6455 Register out = out_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006456 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
6457 DCHECK_LE(num_temps, 1u);
6458 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006459 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006460 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6461 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6462 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07006463 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006464 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006465
6466 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006467 // Avoid null check if we know obj is not null.
6468 if (instruction->MustDoNullCheck()) {
6469 __ testl(obj, obj);
6470 __ j(kEqual, &zero);
6471 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006472
Roland Levillain7c1559a2015-12-15 10:55:36 +00006473 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006474 case TypeCheckKind::kExactCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006475 // /* HeapReference<Class> */ out = obj->klass_
6476 GenerateReferenceLoadTwoRegisters(instruction,
6477 out_loc,
6478 obj_loc,
6479 class_offset,
6480 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006481 if (cls.IsRegister()) {
6482 __ cmpl(out, cls.AsRegister<Register>());
6483 } else {
6484 DCHECK(cls.IsStackSlot()) << cls;
6485 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6486 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006487
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006488 // Classes must be equal for the instanceof to succeed.
6489 __ j(kNotEqual, &zero);
6490 __ movl(out, Immediate(1));
6491 __ jmp(&done);
6492 break;
6493 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006494
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006495 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006496 // /* HeapReference<Class> */ out = obj->klass_
6497 GenerateReferenceLoadTwoRegisters(instruction,
6498 out_loc,
6499 obj_loc,
6500 class_offset,
6501 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006502 // If the class is abstract, we eagerly fetch the super class of the
6503 // object to avoid doing a comparison we know will fail.
6504 NearLabel loop;
6505 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006506 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006507 GenerateReferenceLoadOneRegister(instruction,
6508 out_loc,
6509 super_offset,
6510 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006511 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006512 __ testl(out, out);
6513 // If `out` is null, we use it for the result, and jump to `done`.
6514 __ j(kEqual, &done);
6515 if (cls.IsRegister()) {
6516 __ cmpl(out, cls.AsRegister<Register>());
6517 } else {
6518 DCHECK(cls.IsStackSlot()) << cls;
6519 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6520 }
6521 __ j(kNotEqual, &loop);
6522 __ movl(out, Immediate(1));
6523 if (zero.IsLinked()) {
6524 __ jmp(&done);
6525 }
6526 break;
6527 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006528
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006529 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006530 // /* HeapReference<Class> */ out = obj->klass_
6531 GenerateReferenceLoadTwoRegisters(instruction,
6532 out_loc,
6533 obj_loc,
6534 class_offset,
6535 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006536 // Walk over the class hierarchy to find a match.
6537 NearLabel loop, success;
6538 __ Bind(&loop);
6539 if (cls.IsRegister()) {
6540 __ cmpl(out, cls.AsRegister<Register>());
6541 } else {
6542 DCHECK(cls.IsStackSlot()) << cls;
6543 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6544 }
6545 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006546 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006547 GenerateReferenceLoadOneRegister(instruction,
6548 out_loc,
6549 super_offset,
6550 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006551 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006552 __ testl(out, out);
6553 __ j(kNotEqual, &loop);
6554 // If `out` is null, we use it for the result, and jump to `done`.
6555 __ jmp(&done);
6556 __ Bind(&success);
6557 __ movl(out, Immediate(1));
6558 if (zero.IsLinked()) {
6559 __ jmp(&done);
6560 }
6561 break;
6562 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006563
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006564 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006565 // /* HeapReference<Class> */ out = obj->klass_
6566 GenerateReferenceLoadTwoRegisters(instruction,
6567 out_loc,
6568 obj_loc,
6569 class_offset,
6570 kCompilerReadBarrierOption);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006571 // Do an exact check.
6572 NearLabel exact_check;
6573 if (cls.IsRegister()) {
6574 __ cmpl(out, cls.AsRegister<Register>());
6575 } else {
6576 DCHECK(cls.IsStackSlot()) << cls;
6577 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6578 }
6579 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006580 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006581 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006582 GenerateReferenceLoadOneRegister(instruction,
6583 out_loc,
6584 component_offset,
6585 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006586 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006587 __ testl(out, out);
6588 // If `out` is null, we use it for the result, and jump to `done`.
6589 __ j(kEqual, &done);
6590 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
6591 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006592 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006593 __ movl(out, Immediate(1));
6594 __ jmp(&done);
6595 break;
6596 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006597
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006598 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006599 // No read barrier since the slow path will retry upon failure.
6600 // /* HeapReference<Class> */ out = obj->klass_
6601 GenerateReferenceLoadTwoRegisters(instruction,
6602 out_loc,
6603 obj_loc,
6604 class_offset,
6605 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006606 if (cls.IsRegister()) {
6607 __ cmpl(out, cls.AsRegister<Register>());
6608 } else {
6609 DCHECK(cls.IsStackSlot()) << cls;
6610 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6611 }
6612 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006613 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6614 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006615 codegen_->AddSlowPath(slow_path);
6616 __ j(kNotEqual, slow_path->GetEntryLabel());
6617 __ movl(out, Immediate(1));
6618 if (zero.IsLinked()) {
6619 __ jmp(&done);
6620 }
6621 break;
6622 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006623
Calin Juravle98893e12015-10-02 21:05:03 +01006624 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006625 case TypeCheckKind::kInterfaceCheck: {
6626 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006627 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00006628 // cases.
6629 //
6630 // We cannot directly call the InstanceofNonTrivial runtime
6631 // entry point without resorting to a type checking slow path
6632 // here (i.e. by calling InvokeRuntime directly), as it would
6633 // require to assign fixed registers for the inputs of this
6634 // HInstanceOf instruction (following the runtime calling
6635 // convention), which might be cluttered by the potential first
6636 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006637 //
6638 // TODO: Introduce a new runtime entry point taking the object
6639 // to test (instead of its class) as argument, and let it deal
6640 // with the read barrier issues. This will let us refactor this
6641 // case of the `switch` code as it was previously (with a direct
6642 // call to the runtime not using a type checking slow path).
6643 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006644 DCHECK(locations->OnlyCallsOnSlowPath());
6645 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6646 /* is_fatal */ false);
6647 codegen_->AddSlowPath(slow_path);
6648 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006649 if (zero.IsLinked()) {
6650 __ jmp(&done);
6651 }
6652 break;
6653 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006654 }
6655
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006656 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006657 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006658 __ xorl(out, out);
6659 }
6660
6661 if (done.IsLinked()) {
6662 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006663 }
6664
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006665 if (slow_path != nullptr) {
6666 __ Bind(slow_path->GetExitLabel());
6667 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006668}
6669
Mathieu Chartier5ac321b2016-11-09 16:33:54 -08006670static bool IsTypeCheckSlowPathFatal(TypeCheckKind type_check_kind, bool throws_into_catch) {
6671 switch (type_check_kind) {
6672 case TypeCheckKind::kExactCheck:
6673 case TypeCheckKind::kAbstractClassCheck:
6674 case TypeCheckKind::kClassHierarchyCheck:
6675 case TypeCheckKind::kArrayObjectCheck:
6676 return !throws_into_catch && !kEmitCompilerReadBarrier;
6677 case TypeCheckKind::kInterfaceCheck:
6678 return !throws_into_catch && !kEmitCompilerReadBarrier && !kPoisonHeapReferences;
6679 case TypeCheckKind::kArrayCheck:
6680 case TypeCheckKind::kUnresolvedCheck:
6681 return false;
6682 }
6683 LOG(FATAL) << "Unreachable";
6684 UNREACHABLE();
6685}
6686
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006687void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006688 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006689 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Mathieu Chartier5ac321b2016-11-09 16:33:54 -08006690 LocationSummary::CallKind call_kind =
6691 IsTypeCheckSlowPathFatal(type_check_kind, throws_into_catch)
6692 ? LocationSummary::kNoCall
6693 : LocationSummary::kCallOnSlowPath;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006694 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6695 locations->SetInAt(0, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006696 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
6697 // Require a register for the interface check since there is a loop that compares the class to
6698 // a memory address.
6699 locations->SetInAt(1, Location::RequiresRegister());
6700 } else {
6701 locations->SetInAt(1, Location::Any());
6702 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006703 // Note that TypeCheckSlowPathX86 uses this "temp" register too.
6704 locations->AddTemp(Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006705 // When read barriers are enabled, we need an additional temporary register for some cases.
6706 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
6707}
6708
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006709void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006710 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006711 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006712 Location obj_loc = locations->InAt(0);
6713 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006714 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006715 Location temp_loc = locations->GetTemp(0);
6716 Register temp = temp_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006717 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
6718 DCHECK_GE(num_temps, 1u);
6719 DCHECK_LE(num_temps, 2u);
6720 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
6721 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6722 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6723 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6724 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
6725 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
6726 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
6727 const uint32_t object_array_data_offset =
6728 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006729
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006730 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
6731 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
6732 // read barriers is done for performance and code size reasons.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006733 bool is_type_check_slow_path_fatal =
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006734 IsTypeCheckSlowPathFatal(type_check_kind, instruction->CanThrowIntoCatchBlock());
6735
Roland Levillain0d5a2812015-11-13 10:07:31 +00006736 SlowPathCode* type_check_slow_path =
6737 new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6738 is_type_check_slow_path_fatal);
6739 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006740
Roland Levillain0d5a2812015-11-13 10:07:31 +00006741 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006742 // Avoid null check if we know obj is not null.
6743 if (instruction->MustDoNullCheck()) {
6744 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006745 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006746 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006747
Roland Levillain0d5a2812015-11-13 10:07:31 +00006748 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006749 case TypeCheckKind::kExactCheck:
6750 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006751 // /* HeapReference<Class> */ temp = obj->klass_
6752 GenerateReferenceLoadTwoRegisters(instruction,
6753 temp_loc,
6754 obj_loc,
6755 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006756 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006757
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006758 if (cls.IsRegister()) {
6759 __ cmpl(temp, cls.AsRegister<Register>());
6760 } else {
6761 DCHECK(cls.IsStackSlot()) << cls;
6762 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6763 }
6764 // Jump to slow path for throwing the exception or doing a
6765 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006766 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006767 break;
6768 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006769
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006770 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006771 // /* HeapReference<Class> */ temp = obj->klass_
6772 GenerateReferenceLoadTwoRegisters(instruction,
6773 temp_loc,
6774 obj_loc,
6775 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006776 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006777
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006778 // If the class is abstract, we eagerly fetch the super class of the
6779 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006780 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006781 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006782 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006783 GenerateReferenceLoadOneRegister(instruction,
6784 temp_loc,
6785 super_offset,
6786 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006787 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006788
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006789 // If the class reference currently in `temp` is null, jump to the slow path to throw the
6790 // exception.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006791 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006792 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006793
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006794 // Otherwise, compare the classes
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006795 if (cls.IsRegister()) {
6796 __ cmpl(temp, cls.AsRegister<Register>());
6797 } else {
6798 DCHECK(cls.IsStackSlot()) << cls;
6799 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6800 }
6801 __ j(kNotEqual, &loop);
6802 break;
6803 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006804
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006805 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006806 // /* HeapReference<Class> */ temp = obj->klass_
6807 GenerateReferenceLoadTwoRegisters(instruction,
6808 temp_loc,
6809 obj_loc,
6810 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006811 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006812
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006813 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006814 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006815 __ Bind(&loop);
6816 if (cls.IsRegister()) {
6817 __ cmpl(temp, cls.AsRegister<Register>());
6818 } else {
6819 DCHECK(cls.IsStackSlot()) << cls;
6820 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6821 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006822 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006823
Roland Levillain0d5a2812015-11-13 10:07:31 +00006824 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006825 GenerateReferenceLoadOneRegister(instruction,
6826 temp_loc,
6827 super_offset,
6828 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006829 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006830
6831 // If the class reference currently in `temp` is not null, jump
6832 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006833 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006834 __ j(kNotZero, &loop);
6835 // Otherwise, jump to the slow path to throw the exception.;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006836 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006837 break;
6838 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006839
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006840 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006841 // /* HeapReference<Class> */ temp = obj->klass_
6842 GenerateReferenceLoadTwoRegisters(instruction,
6843 temp_loc,
6844 obj_loc,
6845 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006846 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006847
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006848 // Do an exact check.
6849 if (cls.IsRegister()) {
6850 __ cmpl(temp, cls.AsRegister<Register>());
6851 } else {
6852 DCHECK(cls.IsStackSlot()) << cls;
6853 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6854 }
6855 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006856
6857 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006858 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006859 GenerateReferenceLoadOneRegister(instruction,
6860 temp_loc,
6861 component_offset,
6862 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006863 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006864
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006865 // If the component type is null (i.e. the object not an array), jump to the slow path to
6866 // throw the exception. Otherwise proceed with the check.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006867 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006868 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006869
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006870 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006871 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006872 break;
6873 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006874
Calin Juravle98893e12015-10-02 21:05:03 +01006875 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006876 // We always go into the type check slow path for the unresolved check case.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006877 // We cannot directly call the CheckCast runtime entry point
6878 // without resorting to a type checking slow path here (i.e. by
6879 // calling InvokeRuntime directly), as it would require to
6880 // assign fixed registers for the inputs of this HInstanceOf
6881 // instruction (following the runtime calling convention), which
6882 // might be cluttered by the potential first read barrier
6883 // emission at the beginning of this method.
6884 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006885 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006886
6887 case TypeCheckKind::kInterfaceCheck: {
6888 // Fast path for the interface check. Since we compare with a memory location in the inner
6889 // loop we would need to have cls poisoned. However unpoisoning cls would reset the
6890 // conditional flags and cause the conditional jump to be incorrect. Therefore we just jump
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006891 // to the slow path if we are running under poisoning.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006892 if (!kPoisonHeapReferences) {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006893 // Try to avoid read barriers to improve the fast path. We can not get false positives by
6894 // doing this.
6895 // /* HeapReference<Class> */ temp = obj->klass_
6896 GenerateReferenceLoadTwoRegisters(instruction,
6897 temp_loc,
6898 obj_loc,
6899 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006900 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006901
6902 // /* HeapReference<Class> */ temp = temp->iftable_
6903 GenerateReferenceLoadTwoRegisters(instruction,
6904 temp_loc,
6905 temp_loc,
6906 iftable_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006907 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08006908 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006909 __ movl(maybe_temp2_loc.AsRegister<Register>(), Address(temp, array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08006910 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006911 NearLabel start_loop;
6912 __ Bind(&start_loop);
Mathieu Chartier6beced42016-11-15 15:51:31 -08006913 // Need to subtract first to handle the empty array case.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006914 __ subl(maybe_temp2_loc.AsRegister<Register>(), Immediate(2));
Mathieu Chartier6beced42016-11-15 15:51:31 -08006915 __ j(kNegative, type_check_slow_path->GetEntryLabel());
6916 // Go to next interface if the classes do not match.
6917 __ cmpl(cls.AsRegister<Register>(),
6918 CodeGeneratorX86::ArrayAddress(temp,
6919 maybe_temp2_loc,
6920 TIMES_4,
6921 object_array_data_offset));
6922 __ j(kNotEqual, &start_loop);
6923 } else {
6924 __ jmp(type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006925 }
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006926 break;
6927 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006928 }
6929 __ Bind(&done);
6930
Roland Levillain0d5a2812015-11-13 10:07:31 +00006931 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006932}
6933
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006934void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
6935 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006936 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006937 InvokeRuntimeCallingConvention calling_convention;
6938 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6939}
6940
6941void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006942 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject
6943 : kQuickUnlockObject,
Alexandre Rames8158f282015-08-07 10:26:17 +01006944 instruction,
Serban Constantinescuba45db02016-07-12 22:53:02 +01006945 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006946 if (instruction->IsEnter()) {
6947 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6948 } else {
6949 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6950 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006951}
6952
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006953void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6954void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6955void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6956
6957void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6958 LocationSummary* locations =
6959 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006960 DCHECK(instruction->GetResultType() == DataType::Type::kInt32
6961 || instruction->GetResultType() == DataType::Type::kInt64);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006962 locations->SetInAt(0, Location::RequiresRegister());
6963 locations->SetInAt(1, Location::Any());
6964 locations->SetOut(Location::SameAsFirstInput());
6965}
6966
6967void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
6968 HandleBitwiseOperation(instruction);
6969}
6970
6971void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
6972 HandleBitwiseOperation(instruction);
6973}
6974
6975void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
6976 HandleBitwiseOperation(instruction);
6977}
6978
6979void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6980 LocationSummary* locations = instruction->GetLocations();
6981 Location first = locations->InAt(0);
6982 Location second = locations->InAt(1);
6983 DCHECK(first.Equals(locations->Out()));
6984
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006985 if (instruction->GetResultType() == DataType::Type::kInt32) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006986 if (second.IsRegister()) {
6987 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006988 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006989 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006990 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006991 } else {
6992 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006993 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006994 }
6995 } else if (second.IsConstant()) {
6996 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006997 __ andl(first.AsRegister<Register>(),
6998 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006999 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00007000 __ orl(first.AsRegister<Register>(),
7001 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007002 } else {
7003 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00007004 __ xorl(first.AsRegister<Register>(),
7005 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007006 }
7007 } else {
7008 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007009 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007010 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007011 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007012 } else {
7013 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00007014 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007015 }
7016 }
7017 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007018 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007019 if (second.IsRegisterPair()) {
7020 if (instruction->IsAnd()) {
7021 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7022 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7023 } else if (instruction->IsOr()) {
7024 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7025 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7026 } else {
7027 DCHECK(instruction->IsXor());
7028 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7029 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7030 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007031 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007032 if (instruction->IsAnd()) {
7033 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7034 __ andl(first.AsRegisterPairHigh<Register>(),
7035 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7036 } else if (instruction->IsOr()) {
7037 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7038 __ orl(first.AsRegisterPairHigh<Register>(),
7039 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7040 } else {
7041 DCHECK(instruction->IsXor());
7042 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7043 __ xorl(first.AsRegisterPairHigh<Register>(),
7044 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7045 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007046 } else {
7047 DCHECK(second.IsConstant()) << second;
7048 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007049 int32_t low_value = Low32Bits(value);
7050 int32_t high_value = High32Bits(value);
7051 Immediate low(low_value);
7052 Immediate high(high_value);
7053 Register first_low = first.AsRegisterPairLow<Register>();
7054 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007055 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007056 if (low_value == 0) {
7057 __ xorl(first_low, first_low);
7058 } else if (low_value != -1) {
7059 __ andl(first_low, low);
7060 }
7061 if (high_value == 0) {
7062 __ xorl(first_high, first_high);
7063 } else if (high_value != -1) {
7064 __ andl(first_high, high);
7065 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007066 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007067 if (low_value != 0) {
7068 __ orl(first_low, low);
7069 }
7070 if (high_value != 0) {
7071 __ orl(first_high, high);
7072 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007073 } else {
7074 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007075 if (low_value != 0) {
7076 __ xorl(first_low, low);
7077 }
7078 if (high_value != 0) {
7079 __ xorl(first_high, high);
7080 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007081 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007082 }
7083 }
7084}
7085
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007086void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(
7087 HInstruction* instruction,
7088 Location out,
7089 uint32_t offset,
7090 Location maybe_temp,
7091 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007092 Register out_reg = out.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007093 if (read_barrier_option == kWithReadBarrier) {
7094 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007095 if (kUseBakerReadBarrier) {
7096 // Load with fast path based Baker's read barrier.
7097 // /* HeapReference<Object> */ out = *(out + offset)
7098 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00007099 instruction, out, out_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007100 } else {
7101 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007102 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain7c1559a2015-12-15 10:55:36 +00007103 // in the following move operation, as we will need it for the
7104 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00007105 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007106 __ movl(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007107 // /* HeapReference<Object> */ out = *(out + offset)
7108 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007109 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007110 }
7111 } else {
7112 // Plain load with no read barrier.
7113 // /* HeapReference<Object> */ out = *(out + offset)
7114 __ movl(out_reg, Address(out_reg, offset));
7115 __ MaybeUnpoisonHeapReference(out_reg);
7116 }
7117}
7118
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007119void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(
7120 HInstruction* instruction,
7121 Location out,
7122 Location obj,
7123 uint32_t offset,
7124 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007125 Register out_reg = out.AsRegister<Register>();
7126 Register obj_reg = obj.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007127 if (read_barrier_option == kWithReadBarrier) {
7128 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007129 if (kUseBakerReadBarrier) {
7130 // Load with fast path based Baker's read barrier.
7131 // /* HeapReference<Object> */ out = *(obj + offset)
7132 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00007133 instruction, out, obj_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007134 } else {
7135 // Load with slow path based read barrier.
7136 // /* HeapReference<Object> */ out = *(obj + offset)
7137 __ movl(out_reg, Address(obj_reg, offset));
7138 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
7139 }
7140 } else {
7141 // Plain load with no read barrier.
7142 // /* HeapReference<Object> */ out = *(obj + offset)
7143 __ movl(out_reg, Address(obj_reg, offset));
7144 __ MaybeUnpoisonHeapReference(out_reg);
7145 }
7146}
7147
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007148void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(
7149 HInstruction* instruction,
7150 Location root,
7151 const Address& address,
7152 Label* fixup_label,
7153 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007154 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007155 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07007156 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007157 if (kUseBakerReadBarrier) {
7158 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
7159 // Baker's read barrier are used:
7160 //
Roland Levillaind966ce72017-02-09 16:20:14 +00007161 // root = obj.field;
7162 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
7163 // if (temp != null) {
7164 // root = temp(root)
Roland Levillain7c1559a2015-12-15 10:55:36 +00007165 // }
7166
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007167 // /* GcRoot<mirror::Object> */ root = *address
7168 __ movl(root_reg, address);
7169 if (fixup_label != nullptr) {
7170 __ Bind(fixup_label);
7171 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007172 static_assert(
7173 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
7174 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
7175 "have different sizes.");
7176 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
7177 "art::mirror::CompressedReference<mirror::Object> and int32_t "
7178 "have different sizes.");
7179
Vladimir Marko953437b2016-08-24 08:30:46 +00007180 // Slow path marking the GC root `root`.
7181 SlowPathCode* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007182 instruction, root, /* unpoison_ref_before_marking */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007183 codegen_->AddSlowPath(slow_path);
7184
Roland Levillaind966ce72017-02-09 16:20:14 +00007185 // Test the entrypoint (`Thread::Current()->pReadBarrierMarkReg ## root.reg()`).
7186 const int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +01007187 Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(root.reg());
Roland Levillaind966ce72017-02-09 16:20:14 +00007188 __ fs()->cmpl(Address::Absolute(entry_point_offset), Immediate(0));
7189 // The entrypoint is null when the GC is not marking.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007190 __ j(kNotEqual, slow_path->GetEntryLabel());
7191 __ Bind(slow_path->GetExitLabel());
7192 } else {
7193 // GC root loaded through a slow path for read barriers other
7194 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007195 // /* GcRoot<mirror::Object>* */ root = address
7196 __ leal(root_reg, address);
7197 if (fixup_label != nullptr) {
7198 __ Bind(fixup_label);
7199 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007200 // /* mirror::Object* */ root = root->Read()
7201 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
7202 }
7203 } else {
7204 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007205 // /* GcRoot<mirror::Object> */ root = *address
7206 __ movl(root_reg, address);
7207 if (fixup_label != nullptr) {
7208 __ Bind(fixup_label);
7209 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007210 // Note that GC roots are not affected by heap poisoning, thus we
7211 // do not have to unpoison `root_reg` here.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007212 }
7213}
7214
7215void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
7216 Location ref,
7217 Register obj,
7218 uint32_t offset,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007219 bool needs_null_check) {
7220 DCHECK(kEmitCompilerReadBarrier);
7221 DCHECK(kUseBakerReadBarrier);
7222
7223 // /* HeapReference<Object> */ ref = *(obj + offset)
7224 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007225 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007226}
7227
7228void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
7229 Location ref,
7230 Register obj,
7231 uint32_t data_offset,
7232 Location index,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007233 bool needs_null_check) {
7234 DCHECK(kEmitCompilerReadBarrier);
7235 DCHECK(kUseBakerReadBarrier);
7236
Roland Levillain3d312422016-06-23 13:53:42 +01007237 static_assert(
7238 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
7239 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00007240 // /* HeapReference<Object> */ ref =
7241 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007242 Address src = CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007243 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007244}
7245
7246void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
7247 Location ref,
7248 Register obj,
7249 const Address& src,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007250 bool needs_null_check,
7251 bool always_update_field,
7252 Register* temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007253 DCHECK(kEmitCompilerReadBarrier);
7254 DCHECK(kUseBakerReadBarrier);
7255
7256 // In slow path based read barriers, the read barrier call is
7257 // inserted after the original load. However, in fast path based
7258 // Baker's read barriers, we need to perform the load of
7259 // mirror::Object::monitor_ *before* the original reference load.
7260 // This load-load ordering is required by the read barrier.
7261 // The fast path/slow path (for Baker's algorithm) should look like:
7262 //
7263 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7264 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7265 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007266 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain7c1559a2015-12-15 10:55:36 +00007267 // if (is_gray) {
7268 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7269 // }
7270 //
7271 // Note: the original implementation in ReadBarrier::Barrier is
7272 // slightly more complex as:
7273 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007274 // the high-bits of rb_state, which are expected to be all zeroes
7275 // (we use CodeGeneratorX86::GenerateMemoryBarrier instead here,
7276 // which is a no-op thanks to the x86 memory model);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007277 // - it performs additional checks that we do not do here for
7278 // performance reasons.
7279
7280 Register ref_reg = ref.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +00007281 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7282
Vladimir Marko953437b2016-08-24 08:30:46 +00007283 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007284 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
7285 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko953437b2016-08-24 08:30:46 +00007286 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
7287 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
7288 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
7289
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007290 // if (rb_state == ReadBarrier::GrayState())
Vladimir Marko953437b2016-08-24 08:30:46 +00007291 // ref = ReadBarrier::Mark(ref);
7292 // At this point, just do the "if" and make sure that flags are preserved until the branch.
7293 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain7c1559a2015-12-15 10:55:36 +00007294 if (needs_null_check) {
7295 MaybeRecordImplicitNullCheck(instruction);
7296 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007297
7298 // Load fence to prevent load-load reordering.
7299 // Note that this is a no-op, thanks to the x86 memory model.
7300 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
7301
7302 // The actual reference load.
7303 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00007304 __ movl(ref_reg, src); // Flags are unaffected.
7305
7306 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
7307 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007308 SlowPathCode* slow_path;
7309 if (always_update_field) {
7310 DCHECK(temp != nullptr);
7311 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkAndUpdateFieldSlowPathX86(
7312 instruction, ref, obj, src, /* unpoison_ref_before_marking */ true, *temp);
7313 } else {
7314 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(
7315 instruction, ref, /* unpoison_ref_before_marking */ true);
7316 }
Vladimir Marko953437b2016-08-24 08:30:46 +00007317 AddSlowPath(slow_path);
7318
7319 // We have done the "if" of the gray bit check above, now branch based on the flags.
7320 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain7c1559a2015-12-15 10:55:36 +00007321
7322 // Object* ref = ref_addr->AsMirrorPtr()
7323 __ MaybeUnpoisonHeapReference(ref_reg);
7324
Roland Levillain7c1559a2015-12-15 10:55:36 +00007325 __ Bind(slow_path->GetExitLabel());
7326}
7327
7328void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
7329 Location out,
7330 Location ref,
7331 Location obj,
7332 uint32_t offset,
7333 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007334 DCHECK(kEmitCompilerReadBarrier);
7335
Roland Levillain7c1559a2015-12-15 10:55:36 +00007336 // Insert a slow path based read barrier *after* the reference load.
7337 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007338 // If heap poisoning is enabled, the unpoisoning of the loaded
7339 // reference will be carried out by the runtime within the slow
7340 // path.
7341 //
7342 // Note that `ref` currently does not get unpoisoned (when heap
7343 // poisoning is enabled), which is alright as the `ref` argument is
7344 // not used by the artReadBarrierSlow entry point.
7345 //
7346 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
7347 SlowPathCode* slow_path = new (GetGraph()->GetArena())
7348 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
7349 AddSlowPath(slow_path);
7350
Roland Levillain0d5a2812015-11-13 10:07:31 +00007351 __ jmp(slow_path->GetEntryLabel());
7352 __ Bind(slow_path->GetExitLabel());
7353}
7354
Roland Levillain7c1559a2015-12-15 10:55:36 +00007355void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7356 Location out,
7357 Location ref,
7358 Location obj,
7359 uint32_t offset,
7360 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007361 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007362 // Baker's read barriers shall be handled by the fast path
7363 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
7364 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007365 // If heap poisoning is enabled, unpoisoning will be taken care of
7366 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007367 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007368 } else if (kPoisonHeapReferences) {
7369 __ UnpoisonHeapReference(out.AsRegister<Register>());
7370 }
7371}
7372
Roland Levillain7c1559a2015-12-15 10:55:36 +00007373void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7374 Location out,
7375 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007376 DCHECK(kEmitCompilerReadBarrier);
7377
Roland Levillain7c1559a2015-12-15 10:55:36 +00007378 // Insert a slow path based read barrier *after* the GC root load.
7379 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007380 // Note that GC roots are not affected by heap poisoning, so we do
7381 // not need to do anything special for this here.
7382 SlowPathCode* slow_path =
7383 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86(instruction, out, root);
7384 AddSlowPath(slow_path);
7385
Roland Levillain0d5a2812015-11-13 10:07:31 +00007386 __ jmp(slow_path->GetEntryLabel());
7387 __ Bind(slow_path->GetExitLabel());
7388}
7389
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007390void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007391 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007392 LOG(FATAL) << "Unreachable";
7393}
7394
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007395void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007396 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007397 LOG(FATAL) << "Unreachable";
7398}
7399
Mark Mendellfe57faa2015-09-18 09:26:15 -04007400// Simple implementation of packed switch - generate cascaded compare/jumps.
7401void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7402 LocationSummary* locations =
7403 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7404 locations->SetInAt(0, Location::RequiresRegister());
7405}
7406
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007407void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
7408 int32_t lower_bound,
7409 uint32_t num_entries,
7410 HBasicBlock* switch_block,
7411 HBasicBlock* default_block) {
7412 // Figure out the correct compare values and jump conditions.
7413 // Handle the first compare/branch as a special case because it might
7414 // jump to the default case.
7415 DCHECK_GT(num_entries, 2u);
7416 Condition first_condition;
7417 uint32_t index;
7418 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
7419 if (lower_bound != 0) {
7420 first_condition = kLess;
7421 __ cmpl(value_reg, Immediate(lower_bound));
7422 __ j(first_condition, codegen_->GetLabelOf(default_block));
7423 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007424
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007425 index = 1;
7426 } else {
7427 // Handle all the compare/jumps below.
7428 first_condition = kBelow;
7429 index = 0;
7430 }
7431
7432 // Handle the rest of the compare/jumps.
7433 for (; index + 1 < num_entries; index += 2) {
7434 int32_t compare_to_value = lower_bound + index + 1;
7435 __ cmpl(value_reg, Immediate(compare_to_value));
7436 // Jump to successors[index] if value < case_value[index].
7437 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
7438 // Jump to successors[index + 1] if value == case_value[index + 1].
7439 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
7440 }
7441
7442 if (index != num_entries) {
7443 // There are an odd number of entries. Handle the last one.
7444 DCHECK_EQ(index + 1, num_entries);
7445 __ cmpl(value_reg, Immediate(lower_bound + index));
7446 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007447 }
7448
7449 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007450 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
7451 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007452 }
7453}
7454
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007455void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7456 int32_t lower_bound = switch_instr->GetStartValue();
7457 uint32_t num_entries = switch_instr->GetNumEntries();
7458 LocationSummary* locations = switch_instr->GetLocations();
7459 Register value_reg = locations->InAt(0).AsRegister<Register>();
7460
7461 GenPackedSwitchWithCompares(value_reg,
7462 lower_bound,
7463 num_entries,
7464 switch_instr->GetBlock(),
7465 switch_instr->GetDefaultBlock());
7466}
7467
Mark Mendell805b3b52015-09-18 14:10:29 -04007468void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7469 LocationSummary* locations =
7470 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7471 locations->SetInAt(0, Location::RequiresRegister());
7472
7473 // Constant area pointer.
7474 locations->SetInAt(1, Location::RequiresRegister());
7475
7476 // And the temporary we need.
7477 locations->AddTemp(Location::RequiresRegister());
7478}
7479
7480void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7481 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007482 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04007483 LocationSummary* locations = switch_instr->GetLocations();
7484 Register value_reg = locations->InAt(0).AsRegister<Register>();
7485 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7486
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007487 if (num_entries <= kPackedSwitchJumpTableThreshold) {
7488 GenPackedSwitchWithCompares(value_reg,
7489 lower_bound,
7490 num_entries,
7491 switch_instr->GetBlock(),
7492 default_block);
7493 return;
7494 }
7495
Mark Mendell805b3b52015-09-18 14:10:29 -04007496 // Optimizing has a jump area.
7497 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7498 Register constant_area = locations->InAt(1).AsRegister<Register>();
7499
7500 // Remove the bias, if needed.
7501 if (lower_bound != 0) {
7502 __ leal(temp_reg, Address(value_reg, -lower_bound));
7503 value_reg = temp_reg;
7504 }
7505
7506 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007507 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04007508 __ cmpl(value_reg, Immediate(num_entries - 1));
7509 __ j(kAbove, codegen_->GetLabelOf(default_block));
7510
7511 // We are in the range of the table.
7512 // Load (target-constant_area) from the jump table, indexing by the value.
7513 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
7514
7515 // Compute the actual target address by adding in constant_area.
7516 __ addl(temp_reg, constant_area);
7517
7518 // And jump.
7519 __ jmp(temp_reg);
7520}
7521
Mark Mendell0616ae02015-04-17 12:49:27 -04007522void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
7523 HX86ComputeBaseMethodAddress* insn) {
7524 LocationSummary* locations =
7525 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7526 locations->SetOut(Location::RequiresRegister());
7527}
7528
7529void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
7530 HX86ComputeBaseMethodAddress* insn) {
7531 LocationSummary* locations = insn->GetLocations();
7532 Register reg = locations->Out().AsRegister<Register>();
7533
7534 // Generate call to next instruction.
7535 Label next_instruction;
7536 __ call(&next_instruction);
7537 __ Bind(&next_instruction);
7538
7539 // Remember this offset for later use with constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007540 codegen_->AddMethodAddressOffset(insn, GetAssembler()->CodeSize());
Mark Mendell0616ae02015-04-17 12:49:27 -04007541
7542 // Grab the return address off the stack.
7543 __ popl(reg);
7544}
7545
7546void LocationsBuilderX86::VisitX86LoadFromConstantTable(
7547 HX86LoadFromConstantTable* insn) {
7548 LocationSummary* locations =
7549 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7550
7551 locations->SetInAt(0, Location::RequiresRegister());
7552 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
7553
7554 // If we don't need to be materialized, we only need the inputs to be set.
David Brazdilb3e773e2016-01-26 11:28:37 +00007555 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007556 return;
7557 }
7558
7559 switch (insn->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007560 case DataType::Type::kFloat32:
7561 case DataType::Type::kFloat64:
Mark Mendell0616ae02015-04-17 12:49:27 -04007562 locations->SetOut(Location::RequiresFpuRegister());
7563 break;
7564
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007565 case DataType::Type::kInt32:
Mark Mendell0616ae02015-04-17 12:49:27 -04007566 locations->SetOut(Location::RequiresRegister());
7567 break;
7568
7569 default:
7570 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7571 }
7572}
7573
7574void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
David Brazdilb3e773e2016-01-26 11:28:37 +00007575 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007576 return;
7577 }
7578
7579 LocationSummary* locations = insn->GetLocations();
7580 Location out = locations->Out();
7581 Register const_area = locations->InAt(0).AsRegister<Register>();
7582 HConstant *value = insn->GetConstant();
7583
7584 switch (insn->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007585 case DataType::Type::kFloat32:
Mark Mendell0616ae02015-04-17 12:49:27 -04007586 __ movss(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007587 codegen_->LiteralFloatAddress(
7588 value->AsFloatConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04007589 break;
7590
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007591 case DataType::Type::kFloat64:
Mark Mendell0616ae02015-04-17 12:49:27 -04007592 __ movsd(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007593 codegen_->LiteralDoubleAddress(
7594 value->AsDoubleConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04007595 break;
7596
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007597 case DataType::Type::kInt32:
Mark Mendell0616ae02015-04-17 12:49:27 -04007598 __ movl(out.AsRegister<Register>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007599 codegen_->LiteralInt32Address(
7600 value->AsIntConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04007601 break;
7602
7603 default:
7604 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7605 }
7606}
7607
Mark Mendell0616ae02015-04-17 12:49:27 -04007608/**
7609 * Class to handle late fixup of offsets into constant area.
7610 */
Vladimir Marko5233f932015-09-29 19:01:15 +01007611class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04007612 public:
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007613 RIPFixup(CodeGeneratorX86& codegen,
7614 HX86ComputeBaseMethodAddress* base_method_address,
7615 size_t offset)
7616 : codegen_(&codegen),
7617 base_method_address_(base_method_address),
7618 offset_into_constant_area_(offset) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04007619
7620 protected:
7621 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
7622
7623 CodeGeneratorX86* codegen_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007624 HX86ComputeBaseMethodAddress* base_method_address_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007625
7626 private:
7627 void Process(const MemoryRegion& region, int pos) OVERRIDE {
7628 // Patch the correct offset for the instruction. The place to patch is the
7629 // last 4 bytes of the instruction.
7630 // The value to patch is the distance from the offset in the constant area
7631 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04007632 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007633 int32_t relative_position =
7634 constant_offset - codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell0616ae02015-04-17 12:49:27 -04007635
7636 // Patch in the right value.
7637 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
7638 }
7639
Mark Mendell0616ae02015-04-17 12:49:27 -04007640 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04007641 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007642};
7643
Mark Mendell805b3b52015-09-18 14:10:29 -04007644/**
7645 * Class to handle late fixup of offsets to a jump table that will be created in the
7646 * constant area.
7647 */
7648class JumpTableRIPFixup : public RIPFixup {
7649 public:
7650 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007651 : RIPFixup(codegen, switch_instr->GetBaseMethodAddress(), static_cast<size_t>(-1)),
7652 switch_instr_(switch_instr) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04007653
7654 void CreateJumpTable() {
7655 X86Assembler* assembler = codegen_->GetAssembler();
7656
7657 // Ensure that the reference to the jump table has the correct offset.
7658 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
7659 SetOffset(offset_in_constant_table);
7660
7661 // The label values in the jump table are computed relative to the
7662 // instruction addressing the constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007663 const int32_t relative_offset = codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell805b3b52015-09-18 14:10:29 -04007664
7665 // Populate the jump table with the correct values for the jump table.
7666 int32_t num_entries = switch_instr_->GetNumEntries();
7667 HBasicBlock* block = switch_instr_->GetBlock();
7668 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
7669 // The value that we want is the target offset - the position of the table.
7670 for (int32_t i = 0; i < num_entries; i++) {
7671 HBasicBlock* b = successors[i];
7672 Label* l = codegen_->GetLabelOf(b);
7673 DCHECK(l->IsBound());
7674 int32_t offset_to_block = l->Position() - relative_offset;
7675 assembler->AppendInt32(offset_to_block);
7676 }
7677 }
7678
7679 private:
7680 const HX86PackedSwitch* switch_instr_;
7681};
7682
7683void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
7684 // Generate the constant area if needed.
7685 X86Assembler* assembler = GetAssembler();
7686 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
7687 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
7688 // byte values.
7689 assembler->Align(4, 0);
7690 constant_area_start_ = assembler->CodeSize();
7691
7692 // Populate any jump tables.
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007693 for (JumpTableRIPFixup* jump_table : fixups_to_jump_tables_) {
Mark Mendell805b3b52015-09-18 14:10:29 -04007694 jump_table->CreateJumpTable();
7695 }
7696
7697 // And now add the constant area to the generated code.
7698 assembler->AddConstantArea();
7699 }
7700
7701 // And finish up.
7702 CodeGenerator::Finalize(allocator);
7703}
7704
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007705Address CodeGeneratorX86::LiteralDoubleAddress(double v,
7706 HX86ComputeBaseMethodAddress* method_base,
7707 Register reg) {
7708 AssemblerFixup* fixup =
7709 new (GetGraph()->GetArena()) RIPFixup(*this, method_base, __ AddDouble(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04007710 return Address(reg, kDummy32BitOffset, fixup);
7711}
7712
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007713Address CodeGeneratorX86::LiteralFloatAddress(float v,
7714 HX86ComputeBaseMethodAddress* method_base,
7715 Register reg) {
7716 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, method_base, __ AddFloat(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04007717 return Address(reg, kDummy32BitOffset, fixup);
7718}
7719
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007720Address CodeGeneratorX86::LiteralInt32Address(int32_t v,
7721 HX86ComputeBaseMethodAddress* method_base,
7722 Register reg) {
7723 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, method_base, __ AddInt32(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04007724 return Address(reg, kDummy32BitOffset, fixup);
7725}
7726
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007727Address CodeGeneratorX86::LiteralInt64Address(int64_t v,
7728 HX86ComputeBaseMethodAddress* method_base,
7729 Register reg) {
7730 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, method_base, __ AddInt64(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04007731 return Address(reg, kDummy32BitOffset, fixup);
7732}
7733
Aart Bika19616e2016-02-01 18:57:58 -08007734void CodeGeneratorX86::Load32BitValue(Register dest, int32_t value) {
7735 if (value == 0) {
7736 __ xorl(dest, dest);
7737 } else {
7738 __ movl(dest, Immediate(value));
7739 }
7740}
7741
7742void CodeGeneratorX86::Compare32BitValue(Register dest, int32_t value) {
7743 if (value == 0) {
7744 __ testl(dest, dest);
7745 } else {
7746 __ cmpl(dest, Immediate(value));
7747 }
7748}
7749
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007750void CodeGeneratorX86::GenerateIntCompare(Location lhs, Location rhs) {
7751 Register lhs_reg = lhs.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07007752 GenerateIntCompare(lhs_reg, rhs);
7753}
7754
7755void CodeGeneratorX86::GenerateIntCompare(Register lhs, Location rhs) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007756 if (rhs.IsConstant()) {
7757 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
jessicahandojo4877b792016-09-08 19:49:13 -07007758 Compare32BitValue(lhs, value);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007759 } else if (rhs.IsStackSlot()) {
jessicahandojo4877b792016-09-08 19:49:13 -07007760 __ cmpl(lhs, Address(ESP, rhs.GetStackIndex()));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007761 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07007762 __ cmpl(lhs, rhs.AsRegister<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007763 }
7764}
7765
7766Address CodeGeneratorX86::ArrayAddress(Register obj,
7767 Location index,
7768 ScaleFactor scale,
7769 uint32_t data_offset) {
7770 return index.IsConstant() ?
7771 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << scale) + data_offset) :
7772 Address(obj, index.AsRegister<Register>(), scale, data_offset);
7773}
7774
Mark Mendell805b3b52015-09-18 14:10:29 -04007775Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
7776 Register reg,
7777 Register value) {
7778 // Create a fixup to be used to create and address the jump table.
7779 JumpTableRIPFixup* table_fixup =
7780 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
7781
7782 // We have to populate the jump tables.
7783 fixups_to_jump_tables_.push_back(table_fixup);
7784
7785 // We want a scaled address, as we are extracting the correct offset from the table.
7786 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
7787}
7788
Andreas Gampe85b62f22015-09-09 13:15:38 -07007789// TODO: target as memory.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007790void CodeGeneratorX86::MoveFromReturnRegister(Location target, DataType::Type type) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07007791 if (!target.IsValid()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007792 DCHECK_EQ(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007793 return;
7794 }
7795
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007796 DCHECK_NE(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007797
7798 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
7799 if (target.Equals(return_loc)) {
7800 return;
7801 }
7802
7803 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7804 // with the else branch.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007805 if (type == DataType::Type::kInt64) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07007806 HParallelMove parallel_move(GetGraph()->GetArena());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007807 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), DataType::Type::kInt32, nullptr);
7808 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), DataType::Type::kInt32, nullptr);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007809 GetMoveResolver()->EmitNativeCode(&parallel_move);
7810 } else {
7811 // Let the parallel move resolver take care of all of this.
7812 HParallelMove parallel_move(GetGraph()->GetArena());
7813 parallel_move.AddMove(return_loc, target, type, nullptr);
7814 GetMoveResolver()->EmitNativeCode(&parallel_move);
7815 }
7816}
7817
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007818void CodeGeneratorX86::PatchJitRootUse(uint8_t* code,
7819 const uint8_t* roots_data,
7820 const PatchInfo<Label>& info,
7821 uint64_t index_in_table) const {
7822 uint32_t code_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
7823 uintptr_t address =
7824 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
7825 typedef __attribute__((__aligned__(1))) uint32_t unaligned_uint32_t;
7826 reinterpret_cast<unaligned_uint32_t*>(code + code_offset)[0] =
7827 dchecked_integral_cast<uint32_t>(address);
7828}
7829
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007830void CodeGeneratorX86::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
7831 for (const PatchInfo<Label>& info : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007832 const auto it = jit_string_roots_.find(
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007833 StringReference(&info.dex_file, dex::StringIndex(info.index)));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007834 DCHECK(it != jit_string_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007835 uint64_t index_in_table = it->second;
7836 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007837 }
7838
7839 for (const PatchInfo<Label>& info : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007840 const auto it = jit_class_roots_.find(
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007841 TypeReference(&info.dex_file, dex::TypeIndex(info.index)));
7842 DCHECK(it != jit_class_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007843 uint64_t index_in_table = it->second;
7844 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007845 }
7846}
7847
Roland Levillain4d027112015-07-01 15:41:14 +01007848#undef __
7849
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007850} // namespace x86
7851} // namespace art