blob: 35156491e8e89d6e01ddd2daa4e60dff9d7119a4 [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 Markod5d2f2c2017-09-26 12:37:26 +01001134 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001135 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001136 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001137 case DataType::Type::kInt8:
1138 case DataType::Type::kUint16:
1139 case DataType::Type::kInt16:
1140 case DataType::Type::kInt32:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001141 return Location::RegisterLocation(EAX);
1142
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001143 case DataType::Type::kInt64:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001144 return Location::RegisterPairLocation(EAX, EDX);
1145
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001146 case DataType::Type::kVoid:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001147 return Location::NoLocation();
1148
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001149 case DataType::Type::kFloat64:
1150 case DataType::Type::kFloat32:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001151 return Location::FpuRegisterLocation(XMM0);
1152 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001153
1154 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001155}
1156
1157Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
1158 return Location::RegisterLocation(kMethodRegisterArgument);
1159}
1160
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001161Location InvokeDexCallingConventionVisitorX86::GetNextLocation(DataType::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001162 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001163 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001164 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001165 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001166 case DataType::Type::kInt8:
1167 case DataType::Type::kUint16:
1168 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001169 case DataType::Type::kInt32: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001170 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001171 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001172 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001173 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001174 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001175 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001176 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001177 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001178
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001179 case DataType::Type::kInt64: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001180 uint32_t index = gp_index_;
1181 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001182 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001183 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001184 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
1185 calling_convention.GetRegisterPairAt(index));
1186 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001187 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001188 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1189 }
1190 }
1191
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001192 case DataType::Type::kFloat32: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001193 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001194 stack_index_++;
1195 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1196 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1197 } else {
1198 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1199 }
1200 }
1201
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001202 case DataType::Type::kFloat64: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001203 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001204 stack_index_ += 2;
1205 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1206 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1207 } else {
1208 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001209 }
1210 }
1211
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001212 case DataType::Type::kVoid:
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001213 LOG(FATAL) << "Unexpected parameter type " << type;
1214 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001215 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00001216 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001217}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001218
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001219void CodeGeneratorX86::Move32(Location destination, Location source) {
1220 if (source.Equals(destination)) {
1221 return;
1222 }
1223 if (destination.IsRegister()) {
1224 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001225 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001226 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001227 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001228 } else {
1229 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001230 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001231 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001232 } else if (destination.IsFpuRegister()) {
1233 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001234 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001235 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001236 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001237 } else {
1238 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001239 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001240 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001241 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001242 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001243 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001244 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001245 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001246 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05001247 } else if (source.IsConstant()) {
1248 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001249 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05001250 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001251 } else {
1252 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001253 __ pushl(Address(ESP, source.GetStackIndex()));
1254 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001255 }
1256 }
1257}
1258
1259void CodeGeneratorX86::Move64(Location destination, Location source) {
1260 if (source.Equals(destination)) {
1261 return;
1262 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001263 if (destination.IsRegisterPair()) {
1264 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001265 EmitParallelMoves(
1266 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1267 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001268 DataType::Type::kInt32,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001269 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001270 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001271 DataType::Type::kInt32);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001272 } else if (source.IsFpuRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001273 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
1274 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
1275 __ psrlq(src_reg, Immediate(32));
1276 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001277 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001278 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001279 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001280 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
1281 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001282 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
1283 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001284 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05001285 if (source.IsFpuRegister()) {
1286 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
1287 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001288 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001289 } else if (source.IsRegisterPair()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001290 size_t elem_size = DataType::Size(DataType::Type::kInt32);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001291 // Create stack space for 2 elements.
1292 __ subl(ESP, Immediate(2 * elem_size));
1293 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
1294 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
1295 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
1296 // And remove the temporary stack space we allocated.
1297 __ addl(ESP, Immediate(2 * elem_size));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001298 } else {
1299 LOG(FATAL) << "Unimplemented";
1300 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001301 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001302 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001303 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001304 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001305 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001306 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001307 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001308 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001309 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001310 } else if (source.IsConstant()) {
1311 HConstant* constant = source.GetConstant();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001312 DCHECK(constant->IsLongConstant() || constant->IsDoubleConstant());
1313 int64_t value = GetInt64ValueOf(constant);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001314 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001315 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
1316 Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001317 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001318 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001319 EmitParallelMoves(
1320 Location::StackSlot(source.GetStackIndex()),
1321 Location::StackSlot(destination.GetStackIndex()),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001322 DataType::Type::kInt32,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001323 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001324 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001325 DataType::Type::kInt32);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001326 }
1327 }
1328}
1329
Calin Juravle175dc732015-08-25 15:42:32 +01001330void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
1331 DCHECK(location.IsRegister());
1332 __ movl(location.AsRegister<Register>(), Immediate(value));
1333}
1334
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001335void CodeGeneratorX86::MoveLocation(Location dst, Location src, DataType::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001336 HParallelMove move(GetGraph()->GetArena());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001337 if (dst_type == DataType::Type::kInt64 && !src.IsConstant() && !src.IsFpuRegister()) {
1338 move.AddMove(src.ToLow(), dst.ToLow(), DataType::Type::kInt32, nullptr);
1339 move.AddMove(src.ToHigh(), dst.ToHigh(), DataType::Type::kInt32, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001340 } else {
David Brazdil74eb1b22015-12-14 11:44:01 +00001341 move.AddMove(src, dst, dst_type, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001342 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001343 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001344}
1345
1346void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1347 if (location.IsRegister()) {
1348 locations->AddTemp(location);
1349 } else if (location.IsRegisterPair()) {
1350 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1351 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1352 } else {
1353 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1354 }
1355}
1356
David Brazdilfc6a86a2015-06-26 10:33:45 +00001357void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001358 DCHECK(!successor->IsExitBlock());
1359
1360 HBasicBlock* block = got->GetBlock();
1361 HInstruction* previous = got->GetPrevious();
1362
1363 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001364 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001365 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1366 return;
1367 }
1368
1369 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1370 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1371 }
1372 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001373 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001374 }
1375}
1376
David Brazdilfc6a86a2015-06-26 10:33:45 +00001377void LocationsBuilderX86::VisitGoto(HGoto* got) {
1378 got->SetLocations(nullptr);
1379}
1380
1381void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1382 HandleGoto(got, got->GetSuccessor());
1383}
1384
1385void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1386 try_boundary->SetLocations(nullptr);
1387}
1388
1389void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1390 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1391 if (!successor->IsExitBlock()) {
1392 HandleGoto(try_boundary, successor);
1393 }
1394}
1395
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001396void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001397 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001398}
1399
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001400void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001401}
1402
Mark Mendell152408f2015-12-31 12:28:50 -05001403template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001404void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001405 LabelType* true_label,
1406 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001407 if (cond->IsFPConditionTrueIfNaN()) {
1408 __ j(kUnordered, true_label);
1409 } else if (cond->IsFPConditionFalseIfNaN()) {
1410 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001411 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001412 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001413}
1414
Mark Mendell152408f2015-12-31 12:28:50 -05001415template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001416void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001417 LabelType* true_label,
1418 LabelType* false_label) {
Mark Mendellc4701932015-04-10 13:18:51 -04001419 LocationSummary* locations = cond->GetLocations();
1420 Location left = locations->InAt(0);
1421 Location right = locations->InAt(1);
1422 IfCondition if_cond = cond->GetCondition();
1423
Mark Mendellc4701932015-04-10 13:18:51 -04001424 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001425 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001426 IfCondition true_high_cond = if_cond;
1427 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001428 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001429
1430 // Set the conditions for the test, remembering that == needs to be
1431 // decided using the low words.
1432 switch (if_cond) {
1433 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001434 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001435 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001436 break;
1437 case kCondLT:
1438 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001439 break;
1440 case kCondLE:
1441 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001442 break;
1443 case kCondGT:
1444 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001445 break;
1446 case kCondGE:
1447 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001448 break;
Aart Bike9f37602015-10-09 11:15:55 -07001449 case kCondB:
1450 false_high_cond = kCondA;
1451 break;
1452 case kCondBE:
1453 true_high_cond = kCondB;
1454 break;
1455 case kCondA:
1456 false_high_cond = kCondB;
1457 break;
1458 case kCondAE:
1459 true_high_cond = kCondA;
1460 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001461 }
1462
1463 if (right.IsConstant()) {
1464 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001465 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001466 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001467
Aart Bika19616e2016-02-01 18:57:58 -08001468 codegen_->Compare32BitValue(left_high, val_high);
Mark Mendellc4701932015-04-10 13:18:51 -04001469 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001470 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001471 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001472 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001473 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001474 __ j(X86Condition(true_high_cond), true_label);
1475 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001476 }
1477 // Must be equal high, so compare the lows.
Aart Bika19616e2016-02-01 18:57:58 -08001478 codegen_->Compare32BitValue(left_low, val_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001479 } else if (right.IsRegisterPair()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001480 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001481 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001482
1483 __ cmpl(left_high, right_high);
1484 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001485 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001486 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001487 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001488 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001489 __ j(X86Condition(true_high_cond), true_label);
1490 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001491 }
1492 // Must be equal high, so compare the lows.
1493 __ cmpl(left_low, right_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001494 } else {
1495 DCHECK(right.IsDoubleStackSlot());
1496 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
1497 if (if_cond == kCondNE) {
1498 __ j(X86Condition(true_high_cond), true_label);
1499 } else if (if_cond == kCondEQ) {
1500 __ j(X86Condition(false_high_cond), false_label);
1501 } else {
1502 __ j(X86Condition(true_high_cond), true_label);
1503 __ j(X86Condition(false_high_cond), false_label);
1504 }
1505 // Must be equal high, so compare the lows.
1506 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Mark Mendellc4701932015-04-10 13:18:51 -04001507 }
1508 // The last comparison might be unsigned.
1509 __ j(final_condition, true_label);
1510}
1511
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001512void InstructionCodeGeneratorX86::GenerateFPCompare(Location lhs,
1513 Location rhs,
1514 HInstruction* insn,
1515 bool is_double) {
1516 HX86LoadFromConstantTable* const_area = insn->InputAt(1)->AsX86LoadFromConstantTable();
1517 if (is_double) {
1518 if (rhs.IsFpuRegister()) {
1519 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1520 } else if (const_area != nullptr) {
1521 DCHECK(const_area->IsEmittedAtUseSite());
1522 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(),
1523 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001524 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
1525 const_area->GetBaseMethodAddress(),
1526 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001527 } else {
1528 DCHECK(rhs.IsDoubleStackSlot());
1529 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1530 }
1531 } else {
1532 if (rhs.IsFpuRegister()) {
1533 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1534 } else if (const_area != nullptr) {
1535 DCHECK(const_area->IsEmittedAtUseSite());
1536 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(),
1537 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001538 const_area->GetConstant()->AsFloatConstant()->GetValue(),
1539 const_area->GetBaseMethodAddress(),
1540 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001541 } else {
1542 DCHECK(rhs.IsStackSlot());
1543 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1544 }
1545 }
1546}
1547
Mark Mendell152408f2015-12-31 12:28:50 -05001548template<class LabelType>
David Brazdil0debae72015-11-12 18:37:00 +00001549void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendell152408f2015-12-31 12:28:50 -05001550 LabelType* true_target_in,
1551 LabelType* false_target_in) {
David Brazdil0debae72015-11-12 18:37:00 +00001552 // Generated branching requires both targets to be explicit. If either of the
1553 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Mark Mendell152408f2015-12-31 12:28:50 -05001554 LabelType fallthrough_target;
1555 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1556 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
David Brazdil0debae72015-11-12 18:37:00 +00001557
Mark Mendellc4701932015-04-10 13:18:51 -04001558 LocationSummary* locations = condition->GetLocations();
1559 Location left = locations->InAt(0);
1560 Location right = locations->InAt(1);
1561
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001562 DataType::Type type = condition->InputAt(0)->GetType();
Mark Mendellc4701932015-04-10 13:18:51 -04001563 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001564 case DataType::Type::kInt64:
Mark Mendellc4701932015-04-10 13:18:51 -04001565 GenerateLongComparesAndJumps(condition, true_target, false_target);
1566 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001567 case DataType::Type::kFloat32:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001568 GenerateFPCompare(left, right, condition, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001569 GenerateFPJumps(condition, true_target, false_target);
1570 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001571 case DataType::Type::kFloat64:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001572 GenerateFPCompare(left, right, condition, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001573 GenerateFPJumps(condition, true_target, false_target);
1574 break;
1575 default:
1576 LOG(FATAL) << "Unexpected compare type " << type;
1577 }
1578
David Brazdil0debae72015-11-12 18:37:00 +00001579 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001580 __ jmp(false_target);
1581 }
David Brazdil0debae72015-11-12 18:37:00 +00001582
1583 if (fallthrough_target.IsLinked()) {
1584 __ Bind(&fallthrough_target);
1585 }
Mark Mendellc4701932015-04-10 13:18:51 -04001586}
1587
David Brazdil0debae72015-11-12 18:37:00 +00001588static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1589 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1590 // are set only strictly before `branch`. We can't use the eflags on long/FP
1591 // conditions if they are materialized due to the complex branching.
1592 return cond->IsCondition() &&
1593 cond->GetNext() == branch &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001594 cond->InputAt(0)->GetType() != DataType::Type::kInt64 &&
1595 !DataType::IsFloatingPointType(cond->InputAt(0)->GetType());
David Brazdil0debae72015-11-12 18:37:00 +00001596}
1597
Mark Mendell152408f2015-12-31 12:28:50 -05001598template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001599void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001600 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001601 LabelType* true_target,
1602 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001603 HInstruction* cond = instruction->InputAt(condition_input_index);
1604
1605 if (true_target == nullptr && false_target == nullptr) {
1606 // Nothing to do. The code always falls through.
1607 return;
1608 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001609 // Constant condition, statically compared against "true" (integer value 1).
1610 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001611 if (true_target != nullptr) {
1612 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001613 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001614 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001615 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001616 if (false_target != nullptr) {
1617 __ jmp(false_target);
1618 }
1619 }
1620 return;
1621 }
1622
1623 // The following code generates these patterns:
1624 // (1) true_target == nullptr && false_target != nullptr
1625 // - opposite condition true => branch to false_target
1626 // (2) true_target != nullptr && false_target == nullptr
1627 // - condition true => branch to true_target
1628 // (3) true_target != nullptr && false_target != nullptr
1629 // - condition true => branch to true_target
1630 // - branch to false_target
1631 if (IsBooleanValueOrMaterializedCondition(cond)) {
1632 if (AreEflagsSetFrom(cond, instruction)) {
1633 if (true_target == nullptr) {
1634 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
1635 } else {
1636 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
1637 }
1638 } else {
1639 // Materialized condition, compare against 0.
1640 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1641 if (lhs.IsRegister()) {
1642 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1643 } else {
1644 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1645 }
1646 if (true_target == nullptr) {
1647 __ j(kEqual, false_target);
1648 } else {
1649 __ j(kNotEqual, true_target);
1650 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001651 }
1652 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001653 // Condition has not been materialized, use its inputs as the comparison and
1654 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001655 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001656
1657 // If this is a long or FP comparison that has been folded into
1658 // the HCondition, generate the comparison directly.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001659 DataType::Type type = condition->InputAt(0)->GetType();
1660 if (type == DataType::Type::kInt64 || DataType::IsFloatingPointType(type)) {
David Brazdil0debae72015-11-12 18:37:00 +00001661 GenerateCompareTestAndBranch(condition, true_target, false_target);
1662 return;
1663 }
1664
1665 Location lhs = condition->GetLocations()->InAt(0);
1666 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001667 // LHS is guaranteed to be in a register (see LocationsBuilderX86::HandleCondition).
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001668 codegen_->GenerateIntCompare(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00001669 if (true_target == nullptr) {
1670 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1671 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001672 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001673 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001674 }
David Brazdil0debae72015-11-12 18:37:00 +00001675
1676 // If neither branch falls through (case 3), the conditional branch to `true_target`
1677 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1678 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001679 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001680 }
1681}
1682
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001683void LocationsBuilderX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001684 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1685 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001686 locations->SetInAt(0, Location::Any());
1687 }
1688}
1689
1690void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001691 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1692 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1693 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1694 nullptr : codegen_->GetLabelOf(true_successor);
1695 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1696 nullptr : codegen_->GetLabelOf(false_successor);
1697 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001698}
1699
1700void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1701 LocationSummary* locations = new (GetGraph()->GetArena())
1702 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01001703 InvokeRuntimeCallingConvention calling_convention;
1704 RegisterSet caller_saves = RegisterSet::Empty();
1705 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1706 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00001707 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001708 locations->SetInAt(0, Location::Any());
1709 }
1710}
1711
1712void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001713 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001714 GenerateTestAndBranch<Label>(deoptimize,
1715 /* condition_input_index */ 0,
1716 slow_path->GetEntryLabel(),
1717 /* false_target */ nullptr);
1718}
1719
Mingyao Yang063fc772016-08-02 11:02:54 -07001720void LocationsBuilderX86::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
1721 LocationSummary* locations = new (GetGraph()->GetArena())
1722 LocationSummary(flag, LocationSummary::kNoCall);
1723 locations->SetOut(Location::RequiresRegister());
1724}
1725
1726void InstructionCodeGeneratorX86::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
1727 __ movl(flag->GetLocations()->Out().AsRegister<Register>(),
1728 Address(ESP, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
1729}
1730
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001731static bool SelectCanUseCMOV(HSelect* select) {
1732 // There are no conditional move instructions for XMMs.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001733 if (DataType::IsFloatingPointType(select->GetType())) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001734 return false;
1735 }
1736
1737 // A FP condition doesn't generate the single CC that we need.
1738 // In 32 bit mode, a long condition doesn't generate a single CC either.
1739 HInstruction* condition = select->GetCondition();
1740 if (condition->IsCondition()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001741 DataType::Type compare_type = condition->InputAt(0)->GetType();
1742 if (compare_type == DataType::Type::kInt64 ||
1743 DataType::IsFloatingPointType(compare_type)) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001744 return false;
1745 }
1746 }
1747
1748 // We can generate a CMOV for this Select.
1749 return true;
1750}
1751
David Brazdil74eb1b22015-12-14 11:44:01 +00001752void LocationsBuilderX86::VisitSelect(HSelect* select) {
1753 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001754 if (DataType::IsFloatingPointType(select->GetType())) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001755 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001756 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001757 } else {
1758 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001759 if (SelectCanUseCMOV(select)) {
1760 if (select->InputAt(1)->IsConstant()) {
1761 // Cmov can't handle a constant value.
1762 locations->SetInAt(1, Location::RequiresRegister());
1763 } else {
1764 locations->SetInAt(1, Location::Any());
1765 }
1766 } else {
1767 locations->SetInAt(1, Location::Any());
1768 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001769 }
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001770 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1771 locations->SetInAt(2, Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00001772 }
1773 locations->SetOut(Location::SameAsFirstInput());
1774}
1775
1776void InstructionCodeGeneratorX86::VisitSelect(HSelect* select) {
1777 LocationSummary* locations = select->GetLocations();
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001778 DCHECK(locations->InAt(0).Equals(locations->Out()));
1779 if (SelectCanUseCMOV(select)) {
1780 // If both the condition and the source types are integer, we can generate
1781 // a CMOV to implement Select.
1782
1783 HInstruction* select_condition = select->GetCondition();
1784 Condition cond = kNotEqual;
1785
1786 // Figure out how to test the 'condition'.
1787 if (select_condition->IsCondition()) {
1788 HCondition* condition = select_condition->AsCondition();
1789 if (!condition->IsEmittedAtUseSite()) {
1790 // This was a previously materialized condition.
1791 // Can we use the existing condition code?
1792 if (AreEflagsSetFrom(condition, select)) {
1793 // Materialization was the previous instruction. Condition codes are right.
1794 cond = X86Condition(condition->GetCondition());
1795 } else {
1796 // No, we have to recreate the condition code.
1797 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1798 __ testl(cond_reg, cond_reg);
1799 }
1800 } else {
1801 // We can't handle FP or long here.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001802 DCHECK_NE(condition->InputAt(0)->GetType(), DataType::Type::kInt64);
1803 DCHECK(!DataType::IsFloatingPointType(condition->InputAt(0)->GetType()));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001804 LocationSummary* cond_locations = condition->GetLocations();
Roland Levillain0b671c02016-08-19 12:02:34 +01001805 codegen_->GenerateIntCompare(cond_locations->InAt(0), cond_locations->InAt(1));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001806 cond = X86Condition(condition->GetCondition());
1807 }
1808 } else {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01001809 // Must be a Boolean condition, which needs to be compared to 0.
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001810 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1811 __ testl(cond_reg, cond_reg);
1812 }
1813
1814 // If the condition is true, overwrite the output, which already contains false.
1815 Location false_loc = locations->InAt(0);
1816 Location true_loc = locations->InAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001817 if (select->GetType() == DataType::Type::kInt64) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001818 // 64 bit conditional move.
1819 Register false_high = false_loc.AsRegisterPairHigh<Register>();
1820 Register false_low = false_loc.AsRegisterPairLow<Register>();
1821 if (true_loc.IsRegisterPair()) {
1822 __ cmovl(cond, false_high, true_loc.AsRegisterPairHigh<Register>());
1823 __ cmovl(cond, false_low, true_loc.AsRegisterPairLow<Register>());
1824 } else {
1825 __ cmovl(cond, false_high, Address(ESP, true_loc.GetHighStackIndex(kX86WordSize)));
1826 __ cmovl(cond, false_low, Address(ESP, true_loc.GetStackIndex()));
1827 }
1828 } else {
1829 // 32 bit conditional move.
1830 Register false_reg = false_loc.AsRegister<Register>();
1831 if (true_loc.IsRegister()) {
1832 __ cmovl(cond, false_reg, true_loc.AsRegister<Register>());
1833 } else {
1834 __ cmovl(cond, false_reg, Address(ESP, true_loc.GetStackIndex()));
1835 }
1836 }
1837 } else {
1838 NearLabel false_target;
1839 GenerateTestAndBranch<NearLabel>(
1840 select, /* condition_input_index */ 2, /* true_target */ nullptr, &false_target);
1841 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1842 __ Bind(&false_target);
1843 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001844}
1845
David Srbecky0cf44932015-12-09 14:09:59 +00001846void LocationsBuilderX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1847 new (GetGraph()->GetArena()) LocationSummary(info);
1848}
1849
David Srbeckyd28f4a02016-03-14 17:14:24 +00001850void InstructionCodeGeneratorX86::VisitNativeDebugInfo(HNativeDebugInfo*) {
1851 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001852}
1853
1854void CodeGeneratorX86::GenerateNop() {
1855 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001856}
1857
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001858void LocationsBuilderX86::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001859 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001860 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001861 // Handle the long/FP comparisons made in instruction simplification.
1862 switch (cond->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001863 case DataType::Type::kInt64: {
Mark Mendellc4701932015-04-10 13:18:51 -04001864 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell8659e842016-02-16 10:41:46 -05001865 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001866 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001867 locations->SetOut(Location::RequiresRegister());
1868 }
1869 break;
1870 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001871 case DataType::Type::kFloat32:
1872 case DataType::Type::kFloat64: {
Mark Mendellc4701932015-04-10 13:18:51 -04001873 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001874 if (cond->InputAt(1)->IsX86LoadFromConstantTable()) {
1875 DCHECK(cond->InputAt(1)->IsEmittedAtUseSite());
1876 } else if (cond->InputAt(1)->IsConstant()) {
1877 locations->SetInAt(1, Location::RequiresFpuRegister());
1878 } else {
1879 locations->SetInAt(1, Location::Any());
1880 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001881 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001882 locations->SetOut(Location::RequiresRegister());
1883 }
1884 break;
1885 }
1886 default:
1887 locations->SetInAt(0, Location::RequiresRegister());
1888 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001889 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001890 // We need a byte register.
1891 locations->SetOut(Location::RegisterLocation(ECX));
1892 }
1893 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001894 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001895}
1896
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001897void InstructionCodeGeneratorX86::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001898 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001899 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001900 }
Mark Mendellc4701932015-04-10 13:18:51 -04001901
1902 LocationSummary* locations = cond->GetLocations();
1903 Location lhs = locations->InAt(0);
1904 Location rhs = locations->InAt(1);
1905 Register reg = locations->Out().AsRegister<Register>();
Mark Mendell152408f2015-12-31 12:28:50 -05001906 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001907
1908 switch (cond->InputAt(0)->GetType()) {
1909 default: {
1910 // Integer case.
1911
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01001912 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04001913 __ xorl(reg, reg);
Roland Levillain0b671c02016-08-19 12:02:34 +01001914 codegen_->GenerateIntCompare(lhs, rhs);
Aart Bike9f37602015-10-09 11:15:55 -07001915 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001916 return;
1917 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001918 case DataType::Type::kInt64:
Mark Mendellc4701932015-04-10 13:18:51 -04001919 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1920 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001921 case DataType::Type::kFloat32:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001922 GenerateFPCompare(lhs, rhs, cond, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001923 GenerateFPJumps(cond, &true_label, &false_label);
1924 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001925 case DataType::Type::kFloat64:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001926 GenerateFPCompare(lhs, rhs, cond, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001927 GenerateFPJumps(cond, &true_label, &false_label);
1928 break;
1929 }
1930
1931 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001932 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001933
Roland Levillain4fa13f62015-07-06 18:11:54 +01001934 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001935 __ Bind(&false_label);
1936 __ xorl(reg, reg);
1937 __ jmp(&done_label);
1938
Roland Levillain4fa13f62015-07-06 18:11:54 +01001939 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001940 __ Bind(&true_label);
1941 __ movl(reg, Immediate(1));
1942 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001943}
1944
1945void LocationsBuilderX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001946 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001947}
1948
1949void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001950 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001951}
1952
1953void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001954 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001955}
1956
1957void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001958 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001959}
1960
1961void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001962 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001963}
1964
1965void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001966 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001967}
1968
1969void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001970 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001971}
1972
1973void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001974 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001975}
1976
1977void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001978 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001979}
1980
1981void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001982 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001983}
1984
1985void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001986 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001987}
1988
1989void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001990 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001991}
1992
Aart Bike9f37602015-10-09 11:15:55 -07001993void LocationsBuilderX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001994 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001995}
1996
1997void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001998 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001999}
2000
2001void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002002 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002003}
2004
2005void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002006 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002007}
2008
2009void LocationsBuilderX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002010 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002011}
2012
2013void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002014 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002015}
2016
2017void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002018 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002019}
2020
2021void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002022 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002023}
2024
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002025void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002026 LocationSummary* locations =
2027 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002028 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002029}
2030
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002031void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002032 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002033}
2034
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002035void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
2036 LocationSummary* locations =
2037 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2038 locations->SetOut(Location::ConstantLocation(constant));
2039}
2040
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002041void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002042 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002043}
2044
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002045void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002046 LocationSummary* locations =
2047 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002048 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002049}
2050
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002051void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002052 // Will be generated at use site.
2053}
2054
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002055void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
2056 LocationSummary* locations =
2057 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2058 locations->SetOut(Location::ConstantLocation(constant));
2059}
2060
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002061void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002062 // Will be generated at use site.
2063}
2064
2065void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
2066 LocationSummary* locations =
2067 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2068 locations->SetOut(Location::ConstantLocation(constant));
2069}
2070
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002071void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002072 // Will be generated at use site.
2073}
2074
Igor Murashkind01745e2017-04-05 16:40:31 -07002075void LocationsBuilderX86::VisitConstructorFence(HConstructorFence* constructor_fence) {
2076 constructor_fence->SetLocations(nullptr);
2077}
2078
2079void InstructionCodeGeneratorX86::VisitConstructorFence(
2080 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
2081 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
2082}
2083
Calin Juravle27df7582015-04-17 19:12:31 +01002084void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2085 memory_barrier->SetLocations(nullptr);
2086}
2087
2088void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00002089 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002090}
2091
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002092void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002093 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002094}
2095
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002096void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002097 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002098}
2099
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002100void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002101 LocationSummary* locations =
2102 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002103 switch (ret->InputAt(0)->GetType()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002104 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002105 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002106 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002107 case DataType::Type::kInt8:
2108 case DataType::Type::kUint16:
2109 case DataType::Type::kInt16:
2110 case DataType::Type::kInt32:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002111 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002112 break;
2113
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002114 case DataType::Type::kInt64:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002115 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002116 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002117 break;
2118
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002119 case DataType::Type::kFloat32:
2120 case DataType::Type::kFloat64:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002121 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002122 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002123 break;
2124
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002125 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002126 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002127 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002128}
2129
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002130void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002131 if (kIsDebugBuild) {
2132 switch (ret->InputAt(0)->GetType()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002133 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002134 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002135 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002136 case DataType::Type::kInt8:
2137 case DataType::Type::kUint16:
2138 case DataType::Type::kInt16:
2139 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002140 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002141 break;
2142
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002143 case DataType::Type::kInt64:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002144 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
2145 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002146 break;
2147
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002148 case DataType::Type::kFloat32:
2149 case DataType::Type::kFloat64:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002150 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002151 break;
2152
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002153 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002154 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002155 }
2156 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002157 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002158}
2159
Calin Juravle175dc732015-08-25 15:42:32 +01002160void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2161 // The trampoline uses the same calling convention as dex calling conventions,
2162 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2163 // the method_idx.
2164 HandleInvoke(invoke);
2165}
2166
2167void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2168 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2169}
2170
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002171void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002172 // Explicit clinit checks triggered by static invokes must have been pruned by
2173 // art::PrepareForRegisterAllocation.
2174 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002175
Mark Mendellfb8d2792015-03-31 22:16:59 -04002176 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002177 if (intrinsic.TryDispatch(invoke)) {
Vladimir Marko65979462017-05-19 17:25:12 +01002178 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeMethodLoadKind()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00002179 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002180 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04002181 return;
2182 }
2183
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002184 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002185
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002186 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
Vladimir Marko65979462017-05-19 17:25:12 +01002187 if (invoke->HasPcRelativeMethodLoadKind()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00002188 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002189 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002190}
2191
Mark Mendell09ed1a32015-03-25 08:30:06 -04002192static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
2193 if (invoke->GetLocations()->Intrinsified()) {
2194 IntrinsicCodeGeneratorX86 intrinsic(codegen);
2195 intrinsic.Dispatch(invoke);
2196 return true;
2197 }
2198 return false;
2199}
2200
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002201void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002202 // Explicit clinit checks triggered by static invokes must have been pruned by
2203 // art::PrepareForRegisterAllocation.
2204 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002205
Mark Mendell09ed1a32015-03-25 08:30:06 -04002206 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2207 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002208 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002209
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002210 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04002211 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002212 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002213}
2214
2215void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00002216 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
2217 if (intrinsic.TryDispatch(invoke)) {
2218 return;
2219 }
2220
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002221 HandleInvoke(invoke);
2222}
2223
2224void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002225 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002226 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002227}
2228
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002229void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002230 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2231 return;
2232 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002233
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002234 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002235 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002236}
2237
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002238void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002239 // This call to HandleInvoke allocates a temporary (core) register
2240 // which is also used to transfer the hidden argument from FP to
2241 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002242 HandleInvoke(invoke);
2243 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002244 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002245}
2246
2247void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
2248 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002249 LocationSummary* locations = invoke->GetLocations();
2250 Register temp = locations->GetTemp(0).AsRegister<Register>();
2251 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002252 Location receiver = locations->InAt(0);
2253 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2254
Roland Levillain0d5a2812015-11-13 10:07:31 +00002255 // Set the hidden argument. This is safe to do this here, as XMM7
2256 // won't be modified thereafter, before the `call` instruction.
2257 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002258 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002259 __ movd(hidden_reg, temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002260
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002261 if (receiver.IsStackSlot()) {
2262 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002263 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002264 __ movl(temp, Address(temp, class_offset));
2265 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002266 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002267 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002268 }
Roland Levillain4d027112015-07-01 15:41:14 +01002269 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002270 // Instead of simply (possibly) unpoisoning `temp` here, we should
2271 // emit a read barrier for the previous class reference load.
2272 // However this is not required in practice, as this is an
2273 // intermediate/temporary reference and because the current
2274 // concurrent copying collector keeps the from-space memory
2275 // intact/accessible until the end of the marking phase (the
2276 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002277 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002278 // temp = temp->GetAddressOfIMT()
2279 __ movl(temp,
2280 Address(temp, mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002281 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002282 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002283 invoke->GetImtIndex(), kX86PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002284 __ movl(temp, Address(temp, method_offset));
2285 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002286 __ call(Address(temp,
Andreas Gampe542451c2016-07-26 09:02:02 -07002287 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002288
2289 DCHECK(!codegen_->IsLeafMethod());
2290 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2291}
2292
Orion Hodsonac141392017-01-13 11:53:47 +00002293void LocationsBuilderX86::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2294 HandleInvoke(invoke);
2295}
2296
2297void InstructionCodeGeneratorX86::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2298 codegen_->GenerateInvokePolymorphicCall(invoke);
2299}
2300
Roland Levillain88cb1752014-10-20 16:36:47 +01002301void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2302 LocationSummary* locations =
2303 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2304 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002305 case DataType::Type::kInt32:
2306 case DataType::Type::kInt64:
Roland Levillain88cb1752014-10-20 16:36:47 +01002307 locations->SetInAt(0, Location::RequiresRegister());
2308 locations->SetOut(Location::SameAsFirstInput());
2309 break;
2310
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002311 case DataType::Type::kFloat32:
Roland Levillain5368c212014-11-27 15:03:41 +00002312 locations->SetInAt(0, Location::RequiresFpuRegister());
2313 locations->SetOut(Location::SameAsFirstInput());
2314 locations->AddTemp(Location::RequiresRegister());
2315 locations->AddTemp(Location::RequiresFpuRegister());
2316 break;
2317
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002318 case DataType::Type::kFloat64:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002319 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002320 locations->SetOut(Location::SameAsFirstInput());
2321 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002322 break;
2323
2324 default:
2325 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2326 }
2327}
2328
2329void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2330 LocationSummary* locations = neg->GetLocations();
2331 Location out = locations->Out();
2332 Location in = locations->InAt(0);
2333 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002334 case DataType::Type::kInt32:
Roland Levillain88cb1752014-10-20 16:36:47 +01002335 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002336 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002337 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002338 break;
2339
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002340 case DataType::Type::kInt64:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002341 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002342 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002343 __ negl(out.AsRegisterPairLow<Register>());
2344 // Negation is similar to subtraction from zero. The least
2345 // significant byte triggers a borrow when it is different from
2346 // zero; to take it into account, add 1 to the most significant
2347 // byte if the carry flag (CF) is set to 1 after the first NEGL
2348 // operation.
2349 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2350 __ negl(out.AsRegisterPairHigh<Register>());
2351 break;
2352
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002353 case DataType::Type::kFloat32: {
Roland Levillain5368c212014-11-27 15:03:41 +00002354 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002355 Register constant = locations->GetTemp(0).AsRegister<Register>();
2356 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002357 // Implement float negation with an exclusive or with value
2358 // 0x80000000 (mask for bit 31, representing the sign of a
2359 // single-precision floating-point number).
2360 __ movl(constant, Immediate(INT32_C(0x80000000)));
2361 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002362 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002363 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002364 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002365
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002366 case DataType::Type::kFloat64: {
Roland Levillain5368c212014-11-27 15:03:41 +00002367 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002368 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002369 // Implement double negation with an exclusive or with value
2370 // 0x8000000000000000 (mask for bit 63, representing the sign of
2371 // a double-precision floating-point number).
2372 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002373 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002374 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002375 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002376
2377 default:
2378 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2379 }
2380}
2381
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002382void LocationsBuilderX86::VisitX86FPNeg(HX86FPNeg* neg) {
2383 LocationSummary* locations =
2384 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002385 DCHECK(DataType::IsFloatingPointType(neg->GetType()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002386 locations->SetInAt(0, Location::RequiresFpuRegister());
2387 locations->SetInAt(1, Location::RequiresRegister());
2388 locations->SetOut(Location::SameAsFirstInput());
2389 locations->AddTemp(Location::RequiresFpuRegister());
2390}
2391
2392void InstructionCodeGeneratorX86::VisitX86FPNeg(HX86FPNeg* neg) {
2393 LocationSummary* locations = neg->GetLocations();
2394 Location out = locations->Out();
2395 DCHECK(locations->InAt(0).Equals(out));
2396
2397 Register constant_area = locations->InAt(1).AsRegister<Register>();
2398 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002399 if (neg->GetType() == DataType::Type::kFloat32) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00002400 __ movss(mask, codegen_->LiteralInt32Address(INT32_C(0x80000000),
2401 neg->GetBaseMethodAddress(),
2402 constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002403 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
2404 } else {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00002405 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000),
2406 neg->GetBaseMethodAddress(),
2407 constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002408 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
2409 }
2410}
2411
Roland Levillaindff1f282014-11-05 14:15:05 +00002412void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002413 DataType::Type result_type = conversion->GetResultType();
2414 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002415 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
2416 << input_type << " -> " << result_type;
Roland Levillain624279f2014-12-04 11:54:28 +00002417
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002418 // The float-to-long and double-to-long type conversions rely on a
2419 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002420 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002421 ((input_type == DataType::Type::kFloat32 || input_type == DataType::Type::kFloat64)
2422 && result_type == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002423 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002424 : LocationSummary::kNoCall;
2425 LocationSummary* locations =
2426 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2427
Roland Levillaindff1f282014-11-05 14:15:05 +00002428 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002429 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002430 case DataType::Type::kInt8:
Roland Levillain51d3fc42014-11-13 14:11:42 +00002431 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002432 case DataType::Type::kUint8:
2433 case DataType::Type::kInt8:
2434 case DataType::Type::kUint16:
2435 case DataType::Type::kInt16:
2436 case DataType::Type::kInt32:
2437 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2438 // Make the output overlap to please the register allocator. This greatly simplifies
2439 // the validation of the linear scan implementation
2440 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2441 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002442 case DataType::Type::kInt64: {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002443 HInstruction* input = conversion->InputAt(0);
2444 Location input_location = input->IsConstant()
2445 ? Location::ConstantLocation(input->AsConstant())
2446 : Location::RegisterPairLocation(EAX, EDX);
2447 locations->SetInAt(0, input_location);
2448 // Make the output overlap to please the register allocator. This greatly simplifies
2449 // the validation of the linear scan implementation
2450 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2451 break;
2452 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002453
2454 default:
2455 LOG(FATAL) << "Unexpected type conversion from " << input_type
2456 << " to " << result_type;
2457 }
2458 break;
2459
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002460 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002461 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002462 DCHECK(DataType::IsIntegralType(input_type)) << input_type;
2463 locations->SetInAt(0, Location::Any());
2464 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Roland Levillain01a8d712014-11-14 16:27:39 +00002465 break;
2466
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002467 case DataType::Type::kInt32:
Roland Levillain946e1432014-11-11 17:35:19 +00002468 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002469 case DataType::Type::kInt64:
Roland Levillain946e1432014-11-11 17:35:19 +00002470 locations->SetInAt(0, Location::Any());
2471 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2472 break;
2473
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002474 case DataType::Type::kFloat32:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002475 locations->SetInAt(0, Location::RequiresFpuRegister());
2476 locations->SetOut(Location::RequiresRegister());
2477 locations->AddTemp(Location::RequiresFpuRegister());
2478 break;
2479
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002480 case DataType::Type::kFloat64:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002481 locations->SetInAt(0, Location::RequiresFpuRegister());
2482 locations->SetOut(Location::RequiresRegister());
2483 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002484 break;
2485
2486 default:
2487 LOG(FATAL) << "Unexpected type conversion from " << input_type
2488 << " to " << result_type;
2489 }
2490 break;
2491
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002492 case DataType::Type::kInt64:
Roland Levillaindff1f282014-11-05 14:15:05 +00002493 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002494 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002495 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002496 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002497 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002498 case DataType::Type::kInt16:
2499 case DataType::Type::kInt32:
Roland Levillaindff1f282014-11-05 14:15:05 +00002500 locations->SetInAt(0, Location::RegisterLocation(EAX));
2501 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2502 break;
2503
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002504 case DataType::Type::kFloat32:
2505 case DataType::Type::kFloat64: {
Vladimir Marko949c91f2015-01-27 10:48:44 +00002506 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002507 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2508 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2509
Vladimir Marko949c91f2015-01-27 10:48:44 +00002510 // The runtime helper puts the result in EAX, EDX.
2511 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002512 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002513 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002514
2515 default:
2516 LOG(FATAL) << "Unexpected type conversion from " << input_type
2517 << " to " << result_type;
2518 }
2519 break;
2520
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002521 case DataType::Type::kFloat32:
Roland Levillaincff13742014-11-17 14:32:17 +00002522 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002523 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002524 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002525 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002526 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002527 case DataType::Type::kInt16:
2528 case DataType::Type::kInt32:
Roland Levillaincff13742014-11-17 14:32:17 +00002529 locations->SetInAt(0, Location::RequiresRegister());
2530 locations->SetOut(Location::RequiresFpuRegister());
2531 break;
2532
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002533 case DataType::Type::kInt64:
Roland Levillain232ade02015-04-20 15:14:36 +01002534 locations->SetInAt(0, Location::Any());
2535 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002536 break;
2537
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002538 case DataType::Type::kFloat64:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002539 locations->SetInAt(0, Location::RequiresFpuRegister());
2540 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002541 break;
2542
2543 default:
2544 LOG(FATAL) << "Unexpected type conversion from " << input_type
2545 << " to " << result_type;
2546 };
2547 break;
2548
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002549 case DataType::Type::kFloat64:
Roland Levillaincff13742014-11-17 14:32:17 +00002550 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002551 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002552 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002553 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002554 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002555 case DataType::Type::kInt16:
2556 case DataType::Type::kInt32:
Roland Levillaincff13742014-11-17 14:32:17 +00002557 locations->SetInAt(0, Location::RequiresRegister());
2558 locations->SetOut(Location::RequiresFpuRegister());
2559 break;
2560
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002561 case DataType::Type::kInt64:
Roland Levillain232ade02015-04-20 15:14:36 +01002562 locations->SetInAt(0, Location::Any());
2563 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002564 break;
2565
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002566 case DataType::Type::kFloat32:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002567 locations->SetInAt(0, Location::RequiresFpuRegister());
2568 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002569 break;
2570
2571 default:
2572 LOG(FATAL) << "Unexpected type conversion from " << input_type
2573 << " to " << result_type;
2574 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002575 break;
2576
2577 default:
2578 LOG(FATAL) << "Unexpected type conversion from " << input_type
2579 << " to " << result_type;
2580 }
2581}
2582
2583void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2584 LocationSummary* locations = conversion->GetLocations();
2585 Location out = locations->Out();
2586 Location in = locations->InAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002587 DataType::Type result_type = conversion->GetResultType();
2588 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002589 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
2590 << input_type << " -> " << result_type;
Roland Levillaindff1f282014-11-05 14:15:05 +00002591 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002592 case DataType::Type::kUint8:
2593 switch (input_type) {
2594 case DataType::Type::kInt8:
2595 case DataType::Type::kUint16:
2596 case DataType::Type::kInt16:
2597 case DataType::Type::kInt32:
2598 if (in.IsRegister()) {
2599 __ movzxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
2600 } else {
2601 DCHECK(in.GetConstant()->IsIntConstant());
2602 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2603 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint8_t>(value)));
2604 }
2605 break;
2606 case DataType::Type::kInt64:
2607 if (in.IsRegisterPair()) {
2608 __ movzxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2609 } else {
2610 DCHECK(in.GetConstant()->IsLongConstant());
2611 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2612 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint8_t>(value)));
2613 }
2614 break;
2615
2616 default:
2617 LOG(FATAL) << "Unexpected type conversion from " << input_type
2618 << " to " << result_type;
2619 }
2620 break;
2621
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002622 case DataType::Type::kInt8:
Roland Levillain51d3fc42014-11-13 14:11:42 +00002623 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002624 case DataType::Type::kUint8:
2625 case DataType::Type::kUint16:
2626 case DataType::Type::kInt16:
2627 case DataType::Type::kInt32:
2628 if (in.IsRegister()) {
2629 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
2630 } else {
2631 DCHECK(in.GetConstant()->IsIntConstant());
2632 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2633 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2634 }
2635 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002636 case DataType::Type::kInt64:
Vladimir Markob52bbde2016-02-12 12:06:05 +00002637 if (in.IsRegisterPair()) {
2638 __ movsxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2639 } else {
2640 DCHECK(in.GetConstant()->IsLongConstant());
2641 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2642 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2643 }
2644 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002645
2646 default:
2647 LOG(FATAL) << "Unexpected type conversion from " << input_type
2648 << " to " << result_type;
2649 }
2650 break;
2651
2652 case DataType::Type::kUint16:
2653 switch (input_type) {
2654 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002655 case DataType::Type::kInt16:
2656 case DataType::Type::kInt32:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002657 if (in.IsRegister()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002658 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
2659 } else if (in.IsStackSlot()) {
2660 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002661 } else {
2662 DCHECK(in.GetConstant()->IsIntConstant());
2663 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002664 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
2665 }
2666 break;
2667 case DataType::Type::kInt64:
2668 if (in.IsRegisterPair()) {
2669 __ movzxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2670 } else if (in.IsDoubleStackSlot()) {
2671 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2672 } else {
2673 DCHECK(in.GetConstant()->IsLongConstant());
2674 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2675 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002676 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002677 break;
2678
2679 default:
2680 LOG(FATAL) << "Unexpected type conversion from " << input_type
2681 << " to " << result_type;
2682 }
2683 break;
2684
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002685 case DataType::Type::kInt16:
Roland Levillain01a8d712014-11-14 16:27:39 +00002686 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002687 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002688 case DataType::Type::kInt32:
Roland Levillain01a8d712014-11-14 16:27:39 +00002689 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002690 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002691 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002692 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002693 } else {
2694 DCHECK(in.GetConstant()->IsIntConstant());
2695 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002696 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002697 }
2698 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002699 case DataType::Type::kInt64:
2700 if (in.IsRegisterPair()) {
2701 __ movsxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2702 } else if (in.IsDoubleStackSlot()) {
2703 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2704 } else {
2705 DCHECK(in.GetConstant()->IsLongConstant());
2706 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2707 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
2708 }
2709 break;
Roland Levillain01a8d712014-11-14 16:27:39 +00002710
2711 default:
2712 LOG(FATAL) << "Unexpected type conversion from " << input_type
2713 << " to " << result_type;
2714 }
2715 break;
2716
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002717 case DataType::Type::kInt32:
Roland Levillain946e1432014-11-11 17:35:19 +00002718 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002719 case DataType::Type::kInt64:
Roland Levillain946e1432014-11-11 17:35:19 +00002720 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002721 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002722 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002723 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002724 } else {
2725 DCHECK(in.IsConstant());
2726 DCHECK(in.GetConstant()->IsLongConstant());
2727 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002728 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002729 }
2730 break;
2731
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002732 case DataType::Type::kFloat32: {
Roland Levillain3f8f9362014-12-02 17:45:01 +00002733 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2734 Register output = out.AsRegister<Register>();
2735 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002736 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002737
2738 __ movl(output, Immediate(kPrimIntMax));
2739 // temp = int-to-float(output)
2740 __ cvtsi2ss(temp, output);
2741 // if input >= temp goto done
2742 __ comiss(input, temp);
2743 __ j(kAboveEqual, &done);
2744 // if input == NaN goto nan
2745 __ j(kUnordered, &nan);
2746 // output = float-to-int-truncate(input)
2747 __ cvttss2si(output, input);
2748 __ jmp(&done);
2749 __ Bind(&nan);
2750 // output = 0
2751 __ xorl(output, output);
2752 __ Bind(&done);
2753 break;
2754 }
2755
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002756 case DataType::Type::kFloat64: {
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002757 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2758 Register output = out.AsRegister<Register>();
2759 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002760 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002761
2762 __ movl(output, Immediate(kPrimIntMax));
2763 // temp = int-to-double(output)
2764 __ cvtsi2sd(temp, output);
2765 // if input >= temp goto done
2766 __ comisd(input, temp);
2767 __ j(kAboveEqual, &done);
2768 // if input == NaN goto nan
2769 __ j(kUnordered, &nan);
2770 // output = double-to-int-truncate(input)
2771 __ cvttsd2si(output, input);
2772 __ jmp(&done);
2773 __ Bind(&nan);
2774 // output = 0
2775 __ xorl(output, output);
2776 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002777 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002778 }
Roland Levillain946e1432014-11-11 17:35:19 +00002779
2780 default:
2781 LOG(FATAL) << "Unexpected type conversion from " << input_type
2782 << " to " << result_type;
2783 }
2784 break;
2785
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002786 case DataType::Type::kInt64:
Roland Levillaindff1f282014-11-05 14:15:05 +00002787 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002788 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002789 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002790 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002791 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002792 case DataType::Type::kInt16:
2793 case DataType::Type::kInt32:
Roland Levillaindff1f282014-11-05 14:15:05 +00002794 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2795 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002796 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002797 __ cdq();
2798 break;
2799
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002800 case DataType::Type::kFloat32:
Serban Constantinescuba45db02016-07-12 22:53:02 +01002801 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002802 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002803 break;
2804
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002805 case DataType::Type::kFloat64:
Serban Constantinescuba45db02016-07-12 22:53:02 +01002806 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002807 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002808 break;
2809
2810 default:
2811 LOG(FATAL) << "Unexpected type conversion from " << input_type
2812 << " to " << result_type;
2813 }
2814 break;
2815
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002816 case DataType::Type::kFloat32:
Roland Levillaincff13742014-11-17 14:32:17 +00002817 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002818 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002819 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002820 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002821 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002822 case DataType::Type::kInt16:
2823 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002824 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002825 break;
2826
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002827 case DataType::Type::kInt64: {
Roland Levillain232ade02015-04-20 15:14:36 +01002828 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002829
Roland Levillain232ade02015-04-20 15:14:36 +01002830 // Create stack space for the call to
2831 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2832 // TODO: enhance register allocator to ask for stack temporaries.
2833 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002834 adjustment = DataType::Size(DataType::Type::kInt64);
Roland Levillain232ade02015-04-20 15:14:36 +01002835 __ subl(ESP, Immediate(adjustment));
2836 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002837
Roland Levillain232ade02015-04-20 15:14:36 +01002838 // Load the value to the FP stack, using temporaries if needed.
2839 PushOntoFPStack(in, 0, adjustment, false, true);
2840
2841 if (out.IsStackSlot()) {
2842 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2843 } else {
2844 __ fstps(Address(ESP, 0));
2845 Location stack_temp = Location::StackSlot(0);
2846 codegen_->Move32(out, stack_temp);
2847 }
2848
2849 // Remove the temporary stack space we allocated.
2850 if (adjustment != 0) {
2851 __ addl(ESP, Immediate(adjustment));
2852 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002853 break;
2854 }
2855
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002856 case DataType::Type::kFloat64:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002857 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002858 break;
2859
2860 default:
2861 LOG(FATAL) << "Unexpected type conversion from " << input_type
2862 << " to " << result_type;
2863 };
2864 break;
2865
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002866 case DataType::Type::kFloat64:
Roland Levillaincff13742014-11-17 14:32:17 +00002867 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002868 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002869 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002870 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002871 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002872 case DataType::Type::kInt16:
2873 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002874 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002875 break;
2876
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002877 case DataType::Type::kInt64: {
Roland Levillain232ade02015-04-20 15:14:36 +01002878 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002879
Roland Levillain232ade02015-04-20 15:14:36 +01002880 // Create stack space for the call to
2881 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2882 // TODO: enhance register allocator to ask for stack temporaries.
2883 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002884 adjustment = DataType::Size(DataType::Type::kInt64);
Roland Levillain232ade02015-04-20 15:14:36 +01002885 __ subl(ESP, Immediate(adjustment));
2886 }
2887
2888 // Load the value to the FP stack, using temporaries if needed.
2889 PushOntoFPStack(in, 0, adjustment, false, true);
2890
2891 if (out.IsDoubleStackSlot()) {
2892 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2893 } else {
2894 __ fstpl(Address(ESP, 0));
2895 Location stack_temp = Location::DoubleStackSlot(0);
2896 codegen_->Move64(out, stack_temp);
2897 }
2898
2899 // Remove the temporary stack space we allocated.
2900 if (adjustment != 0) {
2901 __ addl(ESP, Immediate(adjustment));
2902 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002903 break;
2904 }
2905
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002906 case DataType::Type::kFloat32:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002907 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002908 break;
2909
2910 default:
2911 LOG(FATAL) << "Unexpected type conversion from " << input_type
2912 << " to " << result_type;
2913 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002914 break;
2915
2916 default:
2917 LOG(FATAL) << "Unexpected type conversion from " << input_type
2918 << " to " << result_type;
2919 }
2920}
2921
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002922void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002923 LocationSummary* locations =
2924 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002925 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002926 case DataType::Type::kInt32: {
Mark Mendell09b84632015-02-13 17:48:38 -05002927 locations->SetInAt(0, Location::RequiresRegister());
2928 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2929 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2930 break;
2931 }
2932
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002933 case DataType::Type::kInt64: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002934 locations->SetInAt(0, Location::RequiresRegister());
2935 locations->SetInAt(1, Location::Any());
2936 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002937 break;
2938 }
2939
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002940 case DataType::Type::kFloat32:
2941 case DataType::Type::kFloat64: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002942 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002943 if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2944 DCHECK(add->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002945 } else if (add->InputAt(1)->IsConstant()) {
2946 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002947 } else {
2948 locations->SetInAt(1, Location::Any());
2949 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002950 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002951 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002952 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002953
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002954 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002955 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2956 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002957 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002958}
2959
2960void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2961 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002962 Location first = locations->InAt(0);
2963 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002964 Location out = locations->Out();
2965
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002966 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002967 case DataType::Type::kInt32: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002968 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002969 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2970 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002971 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2972 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05002973 } else {
2974 __ leal(out.AsRegister<Register>(), Address(
2975 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
2976 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002977 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002978 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
2979 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2980 __ addl(out.AsRegister<Register>(), Immediate(value));
2981 } else {
2982 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
2983 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002984 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05002985 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002986 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002987 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002988 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002989 }
2990
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002991 case DataType::Type::kInt64: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002992 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002993 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2994 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002995 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002996 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
2997 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002998 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002999 } else {
3000 DCHECK(second.IsConstant()) << second;
3001 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3002 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3003 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003004 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003005 break;
3006 }
3007
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003008 case DataType::Type::kFloat32: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003009 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003010 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04003011 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3012 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003013 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003014 __ addss(first.AsFpuRegister<XmmRegister>(),
3015 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003016 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3017 const_area->GetBaseMethodAddress(),
3018 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003019 } else {
3020 DCHECK(second.IsStackSlot());
3021 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003022 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003023 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003024 }
3025
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003026 case DataType::Type::kFloat64: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003027 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003028 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04003029 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3030 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003031 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003032 __ addsd(first.AsFpuRegister<XmmRegister>(),
3033 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003034 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3035 const_area->GetBaseMethodAddress(),
3036 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003037 } else {
3038 DCHECK(second.IsDoubleStackSlot());
3039 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003040 }
3041 break;
3042 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003043
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003044 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003045 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003046 }
3047}
3048
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003049void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003050 LocationSummary* locations =
3051 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003052 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003053 case DataType::Type::kInt32:
3054 case DataType::Type::kInt64: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003055 locations->SetInAt(0, Location::RequiresRegister());
3056 locations->SetInAt(1, Location::Any());
3057 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003058 break;
3059 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003060 case DataType::Type::kFloat32:
3061 case DataType::Type::kFloat64: {
Calin Juravle11351682014-10-23 15:38:15 +01003062 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003063 if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3064 DCHECK(sub->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003065 } else if (sub->InputAt(1)->IsConstant()) {
3066 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003067 } else {
3068 locations->SetInAt(1, Location::Any());
3069 }
Calin Juravle11351682014-10-23 15:38:15 +01003070 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003071 break;
Calin Juravle11351682014-10-23 15:38:15 +01003072 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003073
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003074 default:
Calin Juravle11351682014-10-23 15:38:15 +01003075 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003076 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003077}
3078
3079void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
3080 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003081 Location first = locations->InAt(0);
3082 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01003083 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003084 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003085 case DataType::Type::kInt32: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003086 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003087 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003088 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003089 __ subl(first.AsRegister<Register>(),
3090 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003091 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003092 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003093 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003094 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003095 }
3096
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003097 case DataType::Type::kInt64: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003098 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01003099 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3100 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003101 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01003102 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003103 __ sbbl(first.AsRegisterPairHigh<Register>(),
3104 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003105 } else {
3106 DCHECK(second.IsConstant()) << second;
3107 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3108 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3109 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003110 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003111 break;
3112 }
3113
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003114 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003115 if (second.IsFpuRegister()) {
3116 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3117 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3118 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003119 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003120 __ subss(first.AsFpuRegister<XmmRegister>(),
3121 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003122 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3123 const_area->GetBaseMethodAddress(),
3124 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003125 } else {
3126 DCHECK(second.IsStackSlot());
3127 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3128 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003129 break;
Calin Juravle11351682014-10-23 15:38:15 +01003130 }
3131
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003132 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003133 if (second.IsFpuRegister()) {
3134 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3135 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3136 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003137 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003138 __ subsd(first.AsFpuRegister<XmmRegister>(),
3139 codegen_->LiteralDoubleAddress(
3140 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003141 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003142 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3143 } else {
3144 DCHECK(second.IsDoubleStackSlot());
3145 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3146 }
Calin Juravle11351682014-10-23 15:38:15 +01003147 break;
3148 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003149
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003150 default:
Calin Juravle11351682014-10-23 15:38:15 +01003151 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003152 }
3153}
3154
Calin Juravle34bacdf2014-10-07 20:23:36 +01003155void LocationsBuilderX86::VisitMul(HMul* mul) {
3156 LocationSummary* locations =
3157 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3158 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003159 case DataType::Type::kInt32:
Calin Juravle34bacdf2014-10-07 20:23:36 +01003160 locations->SetInAt(0, Location::RequiresRegister());
3161 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003162 if (mul->InputAt(1)->IsIntConstant()) {
3163 // Can use 3 operand multiply.
3164 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3165 } else {
3166 locations->SetOut(Location::SameAsFirstInput());
3167 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003168 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003169 case DataType::Type::kInt64: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003170 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003171 locations->SetInAt(1, Location::Any());
3172 locations->SetOut(Location::SameAsFirstInput());
3173 // Needed for imul on 32bits with 64bits output.
3174 locations->AddTemp(Location::RegisterLocation(EAX));
3175 locations->AddTemp(Location::RegisterLocation(EDX));
3176 break;
3177 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003178 case DataType::Type::kFloat32:
3179 case DataType::Type::kFloat64: {
Calin Juravleb5bfa962014-10-21 18:02:24 +01003180 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003181 if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3182 DCHECK(mul->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003183 } else if (mul->InputAt(1)->IsConstant()) {
3184 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003185 } else {
3186 locations->SetInAt(1, Location::Any());
3187 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003188 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003189 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003190 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003191
3192 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003193 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003194 }
3195}
3196
3197void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
3198 LocationSummary* locations = mul->GetLocations();
3199 Location first = locations->InAt(0);
3200 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003201 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003202
3203 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003204 case DataType::Type::kInt32:
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003205 // The constant may have ended up in a register, so test explicitly to avoid
3206 // problems where the output may not be the same as the first operand.
3207 if (mul->InputAt(1)->IsIntConstant()) {
3208 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3209 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
3210 } else if (second.IsRegister()) {
3211 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003212 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003213 } else {
3214 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003215 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003216 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003217 }
3218 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003219
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003220 case DataType::Type::kInt64: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003221 Register in1_hi = first.AsRegisterPairHigh<Register>();
3222 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003223 Register eax = locations->GetTemp(0).AsRegister<Register>();
3224 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003225
3226 DCHECK_EQ(EAX, eax);
3227 DCHECK_EQ(EDX, edx);
3228
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003229 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01003230 // output: in1
3231 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3232 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3233 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003234 if (second.IsConstant()) {
3235 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003236
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003237 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3238 int32_t low_value = Low32Bits(value);
3239 int32_t high_value = High32Bits(value);
3240 Immediate low(low_value);
3241 Immediate high(high_value);
3242
3243 __ movl(eax, high);
3244 // eax <- in1.lo * in2.hi
3245 __ imull(eax, in1_lo);
3246 // in1.hi <- in1.hi * in2.lo
3247 __ imull(in1_hi, low);
3248 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3249 __ addl(in1_hi, eax);
3250 // move in2_lo to eax to prepare for double precision
3251 __ movl(eax, low);
3252 // edx:eax <- in1.lo * in2.lo
3253 __ mull(in1_lo);
3254 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3255 __ addl(in1_hi, edx);
3256 // in1.lo <- (in1.lo * in2.lo)[31:0];
3257 __ movl(in1_lo, eax);
3258 } else if (second.IsRegisterPair()) {
3259 Register in2_hi = second.AsRegisterPairHigh<Register>();
3260 Register in2_lo = second.AsRegisterPairLow<Register>();
3261
3262 __ movl(eax, in2_hi);
3263 // eax <- in1.lo * in2.hi
3264 __ imull(eax, in1_lo);
3265 // in1.hi <- in1.hi * in2.lo
3266 __ imull(in1_hi, in2_lo);
3267 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3268 __ addl(in1_hi, eax);
3269 // move in1_lo to eax to prepare for double precision
3270 __ movl(eax, in1_lo);
3271 // edx:eax <- in1.lo * in2.lo
3272 __ mull(in2_lo);
3273 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3274 __ addl(in1_hi, edx);
3275 // in1.lo <- (in1.lo * in2.lo)[31:0];
3276 __ movl(in1_lo, eax);
3277 } else {
3278 DCHECK(second.IsDoubleStackSlot()) << second;
3279 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
3280 Address in2_lo(ESP, second.GetStackIndex());
3281
3282 __ movl(eax, in2_hi);
3283 // eax <- in1.lo * in2.hi
3284 __ imull(eax, in1_lo);
3285 // in1.hi <- in1.hi * in2.lo
3286 __ imull(in1_hi, in2_lo);
3287 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3288 __ addl(in1_hi, eax);
3289 // move in1_lo to eax to prepare for double precision
3290 __ movl(eax, in1_lo);
3291 // edx:eax <- in1.lo * in2.lo
3292 __ mull(in2_lo);
3293 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3294 __ addl(in1_hi, edx);
3295 // in1.lo <- (in1.lo * in2.lo)[31:0];
3296 __ movl(in1_lo, eax);
3297 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003298
3299 break;
3300 }
3301
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003302 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003303 DCHECK(first.Equals(locations->Out()));
3304 if (second.IsFpuRegister()) {
3305 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3306 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3307 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003308 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003309 __ mulss(first.AsFpuRegister<XmmRegister>(),
3310 codegen_->LiteralFloatAddress(
3311 const_area->GetConstant()->AsFloatConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003312 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003313 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3314 } else {
3315 DCHECK(second.IsStackSlot());
3316 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3317 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003318 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003319 }
3320
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003321 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003322 DCHECK(first.Equals(locations->Out()));
3323 if (second.IsFpuRegister()) {
3324 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3325 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3326 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003327 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003328 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3329 codegen_->LiteralDoubleAddress(
3330 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003331 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003332 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3333 } else {
3334 DCHECK(second.IsDoubleStackSlot());
3335 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3336 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003337 break;
3338 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003339
3340 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003341 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003342 }
3343}
3344
Roland Levillain232ade02015-04-20 15:14:36 +01003345void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3346 uint32_t temp_offset,
3347 uint32_t stack_adjustment,
3348 bool is_fp,
3349 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003350 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003351 DCHECK(!is_wide);
3352 if (is_fp) {
3353 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3354 } else {
3355 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3356 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003357 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003358 DCHECK(is_wide);
3359 if (is_fp) {
3360 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3361 } else {
3362 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3363 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003364 } else {
3365 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003366 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003367 Location stack_temp = Location::StackSlot(temp_offset);
3368 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003369 if (is_fp) {
3370 __ flds(Address(ESP, temp_offset));
3371 } else {
3372 __ filds(Address(ESP, temp_offset));
3373 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003374 } else {
3375 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3376 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003377 if (is_fp) {
3378 __ fldl(Address(ESP, temp_offset));
3379 } else {
3380 __ fildl(Address(ESP, temp_offset));
3381 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003382 }
3383 }
3384}
3385
3386void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003387 DataType::Type type = rem->GetResultType();
3388 bool is_float = type == DataType::Type::kFloat32;
3389 size_t elem_size = DataType::Size(type);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003390 LocationSummary* locations = rem->GetLocations();
3391 Location first = locations->InAt(0);
3392 Location second = locations->InAt(1);
3393 Location out = locations->Out();
3394
3395 // Create stack space for 2 elements.
3396 // TODO: enhance register allocator to ask for stack temporaries.
3397 __ subl(ESP, Immediate(2 * elem_size));
3398
3399 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003400 const bool is_wide = !is_float;
3401 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
3402 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003403
3404 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003405 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003406 __ Bind(&retry);
3407 __ fprem();
3408
3409 // Move FP status to AX.
3410 __ fstsw();
3411
3412 // And see if the argument reduction is complete. This is signaled by the
3413 // C2 FPU flag bit set to 0.
3414 __ andl(EAX, Immediate(kC2ConditionMask));
3415 __ j(kNotEqual, &retry);
3416
3417 // We have settled on the final value. Retrieve it into an XMM register.
3418 // Store FP top of stack to real stack.
3419 if (is_float) {
3420 __ fsts(Address(ESP, 0));
3421 } else {
3422 __ fstl(Address(ESP, 0));
3423 }
3424
3425 // Pop the 2 items from the FP stack.
3426 __ fucompp();
3427
3428 // Load the value from the stack into an XMM register.
3429 DCHECK(out.IsFpuRegister()) << out;
3430 if (is_float) {
3431 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3432 } else {
3433 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3434 }
3435
3436 // And remove the temporary stack space we allocated.
3437 __ addl(ESP, Immediate(2 * elem_size));
3438}
3439
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003440
3441void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3442 DCHECK(instruction->IsDiv() || instruction->IsRem());
3443
3444 LocationSummary* locations = instruction->GetLocations();
3445 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003446 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003447
3448 Register out_register = locations->Out().AsRegister<Register>();
3449 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003450 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003451
3452 DCHECK(imm == 1 || imm == -1);
3453
3454 if (instruction->IsRem()) {
3455 __ xorl(out_register, out_register);
3456 } else {
3457 __ movl(out_register, input_register);
3458 if (imm == -1) {
3459 __ negl(out_register);
3460 }
3461 }
3462}
3463
3464
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003465void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003466 LocationSummary* locations = instruction->GetLocations();
3467
3468 Register out_register = locations->Out().AsRegister<Register>();
3469 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003470 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003471 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3472 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003473
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003474 Register num = locations->GetTemp(0).AsRegister<Register>();
3475
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003476 __ leal(num, Address(input_register, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003477 __ testl(input_register, input_register);
3478 __ cmovl(kGreaterEqual, num, input_register);
3479 int shift = CTZ(imm);
3480 __ sarl(num, Immediate(shift));
3481
3482 if (imm < 0) {
3483 __ negl(num);
3484 }
3485
3486 __ movl(out_register, num);
3487}
3488
3489void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3490 DCHECK(instruction->IsDiv() || instruction->IsRem());
3491
3492 LocationSummary* locations = instruction->GetLocations();
3493 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3494
3495 Register eax = locations->InAt(0).AsRegister<Register>();
3496 Register out = locations->Out().AsRegister<Register>();
3497 Register num;
3498 Register edx;
3499
3500 if (instruction->IsDiv()) {
3501 edx = locations->GetTemp(0).AsRegister<Register>();
3502 num = locations->GetTemp(1).AsRegister<Register>();
3503 } else {
3504 edx = locations->Out().AsRegister<Register>();
3505 num = locations->GetTemp(0).AsRegister<Register>();
3506 }
3507
3508 DCHECK_EQ(EAX, eax);
3509 DCHECK_EQ(EDX, edx);
3510 if (instruction->IsDiv()) {
3511 DCHECK_EQ(EAX, out);
3512 } else {
3513 DCHECK_EQ(EDX, out);
3514 }
3515
3516 int64_t magic;
3517 int shift;
3518 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3519
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003520 // Save the numerator.
3521 __ movl(num, eax);
3522
3523 // EAX = magic
3524 __ movl(eax, Immediate(magic));
3525
3526 // EDX:EAX = magic * numerator
3527 __ imull(num);
3528
3529 if (imm > 0 && magic < 0) {
3530 // EDX += num
3531 __ addl(edx, num);
3532 } else if (imm < 0 && magic > 0) {
3533 __ subl(edx, num);
3534 }
3535
3536 // Shift if needed.
3537 if (shift != 0) {
3538 __ sarl(edx, Immediate(shift));
3539 }
3540
3541 // EDX += 1 if EDX < 0
3542 __ movl(eax, edx);
3543 __ shrl(edx, Immediate(31));
3544 __ addl(edx, eax);
3545
3546 if (instruction->IsRem()) {
3547 __ movl(eax, num);
3548 __ imull(edx, Immediate(imm));
3549 __ subl(eax, edx);
3550 __ movl(edx, eax);
3551 } else {
3552 __ movl(eax, edx);
3553 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003554}
3555
Calin Juravlebacfec32014-11-14 15:54:36 +00003556void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3557 DCHECK(instruction->IsDiv() || instruction->IsRem());
3558
3559 LocationSummary* locations = instruction->GetLocations();
3560 Location out = locations->Out();
3561 Location first = locations->InAt(0);
3562 Location second = locations->InAt(1);
3563 bool is_div = instruction->IsDiv();
3564
3565 switch (instruction->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003566 case DataType::Type::kInt32: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003567 DCHECK_EQ(EAX, first.AsRegister<Register>());
3568 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003569
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003570 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003571 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003572
3573 if (imm == 0) {
3574 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3575 } else if (imm == 1 || imm == -1) {
3576 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003577 } else if (is_div && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003578 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003579 } else {
3580 DCHECK(imm <= -2 || imm >= 2);
3581 GenerateDivRemWithAnyConstant(instruction);
3582 }
3583 } else {
David Srbecky9cd6d372016-02-09 15:24:47 +00003584 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(
3585 instruction, out.AsRegister<Register>(), is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003586 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003587
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003588 Register second_reg = second.AsRegister<Register>();
3589 // 0x80000000/-1 triggers an arithmetic exception!
3590 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3591 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003592
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003593 __ cmpl(second_reg, Immediate(-1));
3594 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003595
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003596 // edx:eax <- sign-extended of eax
3597 __ cdq();
3598 // eax = quotient, edx = remainder
3599 __ idivl(second_reg);
3600 __ Bind(slow_path->GetExitLabel());
3601 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003602 break;
3603 }
3604
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003605 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003606 InvokeRuntimeCallingConvention calling_convention;
3607 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3608 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3609 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3610 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3611 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3612 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3613
3614 if (is_div) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003615 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003616 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003617 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003618 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003619 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003620 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003621 break;
3622 }
3623
3624 default:
3625 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3626 }
3627}
3628
Calin Juravle7c4954d2014-10-28 16:57:40 +00003629void LocationsBuilderX86::VisitDiv(HDiv* div) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003630 LocationSummary::CallKind call_kind = (div->GetResultType() == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003631 ? LocationSummary::kCallOnMainOnly
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003632 : LocationSummary::kNoCall;
3633 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3634
Calin Juravle7c4954d2014-10-28 16:57:40 +00003635 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003636 case DataType::Type::kInt32: {
Calin Juravled0d48522014-11-04 16:40:20 +00003637 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003638 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003639 locations->SetOut(Location::SameAsFirstInput());
3640 // Intel uses edx:eax as the dividend.
3641 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003642 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3643 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3644 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003645 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003646 locations->AddTemp(Location::RequiresRegister());
3647 }
Calin Juravled0d48522014-11-04 16:40:20 +00003648 break;
3649 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003650 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003651 InvokeRuntimeCallingConvention calling_convention;
3652 locations->SetInAt(0, Location::RegisterPairLocation(
3653 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3654 locations->SetInAt(1, Location::RegisterPairLocation(
3655 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3656 // Runtime helper puts the result in EAX, EDX.
3657 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003658 break;
3659 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003660 case DataType::Type::kFloat32:
3661 case DataType::Type::kFloat64: {
Calin Juravle7c4954d2014-10-28 16:57:40 +00003662 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003663 if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3664 DCHECK(div->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003665 } else if (div->InputAt(1)->IsConstant()) {
3666 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003667 } else {
3668 locations->SetInAt(1, Location::Any());
3669 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003670 locations->SetOut(Location::SameAsFirstInput());
3671 break;
3672 }
3673
3674 default:
3675 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3676 }
3677}
3678
3679void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3680 LocationSummary* locations = div->GetLocations();
3681 Location first = locations->InAt(0);
3682 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003683
3684 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003685 case DataType::Type::kInt32:
3686 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003687 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003688 break;
3689 }
3690
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003691 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003692 if (second.IsFpuRegister()) {
3693 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3694 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3695 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003696 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003697 __ divss(first.AsFpuRegister<XmmRegister>(),
3698 codegen_->LiteralFloatAddress(
3699 const_area->GetConstant()->AsFloatConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003700 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003701 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3702 } else {
3703 DCHECK(second.IsStackSlot());
3704 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3705 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003706 break;
3707 }
3708
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003709 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003710 if (second.IsFpuRegister()) {
3711 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3712 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3713 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003714 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003715 __ divsd(first.AsFpuRegister<XmmRegister>(),
3716 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003717 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3718 const_area->GetBaseMethodAddress(),
3719 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003720 } else {
3721 DCHECK(second.IsDoubleStackSlot());
3722 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3723 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003724 break;
3725 }
3726
3727 default:
3728 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3729 }
3730}
3731
Calin Juravlebacfec32014-11-14 15:54:36 +00003732void LocationsBuilderX86::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003733 DataType::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003734
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003735 LocationSummary::CallKind call_kind = (rem->GetResultType() == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003736 ? LocationSummary::kCallOnMainOnly
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003737 : LocationSummary::kNoCall;
3738 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003739
Calin Juravled2ec87d2014-12-08 14:24:46 +00003740 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003741 case DataType::Type::kInt32: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003742 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003743 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003744 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003745 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3746 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3747 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003748 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003749 locations->AddTemp(Location::RequiresRegister());
3750 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003751 break;
3752 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003753 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003754 InvokeRuntimeCallingConvention calling_convention;
3755 locations->SetInAt(0, Location::RegisterPairLocation(
3756 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3757 locations->SetInAt(1, Location::RegisterPairLocation(
3758 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3759 // Runtime helper puts the result in EAX, EDX.
3760 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3761 break;
3762 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003763 case DataType::Type::kFloat64:
3764 case DataType::Type::kFloat32: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003765 locations->SetInAt(0, Location::Any());
3766 locations->SetInAt(1, Location::Any());
3767 locations->SetOut(Location::RequiresFpuRegister());
3768 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003769 break;
3770 }
3771
3772 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003773 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003774 }
3775}
3776
3777void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003778 DataType::Type type = rem->GetResultType();
Calin Juravlebacfec32014-11-14 15:54:36 +00003779 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003780 case DataType::Type::kInt32:
3781 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003782 GenerateDivRemIntegral(rem);
3783 break;
3784 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003785 case DataType::Type::kFloat32:
3786 case DataType::Type::kFloat64: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003787 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003788 break;
3789 }
3790 default:
3791 LOG(FATAL) << "Unexpected rem type " << type;
3792 }
3793}
3794
Calin Juravled0d48522014-11-04 16:40:20 +00003795void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003796 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003797 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003798 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003799 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003800 case DataType::Type::kInt8:
3801 case DataType::Type::kUint16:
3802 case DataType::Type::kInt16:
3803 case DataType::Type::kInt32: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003804 locations->SetInAt(0, Location::Any());
3805 break;
3806 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003807 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003808 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
3809 if (!instruction->IsConstant()) {
3810 locations->AddTemp(Location::RequiresRegister());
3811 }
3812 break;
3813 }
3814 default:
3815 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3816 }
Calin Juravled0d48522014-11-04 16:40:20 +00003817}
3818
3819void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003820 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003821 codegen_->AddSlowPath(slow_path);
3822
3823 LocationSummary* locations = instruction->GetLocations();
3824 Location value = locations->InAt(0);
3825
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003826 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003827 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003828 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003829 case DataType::Type::kInt8:
3830 case DataType::Type::kUint16:
3831 case DataType::Type::kInt16:
3832 case DataType::Type::kInt32: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003833 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003834 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003835 __ j(kEqual, slow_path->GetEntryLabel());
3836 } else if (value.IsStackSlot()) {
3837 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
3838 __ j(kEqual, slow_path->GetEntryLabel());
3839 } else {
3840 DCHECK(value.IsConstant()) << value;
3841 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01003842 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003843 }
3844 }
3845 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003846 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003847 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003848 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003849 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003850 __ movl(temp, value.AsRegisterPairLow<Register>());
3851 __ orl(temp, value.AsRegisterPairHigh<Register>());
3852 __ j(kEqual, slow_path->GetEntryLabel());
3853 } else {
3854 DCHECK(value.IsConstant()) << value;
3855 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3856 __ jmp(slow_path->GetEntryLabel());
3857 }
3858 }
3859 break;
3860 }
3861 default:
3862 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003863 }
Calin Juravled0d48522014-11-04 16:40:20 +00003864}
3865
Calin Juravle9aec02f2014-11-18 23:06:35 +00003866void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3867 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3868
3869 LocationSummary* locations =
3870 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3871
3872 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003873 case DataType::Type::kInt32:
3874 case DataType::Type::kInt64: {
Mark P Mendell73945692015-04-29 14:56:17 +00003875 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003876 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003877 // The shift count needs to be in CL or a constant.
3878 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003879 locations->SetOut(Location::SameAsFirstInput());
3880 break;
3881 }
3882 default:
3883 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3884 }
3885}
3886
3887void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3888 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3889
3890 LocationSummary* locations = op->GetLocations();
3891 Location first = locations->InAt(0);
3892 Location second = locations->InAt(1);
3893 DCHECK(first.Equals(locations->Out()));
3894
3895 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003896 case DataType::Type::kInt32: {
Mark P Mendell73945692015-04-29 14:56:17 +00003897 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003898 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003899 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003900 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003901 DCHECK_EQ(ECX, second_reg);
3902 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003903 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003904 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003905 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003906 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003907 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003908 }
3909 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003910 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003911 if (shift == 0) {
3912 return;
3913 }
3914 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003915 if (op->IsShl()) {
3916 __ shll(first_reg, imm);
3917 } else if (op->IsShr()) {
3918 __ sarl(first_reg, imm);
3919 } else {
3920 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003921 }
3922 }
3923 break;
3924 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003925 case DataType::Type::kInt64: {
Mark P Mendell73945692015-04-29 14:56:17 +00003926 if (second.IsRegister()) {
3927 Register second_reg = second.AsRegister<Register>();
3928 DCHECK_EQ(ECX, second_reg);
3929 if (op->IsShl()) {
3930 GenerateShlLong(first, second_reg);
3931 } else if (op->IsShr()) {
3932 GenerateShrLong(first, second_reg);
3933 } else {
3934 GenerateUShrLong(first, second_reg);
3935 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003936 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003937 // Shift by a constant.
Roland Levillain5b5b9312016-03-22 14:57:31 +00003938 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003939 // Nothing to do if the shift is 0, as the input is already the output.
3940 if (shift != 0) {
3941 if (op->IsShl()) {
3942 GenerateShlLong(first, shift);
3943 } else if (op->IsShr()) {
3944 GenerateShrLong(first, shift);
3945 } else {
3946 GenerateUShrLong(first, shift);
3947 }
3948 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003949 }
3950 break;
3951 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003952 default:
3953 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3954 }
3955}
3956
Mark P Mendell73945692015-04-29 14:56:17 +00003957void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3958 Register low = loc.AsRegisterPairLow<Register>();
3959 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003960 if (shift == 1) {
3961 // This is just an addition.
3962 __ addl(low, low);
3963 __ adcl(high, high);
3964 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003965 // Shift by 32 is easy. High gets low, and low gets 0.
3966 codegen_->EmitParallelMoves(
3967 loc.ToLow(),
3968 loc.ToHigh(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003969 DataType::Type::kInt32,
Mark P Mendell73945692015-04-29 14:56:17 +00003970 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3971 loc.ToLow(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003972 DataType::Type::kInt32);
Mark P Mendell73945692015-04-29 14:56:17 +00003973 } else if (shift > 32) {
3974 // Low part becomes 0. High part is low part << (shift-32).
3975 __ movl(high, low);
3976 __ shll(high, Immediate(shift - 32));
3977 __ xorl(low, low);
3978 } else {
3979 // Between 1 and 31.
3980 __ shld(high, low, Immediate(shift));
3981 __ shll(low, Immediate(shift));
3982 }
3983}
3984
Calin Juravle9aec02f2014-11-18 23:06:35 +00003985void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003986 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003987 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
3988 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
3989 __ testl(shifter, Immediate(32));
3990 __ j(kEqual, &done);
3991 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
3992 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
3993 __ Bind(&done);
3994}
3995
Mark P Mendell73945692015-04-29 14:56:17 +00003996void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
3997 Register low = loc.AsRegisterPairLow<Register>();
3998 Register high = loc.AsRegisterPairHigh<Register>();
3999 if (shift == 32) {
4000 // Need to copy the sign.
4001 DCHECK_NE(low, high);
4002 __ movl(low, high);
4003 __ sarl(high, Immediate(31));
4004 } else if (shift > 32) {
4005 DCHECK_NE(low, high);
4006 // High part becomes sign. Low part is shifted by shift - 32.
4007 __ movl(low, high);
4008 __ sarl(high, Immediate(31));
4009 __ sarl(low, Immediate(shift - 32));
4010 } else {
4011 // Between 1 and 31.
4012 __ shrd(low, high, Immediate(shift));
4013 __ sarl(high, Immediate(shift));
4014 }
4015}
4016
Calin Juravle9aec02f2014-11-18 23:06:35 +00004017void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004018 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004019 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4020 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
4021 __ testl(shifter, Immediate(32));
4022 __ j(kEqual, &done);
4023 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4024 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
4025 __ Bind(&done);
4026}
4027
Mark P Mendell73945692015-04-29 14:56:17 +00004028void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
4029 Register low = loc.AsRegisterPairLow<Register>();
4030 Register high = loc.AsRegisterPairHigh<Register>();
4031 if (shift == 32) {
4032 // Shift by 32 is easy. Low gets high, and high gets 0.
4033 codegen_->EmitParallelMoves(
4034 loc.ToHigh(),
4035 loc.ToLow(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004036 DataType::Type::kInt32,
Mark P Mendell73945692015-04-29 14:56:17 +00004037 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
4038 loc.ToHigh(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004039 DataType::Type::kInt32);
Mark P Mendell73945692015-04-29 14:56:17 +00004040 } else if (shift > 32) {
4041 // Low part is high >> (shift - 32). High part becomes 0.
4042 __ movl(low, high);
4043 __ shrl(low, Immediate(shift - 32));
4044 __ xorl(high, high);
4045 } else {
4046 // Between 1 and 31.
4047 __ shrd(low, high, Immediate(shift));
4048 __ shrl(high, Immediate(shift));
4049 }
4050}
4051
Calin Juravle9aec02f2014-11-18 23:06:35 +00004052void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004053 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004054 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4055 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
4056 __ testl(shifter, Immediate(32));
4057 __ j(kEqual, &done);
4058 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4059 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
4060 __ Bind(&done);
4061}
4062
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004063void LocationsBuilderX86::VisitRor(HRor* ror) {
4064 LocationSummary* locations =
4065 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
4066
4067 switch (ror->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004068 case DataType::Type::kInt64:
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004069 // Add the temporary needed.
4070 locations->AddTemp(Location::RequiresRegister());
4071 FALLTHROUGH_INTENDED;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004072 case DataType::Type::kInt32:
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004073 locations->SetInAt(0, Location::RequiresRegister());
4074 // The shift count needs to be in CL (unless it is a constant).
4075 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
4076 locations->SetOut(Location::SameAsFirstInput());
4077 break;
4078 default:
4079 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4080 UNREACHABLE();
4081 }
4082}
4083
4084void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
4085 LocationSummary* locations = ror->GetLocations();
4086 Location first = locations->InAt(0);
4087 Location second = locations->InAt(1);
4088
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004089 if (ror->GetResultType() == DataType::Type::kInt32) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004090 Register first_reg = first.AsRegister<Register>();
4091 if (second.IsRegister()) {
4092 Register second_reg = second.AsRegister<Register>();
4093 __ rorl(first_reg, second_reg);
4094 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004095 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004096 __ rorl(first_reg, imm);
4097 }
4098 return;
4099 }
4100
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004101 DCHECK_EQ(ror->GetResultType(), DataType::Type::kInt64);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004102 Register first_reg_lo = first.AsRegisterPairLow<Register>();
4103 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
4104 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
4105 if (second.IsRegister()) {
4106 Register second_reg = second.AsRegister<Register>();
4107 DCHECK_EQ(second_reg, ECX);
4108 __ movl(temp_reg, first_reg_hi);
4109 __ shrd(first_reg_hi, first_reg_lo, second_reg);
4110 __ shrd(first_reg_lo, temp_reg, second_reg);
4111 __ movl(temp_reg, first_reg_hi);
4112 __ testl(second_reg, Immediate(32));
4113 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
4114 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
4115 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004116 int32_t shift_amt = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004117 if (shift_amt == 0) {
4118 // Already fine.
4119 return;
4120 }
4121 if (shift_amt == 32) {
4122 // Just swap.
4123 __ movl(temp_reg, first_reg_lo);
4124 __ movl(first_reg_lo, first_reg_hi);
4125 __ movl(first_reg_hi, temp_reg);
4126 return;
4127 }
4128
4129 Immediate imm(shift_amt);
4130 // Save the constents of the low value.
4131 __ movl(temp_reg, first_reg_lo);
4132
4133 // Shift right into low, feeding bits from high.
4134 __ shrd(first_reg_lo, first_reg_hi, imm);
4135
4136 // Shift right into high, feeding bits from the original low.
4137 __ shrd(first_reg_hi, temp_reg, imm);
4138
4139 // Swap if needed.
4140 if (shift_amt > 32) {
4141 __ movl(temp_reg, first_reg_lo);
4142 __ movl(first_reg_lo, first_reg_hi);
4143 __ movl(first_reg_hi, temp_reg);
4144 }
4145 }
4146}
4147
Calin Juravle9aec02f2014-11-18 23:06:35 +00004148void LocationsBuilderX86::VisitShl(HShl* shl) {
4149 HandleShift(shl);
4150}
4151
4152void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
4153 HandleShift(shl);
4154}
4155
4156void LocationsBuilderX86::VisitShr(HShr* shr) {
4157 HandleShift(shr);
4158}
4159
4160void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
4161 HandleShift(shr);
4162}
4163
4164void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
4165 HandleShift(ushr);
4166}
4167
4168void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
4169 HandleShift(ushr);
4170}
4171
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004172void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004173 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004174 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004175 locations->SetOut(Location::RegisterLocation(EAX));
David Brazdil6de19382016-01-08 17:37:10 +00004176 if (instruction->IsStringAlloc()) {
4177 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4178 } else {
4179 InvokeRuntimeCallingConvention calling_convention;
4180 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00004181 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004182}
4183
4184void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004185 // Note: if heap poisoning is enabled, the entry point takes cares
4186 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004187 if (instruction->IsStringAlloc()) {
4188 // String is allocated through StringFactory. Call NewEmptyString entry point.
4189 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07004190 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004191 __ fs()->movl(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString)));
4192 __ call(Address(temp, code_offset.Int32Value()));
4193 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4194 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01004195 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00004196 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00004197 DCHECK(!codegen_->IsLeafMethod());
4198 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004199}
4200
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004201void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
4202 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004203 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004204 locations->SetOut(Location::RegisterLocation(EAX));
4205 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004206 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4207 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004208}
4209
4210void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004211 // Note: if heap poisoning is enabled, the entry point takes cares
4212 // of poisoning the reference.
Nicolas Geoffrayd0958442017-01-30 14:57:16 +00004213 QuickEntrypointEnum entrypoint =
4214 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
4215 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004216 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004217 DCHECK(!codegen_->IsLeafMethod());
4218}
4219
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004220void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004221 LocationSummary* locations =
4222 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004223 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4224 if (location.IsStackSlot()) {
4225 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4226 } else if (location.IsDoubleStackSlot()) {
4227 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004228 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004229 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004230}
4231
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004232void InstructionCodeGeneratorX86::VisitParameterValue(
4233 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4234}
4235
4236void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
4237 LocationSummary* locations =
4238 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4239 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4240}
4241
4242void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004243}
4244
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004245void LocationsBuilderX86::VisitClassTableGet(HClassTableGet* instruction) {
4246 LocationSummary* locations =
4247 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4248 locations->SetInAt(0, Location::RequiresRegister());
4249 locations->SetOut(Location::RequiresRegister());
4250}
4251
4252void InstructionCodeGeneratorX86::VisitClassTableGet(HClassTableGet* instruction) {
4253 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004254 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004255 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004256 instruction->GetIndex(), kX86PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004257 __ movl(locations->Out().AsRegister<Register>(),
4258 Address(locations->InAt(0).AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004259 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004260 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004261 instruction->GetIndex(), kX86PointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004262 __ movl(locations->Out().AsRegister<Register>(),
4263 Address(locations->InAt(0).AsRegister<Register>(),
4264 mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
4265 // temp = temp->GetImtEntryAt(method_offset);
4266 __ movl(locations->Out().AsRegister<Register>(),
4267 Address(locations->Out().AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004268 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004269}
4270
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004271void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004272 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004273 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004274 locations->SetInAt(0, Location::RequiresRegister());
4275 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004276}
4277
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004278void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
4279 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01004280 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004281 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01004282 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004283 switch (not_->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004284 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004285 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004286 break;
4287
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004288 case DataType::Type::kInt64:
Roland Levillain70566432014-10-24 16:20:17 +01004289 __ notl(out.AsRegisterPairLow<Register>());
4290 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004291 break;
4292
4293 default:
4294 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4295 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004296}
4297
David Brazdil66d126e2015-04-03 16:02:44 +01004298void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
4299 LocationSummary* locations =
4300 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4301 locations->SetInAt(0, Location::RequiresRegister());
4302 locations->SetOut(Location::SameAsFirstInput());
4303}
4304
4305void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004306 LocationSummary* locations = bool_not->GetLocations();
4307 Location in = locations->InAt(0);
4308 Location out = locations->Out();
4309 DCHECK(in.Equals(out));
4310 __ xorl(out.AsRegister<Register>(), Immediate(1));
4311}
4312
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004313void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004314 LocationSummary* locations =
4315 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004316 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004317 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004318 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004319 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004320 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004321 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004322 case DataType::Type::kInt32:
4323 case DataType::Type::kInt64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00004324 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00004325 locations->SetInAt(1, Location::Any());
4326 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4327 break;
4328 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004329 case DataType::Type::kFloat32:
4330 case DataType::Type::kFloat64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00004331 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004332 if (compare->InputAt(1)->IsX86LoadFromConstantTable()) {
4333 DCHECK(compare->InputAt(1)->IsEmittedAtUseSite());
4334 } else if (compare->InputAt(1)->IsConstant()) {
4335 locations->SetInAt(1, Location::RequiresFpuRegister());
4336 } else {
4337 locations->SetInAt(1, Location::Any());
4338 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004339 locations->SetOut(Location::RequiresRegister());
4340 break;
4341 }
4342 default:
4343 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4344 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004345}
4346
4347void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004348 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004349 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004350 Location left = locations->InAt(0);
4351 Location right = locations->InAt(1);
4352
Mark Mendell0c9497d2015-08-21 09:30:05 -04004353 NearLabel less, greater, done;
Aart Bika19616e2016-02-01 18:57:58 -08004354 Condition less_cond = kLess;
4355
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004356 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004357 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004358 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004359 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004360 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004361 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004362 case DataType::Type::kInt32: {
Roland Levillain0b671c02016-08-19 12:02:34 +01004363 codegen_->GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08004364 break;
4365 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004366 case DataType::Type::kInt64: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004367 Register left_low = left.AsRegisterPairLow<Register>();
4368 Register left_high = left.AsRegisterPairHigh<Register>();
4369 int32_t val_low = 0;
4370 int32_t val_high = 0;
4371 bool right_is_const = false;
4372
4373 if (right.IsConstant()) {
4374 DCHECK(right.GetConstant()->IsLongConstant());
4375 right_is_const = true;
4376 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
4377 val_low = Low32Bits(val);
4378 val_high = High32Bits(val);
4379 }
4380
Calin Juravleddb7df22014-11-25 20:56:51 +00004381 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004382 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004383 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004384 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004385 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004386 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004387 codegen_->Compare32BitValue(left_high, val_high);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004388 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004389 __ j(kLess, &less); // Signed compare.
4390 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004391 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004392 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004393 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004394 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004395 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004396 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004397 codegen_->Compare32BitValue(left_low, val_low);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004398 }
Aart Bika19616e2016-02-01 18:57:58 -08004399 less_cond = kBelow; // for CF (unsigned).
Calin Juravleddb7df22014-11-25 20:56:51 +00004400 break;
4401 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004402 case DataType::Type::kFloat32: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004403 GenerateFPCompare(left, right, compare, false);
Calin Juravleddb7df22014-11-25 20:56:51 +00004404 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004405 less_cond = kBelow; // for CF (floats).
Calin Juravleddb7df22014-11-25 20:56:51 +00004406 break;
4407 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004408 case DataType::Type::kFloat64: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004409 GenerateFPCompare(left, right, compare, true);
Calin Juravleddb7df22014-11-25 20:56:51 +00004410 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004411 less_cond = kBelow; // for CF (floats).
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004412 break;
4413 }
4414 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004415 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004416 }
Aart Bika19616e2016-02-01 18:57:58 -08004417
Calin Juravleddb7df22014-11-25 20:56:51 +00004418 __ movl(out, Immediate(0));
4419 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08004420 __ j(less_cond, &less);
Calin Juravleddb7df22014-11-25 20:56:51 +00004421
4422 __ Bind(&greater);
4423 __ movl(out, Immediate(1));
4424 __ jmp(&done);
4425
4426 __ Bind(&less);
4427 __ movl(out, Immediate(-1));
4428
4429 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004430}
4431
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004432void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004433 LocationSummary* locations =
4434 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004435 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004436 locations->SetInAt(i, Location::Any());
4437 }
4438 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004439}
4440
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004441void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004442 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004443}
4444
Roland Levillain7c1559a2015-12-15 10:55:36 +00004445void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004446 /*
4447 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
4448 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
4449 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4450 */
4451 switch (kind) {
4452 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004453 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004454 break;
4455 }
4456 case MemBarrierKind::kAnyStore:
4457 case MemBarrierKind::kLoadAny:
4458 case MemBarrierKind::kStoreStore: {
4459 // nop
4460 break;
4461 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004462 case MemBarrierKind::kNTStoreStore:
4463 // Non-Temporal Store/Store needs an explicit fence.
4464 MemoryFence(/* non-temporal */ true);
4465 break;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004466 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004467}
4468
Vladimir Markodc151b22015-10-15 18:02:30 +01004469HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
4470 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004471 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004472 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004473}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004474
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004475Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4476 Register temp) {
4477 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Markoc53c0792015-11-19 15:48:33 +00004478 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004479 if (!invoke->GetLocations()->Intrinsified()) {
4480 return location.AsRegister<Register>();
4481 }
4482 // For intrinsics we allow any location, so it may be on the stack.
4483 if (!location.IsRegister()) {
4484 __ movl(temp, Address(ESP, location.GetStackIndex()));
4485 return temp;
4486 }
4487 // For register locations, check if the register was saved. If so, get it from the stack.
4488 // Note: There is a chance that the register was saved but not overwritten, so we could
4489 // save one load. However, since this is just an intrinsic slow path we prefer this
4490 // simple and more robust approach rather that trying to determine if that's the case.
4491 SlowPathCode* slow_path = GetCurrentSlowPath();
Vladimir Marko4ee8e292017-06-02 15:39:30 +00004492 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
4493 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4494 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4495 __ movl(temp, Address(ESP, stack_offset));
4496 return temp;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004497 }
4498 return location.AsRegister<Register>();
4499}
4500
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004501void CodeGeneratorX86::GenerateStaticOrDirectCall(
4502 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Vladimir Marko58155012015-08-19 12:49:41 +00004503 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4504 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004505 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00004506 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004507 uint32_t offset =
4508 GetThreadOffset<kX86PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
4509 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004510 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004511 }
Vladimir Marko58155012015-08-19 12:49:41 +00004512 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004513 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004514 break;
Vladimir Marko65979462017-05-19 17:25:12 +01004515 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
4516 DCHECK(GetCompilerOptions().IsBootImage());
4517 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4518 temp.AsRegister<Register>());
4519 __ leal(temp.AsRegister<Register>(), Address(base_reg, CodeGeneratorX86::kDummy32BitOffset));
4520 RecordBootMethodPatch(invoke);
4521 break;
4522 }
Vladimir Marko58155012015-08-19 12:49:41 +00004523 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4524 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
4525 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004526 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004527 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4528 temp.AsRegister<Register>());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004529 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004530 // Bind a new fixup label at the end of the "movl" insn.
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004531 __ Bind(NewMethodBssEntryPatch(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004532 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress(),
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004533 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex())));
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004534 break;
4535 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004536 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
4537 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
4538 return; // No code pointer retrieval; the runtime performs the call directly.
Vladimir Marko9b688a02015-05-06 14:12:42 +01004539 }
Vladimir Marko58155012015-08-19 12:49:41 +00004540 }
4541
4542 switch (invoke->GetCodePtrLocation()) {
4543 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4544 __ call(GetFrameEntryLabel());
4545 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004546 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4547 // (callee_method + offset_of_quick_compiled_code)()
4548 __ call(Address(callee_method.AsRegister<Register>(),
4549 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07004550 kX86PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004551 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04004552 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004553 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Mark Mendell09ed1a32015-03-25 08:30:06 -04004554
4555 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04004556}
4557
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004558void CodeGeneratorX86::GenerateVirtualCall(
4559 HInvokeVirtual* invoke, Location temp_in, SlowPathCode* slow_path) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004560 Register temp = temp_in.AsRegister<Register>();
4561 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4562 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004563
4564 // Use the calling convention instead of the location of the receiver, as
4565 // intrinsics may have put the receiver in a different register. In the intrinsics
4566 // slow path, the arguments have been moved to the right place, so here we are
4567 // guaranteed that the receiver is the first register of the calling convention.
4568 InvokeDexCallingConvention calling_convention;
4569 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004570 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004571 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004572 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004573 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004574 // Instead of simply (possibly) unpoisoning `temp` here, we should
4575 // emit a read barrier for the previous class reference load.
4576 // However this is not required in practice, as this is an
4577 // intermediate/temporary reference and because the current
4578 // concurrent copying collector keeps the from-space memory
4579 // intact/accessible until the end of the marking phase (the
4580 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004581 __ MaybeUnpoisonHeapReference(temp);
4582 // temp = temp->GetMethodAt(method_offset);
4583 __ movl(temp, Address(temp, method_offset));
4584 // call temp->GetEntryPoint();
4585 __ call(Address(
Andreas Gampe542451c2016-07-26 09:02:02 -07004586 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004587 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004588}
4589
Vladimir Marko65979462017-05-19 17:25:12 +01004590void CodeGeneratorX86::RecordBootMethodPatch(HInvokeStaticOrDirect* invoke) {
4591 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
4592 HX86ComputeBaseMethodAddress* address =
4593 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
4594 boot_image_method_patches_.emplace_back(address,
4595 *invoke->GetTargetMethod().dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07004596 invoke->GetTargetMethod().index);
Vladimir Marko65979462017-05-19 17:25:12 +01004597 __ Bind(&boot_image_method_patches_.back().label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004598}
4599
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004600Label* CodeGeneratorX86::NewMethodBssEntryPatch(
4601 HX86ComputeBaseMethodAddress* method_address,
4602 MethodReference target_method) {
4603 // Add the patch entry and bind its label at the end of the instruction.
4604 method_bss_entry_patches_.emplace_back(method_address,
4605 *target_method.dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07004606 target_method.index);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004607 return &method_bss_entry_patches_.back().label;
4608}
4609
Vladimir Marko1998cd02017-01-13 13:02:58 +00004610void CodeGeneratorX86::RecordBootTypePatch(HLoadClass* load_class) {
Vladimir Marko00916b92017-05-17 17:45:45 +01004611 HX86ComputeBaseMethodAddress* address = load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004612 boot_image_type_patches_.emplace_back(address,
4613 load_class->GetDexFile(),
Vladimir Marko1998cd02017-01-13 13:02:58 +00004614 load_class->GetTypeIndex().index_);
4615 __ Bind(&boot_image_type_patches_.back().label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004616}
4617
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004618Label* CodeGeneratorX86::NewTypeBssEntryPatch(HLoadClass* load_class) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004619 HX86ComputeBaseMethodAddress* address =
4620 load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
4621 type_bss_entry_patches_.emplace_back(
4622 address, load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00004623 return &type_bss_entry_patches_.back().label;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004624}
4625
Vladimir Marko65979462017-05-19 17:25:12 +01004626void CodeGeneratorX86::RecordBootStringPatch(HLoadString* load_string) {
Vladimir Marko65979462017-05-19 17:25:12 +01004627 HX86ComputeBaseMethodAddress* address = load_string->InputAt(0)->AsX86ComputeBaseMethodAddress();
4628 string_patches_.emplace_back(address,
4629 load_string->GetDexFile(),
4630 load_string->GetStringIndex().index_);
4631 __ Bind(&string_patches_.back().label);
4632}
4633
Vladimir Markoaad75c62016-10-03 08:46:48 +00004634Label* CodeGeneratorX86::NewStringBssEntryPatch(HLoadString* load_string) {
4635 DCHECK(!GetCompilerOptions().IsBootImage());
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004636 HX86ComputeBaseMethodAddress* address =
4637 load_string->InputAt(0)->AsX86ComputeBaseMethodAddress();
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004638 string_bss_entry_patches_.emplace_back(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004639 address, load_string->GetDexFile(), load_string->GetStringIndex().index_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004640 return &string_bss_entry_patches_.back().label;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004641}
4642
Vladimir Markoaad75c62016-10-03 08:46:48 +00004643// The label points to the end of the "movl" or another instruction but the literal offset
4644// for method patch needs to point to the embedded constant which occupies the last 4 bytes.
4645constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
4646
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004647template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +00004648inline void CodeGeneratorX86::EmitPcRelativeLinkerPatches(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004649 const ArenaDeque<X86PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004650 ArenaVector<linker::LinkerPatch>* linker_patches) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004651 for (const X86PcRelativePatchInfo& info : infos) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00004652 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004653 linker_patches->push_back(Factory(
4654 literal_offset, &info.dex_file, GetMethodAddressOffset(info.method_address), info.index));
Vladimir Markoaad75c62016-10-03 08:46:48 +00004655 }
4656}
4657
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004658void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Marko58155012015-08-19 12:49:41 +00004659 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004660 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01004661 boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004662 method_bss_entry_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +00004663 boot_image_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01004664 type_bss_entry_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004665 string_patches_.size() +
4666 string_bss_entry_patches_.size();
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004667 linker_patches->reserve(size);
Vladimir Marko764d4542017-05-16 10:31:41 +01004668 if (GetCompilerOptions().IsBootImage()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004669 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
4670 boot_image_method_patches_, linker_patches);
4671 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
4672 boot_image_type_patches_, linker_patches);
4673 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
4674 string_patches_, linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004675 } else {
Vladimir Marko65979462017-05-19 17:25:12 +01004676 DCHECK(boot_image_method_patches_.empty());
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004677 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeClassTablePatch>(
4678 boot_image_type_patches_, linker_patches);
4679 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringInternTablePatch>(
4680 string_patches_, linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004681 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01004682 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
4683 method_bss_entry_patches_, linker_patches);
4684 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
4685 type_bss_entry_patches_, linker_patches);
4686 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
4687 string_bss_entry_patches_, linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00004688 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00004689}
4690
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004691void CodeGeneratorX86::MarkGCCard(Register temp,
4692 Register card,
4693 Register object,
4694 Register value,
4695 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004696 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004697 if (value_can_be_null) {
4698 __ testl(value, value);
4699 __ j(kEqual, &is_null);
4700 }
Andreas Gampe542451c2016-07-26 09:02:02 -07004701 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86PointerSize>().Int32Value()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004702 __ movl(temp, object);
4703 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004704 __ movb(Address(temp, card, TIMES_1, 0),
4705 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004706 if (value_can_be_null) {
4707 __ Bind(&is_null);
4708 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004709}
4710
Calin Juravle52c48962014-12-16 17:02:57 +00004711void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4712 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00004713
4714 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004715 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004716 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004717 new (GetGraph()->GetArena()) LocationSummary(instruction,
4718 kEmitCompilerReadBarrier ?
4719 LocationSummary::kCallOnSlowPath :
4720 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004721 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004722 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004723 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004724 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004725
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004726 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004727 locations->SetOut(Location::RequiresFpuRegister());
4728 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004729 // The output overlaps in case of long: we don't want the low move
4730 // to overwrite the object's location. Likewise, in the case of
4731 // an object field get with read barriers enabled, we do not want
4732 // the move to overwrite the object's location, as we need it to emit
4733 // the read barrier.
4734 locations->SetOut(
4735 Location::RequiresRegister(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004736 (object_field_get_with_read_barrier || instruction->GetType() == DataType::Type::kInt64) ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00004737 Location::kOutputOverlap :
4738 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004739 }
Calin Juravle52c48962014-12-16 17:02:57 +00004740
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004741 if (field_info.IsVolatile() && (field_info.GetFieldType() == DataType::Type::kInt64)) {
Calin Juravle52c48962014-12-16 17:02:57 +00004742 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00004743 // So we use an XMM register as a temp to achieve atomicity (first
4744 // load the temp into the XMM and then copy the XMM into the
4745 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00004746 locations->AddTemp(Location::RequiresFpuRegister());
4747 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004748}
4749
Calin Juravle52c48962014-12-16 17:02:57 +00004750void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
4751 const FieldInfo& field_info) {
4752 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004753
Calin Juravle52c48962014-12-16 17:02:57 +00004754 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004755 Location base_loc = locations->InAt(0);
4756 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004757 Location out = locations->Out();
4758 bool is_volatile = field_info.IsVolatile();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004759 DataType::Type field_type = field_info.GetFieldType();
Calin Juravle52c48962014-12-16 17:02:57 +00004760 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4761
4762 switch (field_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004763 case DataType::Type::kBool:
4764 case DataType::Type::kUint8: {
Calin Juravle52c48962014-12-16 17:02:57 +00004765 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004766 break;
4767 }
4768
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004769 case DataType::Type::kInt8: {
Calin Juravle52c48962014-12-16 17:02:57 +00004770 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004771 break;
4772 }
4773
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004774 case DataType::Type::kUint16: {
4775 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004776 break;
4777 }
4778
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004779 case DataType::Type::kInt16: {
4780 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004781 break;
4782 }
4783
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004784 case DataType::Type::kInt32:
Calin Juravle52c48962014-12-16 17:02:57 +00004785 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004786 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00004787
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004788 case DataType::Type::kReference: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004789 // /* HeapReference<Object> */ out = *(base + offset)
4790 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004791 // Note that a potential implicit null check is handled in this
4792 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4793 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00004794 instruction, out, base, offset, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00004795 if (is_volatile) {
4796 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4797 }
4798 } else {
4799 __ movl(out.AsRegister<Register>(), Address(base, offset));
4800 codegen_->MaybeRecordImplicitNullCheck(instruction);
4801 if (is_volatile) {
4802 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4803 }
4804 // If read barriers are enabled, emit read barriers other than
4805 // Baker's using a slow path (and also unpoison the loaded
4806 // reference, if heap poisoning is enabled).
4807 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4808 }
4809 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004810 }
4811
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004812 case DataType::Type::kInt64: {
Calin Juravle52c48962014-12-16 17:02:57 +00004813 if (is_volatile) {
4814 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4815 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004816 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004817 __ movd(out.AsRegisterPairLow<Register>(), temp);
4818 __ psrlq(temp, Immediate(32));
4819 __ movd(out.AsRegisterPairHigh<Register>(), temp);
4820 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004821 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00004822 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004823 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004824 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
4825 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004826 break;
4827 }
4828
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004829 case DataType::Type::kFloat32: {
Calin Juravle52c48962014-12-16 17:02:57 +00004830 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004831 break;
4832 }
4833
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004834 case DataType::Type::kFloat64: {
Calin Juravle52c48962014-12-16 17:02:57 +00004835 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004836 break;
4837 }
4838
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004839 case DataType::Type::kVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004840 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004841 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004842 }
Calin Juravle52c48962014-12-16 17:02:57 +00004843
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004844 if (field_type == DataType::Type::kReference || field_type == DataType::Type::kInt64) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004845 // Potential implicit null checks, in the case of reference or
4846 // long fields, are handled in the previous switch statement.
4847 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004848 codegen_->MaybeRecordImplicitNullCheck(instruction);
4849 }
4850
Calin Juravle52c48962014-12-16 17:02:57 +00004851 if (is_volatile) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004852 if (field_type == DataType::Type::kReference) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004853 // Memory barriers, in the case of references, are also handled
4854 // in the previous switch statement.
4855 } else {
4856 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4857 }
Roland Levillain4d027112015-07-01 15:41:14 +01004858 }
Calin Juravle52c48962014-12-16 17:02:57 +00004859}
4860
4861void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4862 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4863
4864 LocationSummary* locations =
4865 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4866 locations->SetInAt(0, Location::RequiresRegister());
4867 bool is_volatile = field_info.IsVolatile();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004868 DataType::Type field_type = field_info.GetFieldType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004869 bool is_byte_type = DataType::Size(field_type) == 1u;
Calin Juravle52c48962014-12-16 17:02:57 +00004870
4871 // The register allocator does not support multiple
4872 // inputs that die at entry with one in a specific register.
4873 if (is_byte_type) {
4874 // Ensure the value is in a byte register.
4875 locations->SetInAt(1, Location::RegisterLocation(EAX));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004876 } else if (DataType::IsFloatingPointType(field_type)) {
4877 if (is_volatile && field_type == DataType::Type::kFloat64) {
Mark Mendell81489372015-11-04 11:30:41 -05004878 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4879 locations->SetInAt(1, Location::RequiresFpuRegister());
4880 } else {
4881 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4882 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004883 } else if (is_volatile && field_type == DataType::Type::kInt64) {
Mark Mendell81489372015-11-04 11:30:41 -05004884 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00004885 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004886
Calin Juravle52c48962014-12-16 17:02:57 +00004887 // 64bits value can be atomically written to an address with movsd and an XMM register.
4888 // We need two XMM registers because there's no easier way to (bit) copy a register pair
4889 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
4890 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
4891 // isolated cases when we need this it isn't worth adding the extra complexity.
4892 locations->AddTemp(Location::RequiresFpuRegister());
4893 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004894 } else {
4895 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4896
4897 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4898 // Temporary registers for the write barrier.
4899 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
4900 // Ensure the card is in a byte register.
4901 locations->AddTemp(Location::RegisterLocation(ECX));
4902 }
Calin Juravle52c48962014-12-16 17:02:57 +00004903 }
4904}
4905
4906void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004907 const FieldInfo& field_info,
4908 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004909 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4910
4911 LocationSummary* locations = instruction->GetLocations();
4912 Register base = locations->InAt(0).AsRegister<Register>();
4913 Location value = locations->InAt(1);
4914 bool is_volatile = field_info.IsVolatile();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004915 DataType::Type field_type = field_info.GetFieldType();
Calin Juravle52c48962014-12-16 17:02:57 +00004916 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004917 bool needs_write_barrier =
4918 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004919
4920 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004921 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004922 }
4923
Mark Mendell81489372015-11-04 11:30:41 -05004924 bool maybe_record_implicit_null_check_done = false;
4925
Calin Juravle52c48962014-12-16 17:02:57 +00004926 switch (field_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004927 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004928 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004929 case DataType::Type::kInt8: {
Calin Juravle52c48962014-12-16 17:02:57 +00004930 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
4931 break;
4932 }
4933
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004934 case DataType::Type::kUint16:
4935 case DataType::Type::kInt16: {
Mark Mendell81489372015-11-04 11:30:41 -05004936 if (value.IsConstant()) {
Nicolas Geoffray78612082017-07-24 14:18:53 +01004937 __ movw(Address(base, offset),
4938 Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Mark Mendell81489372015-11-04 11:30:41 -05004939 } else {
4940 __ movw(Address(base, offset), value.AsRegister<Register>());
4941 }
Calin Juravle52c48962014-12-16 17:02:57 +00004942 break;
4943 }
4944
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004945 case DataType::Type::kInt32:
4946 case DataType::Type::kReference: {
Roland Levillain4d027112015-07-01 15:41:14 +01004947 if (kPoisonHeapReferences && needs_write_barrier) {
4948 // Note that in the case where `value` is a null reference,
4949 // we do not enter this block, as the reference does not
4950 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004951 DCHECK_EQ(field_type, DataType::Type::kReference);
Roland Levillain4d027112015-07-01 15:41:14 +01004952 Register temp = locations->GetTemp(0).AsRegister<Register>();
4953 __ movl(temp, value.AsRegister<Register>());
4954 __ PoisonHeapReference(temp);
4955 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05004956 } else if (value.IsConstant()) {
4957 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4958 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01004959 } else {
Nicolas Geoffray03971632016-03-17 10:44:24 +00004960 DCHECK(value.IsRegister()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01004961 __ movl(Address(base, offset), value.AsRegister<Register>());
4962 }
Calin Juravle52c48962014-12-16 17:02:57 +00004963 break;
4964 }
4965
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004966 case DataType::Type::kInt64: {
Calin Juravle52c48962014-12-16 17:02:57 +00004967 if (is_volatile) {
4968 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4969 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
4970 __ movd(temp1, value.AsRegisterPairLow<Register>());
4971 __ movd(temp2, value.AsRegisterPairHigh<Register>());
4972 __ punpckldq(temp1, temp2);
4973 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00004974 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05004975 } else if (value.IsConstant()) {
4976 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4977 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4978 codegen_->MaybeRecordImplicitNullCheck(instruction);
4979 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00004980 } else {
4981 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004982 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004983 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
4984 }
Mark Mendell81489372015-11-04 11:30:41 -05004985 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00004986 break;
4987 }
4988
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004989 case DataType::Type::kFloat32: {
Mark Mendell81489372015-11-04 11:30:41 -05004990 if (value.IsConstant()) {
4991 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4992 __ movl(Address(base, offset), Immediate(v));
4993 } else {
4994 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4995 }
Calin Juravle52c48962014-12-16 17:02:57 +00004996 break;
4997 }
4998
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004999 case DataType::Type::kFloat64: {
Mark Mendell81489372015-11-04 11:30:41 -05005000 if (value.IsConstant()) {
5001 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
5002 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
5003 codegen_->MaybeRecordImplicitNullCheck(instruction);
5004 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
5005 maybe_record_implicit_null_check_done = true;
5006 } else {
5007 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
5008 }
Calin Juravle52c48962014-12-16 17:02:57 +00005009 break;
5010 }
5011
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005012 case DataType::Type::kVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00005013 LOG(FATAL) << "Unreachable type " << field_type;
5014 UNREACHABLE();
5015 }
5016
Mark Mendell81489372015-11-04 11:30:41 -05005017 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005018 codegen_->MaybeRecordImplicitNullCheck(instruction);
5019 }
5020
Roland Levillain4d027112015-07-01 15:41:14 +01005021 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005022 Register temp = locations->GetTemp(0).AsRegister<Register>();
5023 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005024 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00005025 }
5026
Calin Juravle52c48962014-12-16 17:02:57 +00005027 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005028 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00005029 }
5030}
5031
5032void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5033 HandleFieldGet(instruction, instruction->GetFieldInfo());
5034}
5035
5036void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5037 HandleFieldGet(instruction, instruction->GetFieldInfo());
5038}
5039
5040void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5041 HandleFieldSet(instruction, instruction->GetFieldInfo());
5042}
5043
5044void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005045 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005046}
5047
5048void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5049 HandleFieldSet(instruction, instruction->GetFieldInfo());
5050}
5051
5052void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005053 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005054}
5055
5056void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5057 HandleFieldGet(instruction, instruction->GetFieldInfo());
5058}
5059
5060void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5061 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005062}
5063
Calin Juravlee460d1d2015-09-29 04:52:17 +01005064void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
5065 HUnresolvedInstanceFieldGet* instruction) {
5066 FieldAccessCallingConventionX86 calling_convention;
5067 codegen_->CreateUnresolvedFieldLocationSummary(
5068 instruction, instruction->GetFieldType(), calling_convention);
5069}
5070
5071void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
5072 HUnresolvedInstanceFieldGet* instruction) {
5073 FieldAccessCallingConventionX86 calling_convention;
5074 codegen_->GenerateUnresolvedFieldAccess(instruction,
5075 instruction->GetFieldType(),
5076 instruction->GetFieldIndex(),
5077 instruction->GetDexPc(),
5078 calling_convention);
5079}
5080
5081void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
5082 HUnresolvedInstanceFieldSet* instruction) {
5083 FieldAccessCallingConventionX86 calling_convention;
5084 codegen_->CreateUnresolvedFieldLocationSummary(
5085 instruction, instruction->GetFieldType(), calling_convention);
5086}
5087
5088void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
5089 HUnresolvedInstanceFieldSet* instruction) {
5090 FieldAccessCallingConventionX86 calling_convention;
5091 codegen_->GenerateUnresolvedFieldAccess(instruction,
5092 instruction->GetFieldType(),
5093 instruction->GetFieldIndex(),
5094 instruction->GetDexPc(),
5095 calling_convention);
5096}
5097
5098void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
5099 HUnresolvedStaticFieldGet* instruction) {
5100 FieldAccessCallingConventionX86 calling_convention;
5101 codegen_->CreateUnresolvedFieldLocationSummary(
5102 instruction, instruction->GetFieldType(), calling_convention);
5103}
5104
5105void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
5106 HUnresolvedStaticFieldGet* instruction) {
5107 FieldAccessCallingConventionX86 calling_convention;
5108 codegen_->GenerateUnresolvedFieldAccess(instruction,
5109 instruction->GetFieldType(),
5110 instruction->GetFieldIndex(),
5111 instruction->GetDexPc(),
5112 calling_convention);
5113}
5114
5115void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
5116 HUnresolvedStaticFieldSet* instruction) {
5117 FieldAccessCallingConventionX86 calling_convention;
5118 codegen_->CreateUnresolvedFieldLocationSummary(
5119 instruction, instruction->GetFieldType(), calling_convention);
5120}
5121
5122void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
5123 HUnresolvedStaticFieldSet* instruction) {
5124 FieldAccessCallingConventionX86 calling_convention;
5125 codegen_->GenerateUnresolvedFieldAccess(instruction,
5126 instruction->GetFieldType(),
5127 instruction->GetFieldIndex(),
5128 instruction->GetDexPc(),
5129 calling_convention);
5130}
5131
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005132void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005133 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5134 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
5135 ? Location::RequiresRegister()
5136 : Location::Any();
5137 locations->SetInAt(0, loc);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005138}
5139
Calin Juravle2ae48182016-03-16 14:05:09 +00005140void CodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
5141 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005142 return;
5143 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005144 LocationSummary* locations = instruction->GetLocations();
5145 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005146
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005147 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00005148 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005149}
5150
Calin Juravle2ae48182016-03-16 14:05:09 +00005151void CodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07005152 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005153 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005154
5155 LocationSummary* locations = instruction->GetLocations();
5156 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005157
5158 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04005159 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005160 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005161 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005162 } else {
5163 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00005164 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005165 __ jmp(slow_path->GetEntryLabel());
5166 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005167 }
5168 __ j(kEqual, slow_path->GetEntryLabel());
5169}
5170
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005171void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005172 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005173}
5174
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005175void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005176 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005177 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005178 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00005179 new (GetGraph()->GetArena()) LocationSummary(instruction,
5180 object_array_get_with_read_barrier ?
5181 LocationSummary::kCallOnSlowPath :
5182 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005183 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005184 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005185 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005186 locations->SetInAt(0, Location::RequiresRegister());
5187 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005188 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005189 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5190 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005191 // The output overlaps in case of long: we don't want the low move
5192 // to overwrite the array's location. Likewise, in the case of an
5193 // object array get with read barriers enabled, we do not want the
5194 // move to overwrite the array's location, as we need it to emit
5195 // the read barrier.
5196 locations->SetOut(
5197 Location::RequiresRegister(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005198 (instruction->GetType() == DataType::Type::kInt64 || object_array_get_with_read_barrier)
5199 ? Location::kOutputOverlap
5200 : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005201 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005202}
5203
5204void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
5205 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005206 Location obj_loc = locations->InAt(0);
5207 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005208 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005209 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005210 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005211
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005212 DataType::Type type = instruction->GetType();
Calin Juravle77520bc2015-01-12 18:45:46 +00005213 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005214 case DataType::Type::kBool:
5215 case DataType::Type::kUint8: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005216 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005217 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005218 break;
5219 }
5220
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005221 case DataType::Type::kInt8: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005222 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005223 __ movsxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005224 break;
5225 }
5226
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005227 case DataType::Type::kUint16: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005228 Register out = out_loc.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07005229 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5230 // Branch cases into compressed and uncompressed for each index's type.
5231 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
5232 NearLabel done, not_compressed;
Vladimir Marko3c89d422017-02-17 11:30:23 +00005233 __ testb(Address(obj, count_offset), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005234 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005235 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
5236 "Expecting 0=compressed, 1=uncompressed");
5237 __ j(kNotZero, &not_compressed);
jessicahandojo4877b792016-09-08 19:49:13 -07005238 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
5239 __ jmp(&done);
5240 __ Bind(&not_compressed);
5241 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5242 __ Bind(&done);
5243 } else {
5244 // Common case for charAt of array of char or when string compression's
5245 // feature is turned off.
5246 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5247 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005248 break;
5249 }
5250
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005251 case DataType::Type::kInt16: {
5252 Register out = out_loc.AsRegister<Register>();
5253 __ movsxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5254 break;
5255 }
5256
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005257 case DataType::Type::kInt32: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005258 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005259 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005260 break;
5261 }
5262
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005263 case DataType::Type::kReference: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005264 static_assert(
5265 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5266 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00005267 // /* HeapReference<Object> */ out =
5268 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5269 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005270 // Note that a potential implicit null check is handled in this
5271 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
5272 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00005273 instruction, out_loc, obj, data_offset, index, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005274 } else {
5275 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005276 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
5277 codegen_->MaybeRecordImplicitNullCheck(instruction);
5278 // If read barriers are enabled, emit read barriers other than
5279 // Baker's using a slow path (and also unpoison the loaded
5280 // reference, if heap poisoning is enabled).
Roland Levillain7c1559a2015-12-15 10:55:36 +00005281 if (index.IsConstant()) {
5282 uint32_t offset =
5283 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005284 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5285 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005286 codegen_->MaybeGenerateReadBarrierSlow(
5287 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5288 }
5289 }
5290 break;
5291 }
5292
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005293 case DataType::Type::kInt64: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005294 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005295 __ movl(out_loc.AsRegisterPairLow<Register>(),
5296 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
5297 codegen_->MaybeRecordImplicitNullCheck(instruction);
5298 __ movl(out_loc.AsRegisterPairHigh<Register>(),
5299 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005300 break;
5301 }
5302
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005303 case DataType::Type::kFloat32: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005304 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005305 __ movss(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005306 break;
5307 }
5308
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005309 case DataType::Type::kFloat64: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005310 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005311 __ movsd(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005312 break;
5313 }
5314
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005315 case DataType::Type::kVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00005316 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005317 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005318 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005319
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005320 if (type == DataType::Type::kReference || type == DataType::Type::kInt64) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005321 // Potential implicit null checks, in the case of reference or
5322 // long arrays, are handled in the previous switch statement.
5323 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005324 codegen_->MaybeRecordImplicitNullCheck(instruction);
5325 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005326}
5327
5328void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005329 DataType::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005330
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005331 bool needs_write_barrier =
5332 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005333 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005334
Nicolas Geoffray39468442014-09-02 15:17:15 +01005335 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
5336 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01005337 may_need_runtime_call_for_type_check ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00005338 LocationSummary::kCallOnSlowPath :
5339 LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005340
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005341 bool is_byte_type = DataType::Size(value_type) == 1u;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005342 // We need the inputs to be different than the output in case of long operation.
5343 // In case of a byte operation, the register allocator does not support multiple
5344 // inputs that die at entry with one in a specific register.
5345 locations->SetInAt(0, Location::RequiresRegister());
5346 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5347 if (is_byte_type) {
5348 // Ensure the value is in a byte register.
5349 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005350 } else if (DataType::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05005351 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005352 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005353 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
5354 }
5355 if (needs_write_barrier) {
5356 // Temporary registers for the write barrier.
5357 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5358 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005359 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005360 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005361}
5362
5363void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
5364 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005365 Location array_loc = locations->InAt(0);
5366 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005367 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005368 Location value = locations->InAt(2);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005369 DataType::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005370 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5371 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5372 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005373 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005374 bool needs_write_barrier =
5375 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005376
5377 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005378 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005379 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005380 case DataType::Type::kInt8: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005381 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005382 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_1, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005383 if (value.IsRegister()) {
5384 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005385 } else {
Nicolas Geoffray78612082017-07-24 14:18:53 +01005386 __ movb(address, Immediate(CodeGenerator::GetInt8ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005387 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005388 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005389 break;
5390 }
5391
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005392 case DataType::Type::kUint16:
5393 case DataType::Type::kInt16: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005394 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005395 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_2, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005396 if (value.IsRegister()) {
5397 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005398 } else {
Nicolas Geoffray78612082017-07-24 14:18:53 +01005399 __ movw(address, Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005400 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005401 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005402 break;
5403 }
5404
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005405 case DataType::Type::kReference: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005406 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005407 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005408
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005409 if (!value.IsRegister()) {
5410 // Just setting null.
5411 DCHECK(instruction->InputAt(2)->IsNullConstant());
5412 DCHECK(value.IsConstant()) << value;
5413 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00005414 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005415 DCHECK(!needs_write_barrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005416 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005417 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005418 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005419
5420 DCHECK(needs_write_barrier);
5421 Register register_value = value.AsRegister<Register>();
Roland Levillain16d9f942016-08-25 17:27:56 +01005422 // We cannot use a NearLabel for `done`, as its range may be too
5423 // short when Baker read barriers are enabled.
5424 Label done;
5425 NearLabel not_null, do_put;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005426 SlowPathCode* slow_path = nullptr;
Roland Levillain16d9f942016-08-25 17:27:56 +01005427 Location temp_loc = locations->GetTemp(0);
5428 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005429 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005430 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86(instruction);
5431 codegen_->AddSlowPath(slow_path);
5432 if (instruction->GetValueCanBeNull()) {
5433 __ testl(register_value, register_value);
5434 __ j(kNotEqual, &not_null);
5435 __ movl(address, Immediate(0));
5436 codegen_->MaybeRecordImplicitNullCheck(instruction);
5437 __ jmp(&done);
5438 __ Bind(&not_null);
5439 }
5440
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005441 // Note that when Baker read barriers are enabled, the type
5442 // checks are performed without read barriers. This is fine,
5443 // even in the case where a class object is in the from-space
5444 // after the flip, as a comparison involving such a type would
5445 // not produce a false positive; it may of course produce a
5446 // false negative, in which case we would take the ArraySet
5447 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01005448
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005449 // /* HeapReference<Class> */ temp = array->klass_
5450 __ movl(temp, Address(array, class_offset));
5451 codegen_->MaybeRecordImplicitNullCheck(instruction);
5452 __ MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01005453
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005454 // /* HeapReference<Class> */ temp = temp->component_type_
5455 __ movl(temp, Address(temp, component_offset));
5456 // If heap poisoning is enabled, no need to unpoison `temp`
5457 // nor the object reference in `register_value->klass`, as
5458 // we are comparing two poisoned references.
5459 __ cmpl(temp, Address(register_value, class_offset));
Roland Levillain16d9f942016-08-25 17:27:56 +01005460
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005461 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5462 __ j(kEqual, &do_put);
5463 // If heap poisoning is enabled, the `temp` reference has
5464 // not been unpoisoned yet; unpoison it now.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005465 __ MaybeUnpoisonHeapReference(temp);
5466
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005467 // If heap poisoning is enabled, no need to unpoison the
5468 // heap reference loaded below, as it is only used for a
5469 // comparison with null.
5470 __ cmpl(Address(temp, super_offset), Immediate(0));
5471 __ j(kNotEqual, slow_path->GetEntryLabel());
5472 __ Bind(&do_put);
5473 } else {
5474 __ j(kNotEqual, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005475 }
5476 }
5477
5478 if (kPoisonHeapReferences) {
5479 __ movl(temp, register_value);
5480 __ PoisonHeapReference(temp);
5481 __ movl(address, temp);
5482 } else {
5483 __ movl(address, register_value);
5484 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005485 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005486 codegen_->MaybeRecordImplicitNullCheck(instruction);
5487 }
5488
5489 Register card = locations->GetTemp(1).AsRegister<Register>();
5490 codegen_->MarkGCCard(
5491 temp, card, array, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
5492 __ Bind(&done);
5493
5494 if (slow_path != nullptr) {
5495 __ Bind(slow_path->GetExitLabel());
5496 }
5497
5498 break;
5499 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005500
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005501 case DataType::Type::kInt32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005502 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005503 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005504 if (value.IsRegister()) {
5505 __ movl(address, value.AsRegister<Register>());
5506 } else {
5507 DCHECK(value.IsConstant()) << value;
5508 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5509 __ movl(address, Immediate(v));
5510 }
5511 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005512 break;
5513 }
5514
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005515 case DataType::Type::kInt64: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005516 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005517 if (value.IsRegisterPair()) {
5518 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
5519 value.AsRegisterPairLow<Register>());
5520 codegen_->MaybeRecordImplicitNullCheck(instruction);
5521 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
5522 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005523 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005524 DCHECK(value.IsConstant());
5525 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
5526 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
5527 Immediate(Low32Bits(val)));
5528 codegen_->MaybeRecordImplicitNullCheck(instruction);
5529 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
5530 Immediate(High32Bits(val)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005531 }
5532 break;
5533 }
5534
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005535 case DataType::Type::kFloat32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005536 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005537 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005538 if (value.IsFpuRegister()) {
5539 __ movss(address, value.AsFpuRegister<XmmRegister>());
5540 } else {
5541 DCHECK(value.IsConstant());
5542 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
5543 __ movl(address, Immediate(v));
5544 }
5545 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005546 break;
5547 }
5548
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005549 case DataType::Type::kFloat64: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005550 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005551 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005552 if (value.IsFpuRegister()) {
5553 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5554 } else {
5555 DCHECK(value.IsConstant());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005556 Address address_hi =
5557 CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset + kX86WordSize);
Mark Mendell81489372015-11-04 11:30:41 -05005558 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5559 __ movl(address, Immediate(Low32Bits(v)));
5560 codegen_->MaybeRecordImplicitNullCheck(instruction);
5561 __ movl(address_hi, Immediate(High32Bits(v)));
5562 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005563 break;
5564 }
5565
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005566 case DataType::Type::kVoid:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005567 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005568 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005569 }
5570}
5571
5572void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
5573 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005574 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04005575 if (!instruction->IsEmittedAtUseSite()) {
5576 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5577 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005578}
5579
5580void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04005581 if (instruction->IsEmittedAtUseSite()) {
5582 return;
5583 }
5584
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005585 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005586 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005587 Register obj = locations->InAt(0).AsRegister<Register>();
5588 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005589 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005590 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo4877b792016-09-08 19:49:13 -07005591 // Mask out most significant bit in case the array is String's array of char.
5592 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005593 __ shrl(out, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005594 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005595}
5596
5597void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005598 RegisterSet caller_saves = RegisterSet::Empty();
5599 InvokeRuntimeCallingConvention calling_convention;
5600 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5601 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5602 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005603 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04005604 HInstruction* length = instruction->InputAt(1);
5605 if (!length->IsEmittedAtUseSite()) {
5606 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5607 }
jessicahandojo4877b792016-09-08 19:49:13 -07005608 // Need register to see array's length.
5609 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5610 locations->AddTemp(Location::RequiresRegister());
5611 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005612}
5613
5614void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
jessicahandojo4877b792016-09-08 19:49:13 -07005615 const bool is_string_compressed_char_at =
5616 mirror::kUseStringCompression && instruction->IsStringCharAt();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005617 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005618 Location index_loc = locations->InAt(0);
5619 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07005620 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005621 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005622
Mark Mendell99dbd682015-04-22 16:18:52 -04005623 if (length_loc.IsConstant()) {
5624 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5625 if (index_loc.IsConstant()) {
5626 // BCE will remove the bounds check if we are guarenteed to pass.
5627 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5628 if (index < 0 || index >= length) {
5629 codegen_->AddSlowPath(slow_path);
5630 __ jmp(slow_path->GetEntryLabel());
5631 } else {
5632 // Some optimization after BCE may have generated this, and we should not
5633 // generate a bounds check if it is a valid range.
5634 }
5635 return;
5636 }
5637
5638 // We have to reverse the jump condition because the length is the constant.
5639 Register index_reg = index_loc.AsRegister<Register>();
5640 __ cmpl(index_reg, Immediate(length));
5641 codegen_->AddSlowPath(slow_path);
5642 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005643 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04005644 HInstruction* array_length = instruction->InputAt(1);
5645 if (array_length->IsEmittedAtUseSite()) {
5646 // Address the length field in the array.
5647 DCHECK(array_length->IsArrayLength());
5648 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
5649 Location array_loc = array_length->GetLocations()->InAt(0);
5650 Address array_len(array_loc.AsRegister<Register>(), len_offset);
jessicahandojo4877b792016-09-08 19:49:13 -07005651 if (is_string_compressed_char_at) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005652 // TODO: if index_loc.IsConstant(), compare twice the index (to compensate for
5653 // the string compression flag) with the in-memory length and avoid the temporary.
jessicahandojo4877b792016-09-08 19:49:13 -07005654 Register length_reg = locations->GetTemp(0).AsRegister<Register>();
5655 __ movl(length_reg, array_len);
5656 codegen_->MaybeRecordImplicitNullCheck(array_length);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005657 __ shrl(length_reg, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005658 codegen_->GenerateIntCompare(length_reg, index_loc);
Mark Mendellee8d9712016-07-12 11:13:15 -04005659 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07005660 // Checking bounds for general case:
5661 // Array of char or string's array with feature compression off.
5662 if (index_loc.IsConstant()) {
5663 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5664 __ cmpl(array_len, Immediate(value));
5665 } else {
5666 __ cmpl(array_len, index_loc.AsRegister<Register>());
5667 }
5668 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendellee8d9712016-07-12 11:13:15 -04005669 }
Mark Mendell99dbd682015-04-22 16:18:52 -04005670 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005671 codegen_->GenerateIntCompare(length_loc, index_loc);
Mark Mendell99dbd682015-04-22 16:18:52 -04005672 }
5673 codegen_->AddSlowPath(slow_path);
5674 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005675 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005676}
5677
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005678void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005679 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005680}
5681
5682void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005683 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5684}
5685
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005686void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005687 LocationSummary* locations =
5688 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Aart Bikb13c65b2017-03-21 20:14:07 -07005689 // In suspend check slow path, usually there are no caller-save registers at all.
5690 // If SIMD instructions are present, however, we force spilling all live SIMD
5691 // registers in full width (since the runtime only saves/restores lower part).
Aart Bik5576f372017-03-23 16:17:37 -07005692 locations->SetCustomSlowPathCallerSaves(
5693 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005694}
5695
5696void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005697 HBasicBlock* block = instruction->GetBlock();
5698 if (block->GetLoopInformation() != nullptr) {
5699 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5700 // The back edge will generate the suspend check.
5701 return;
5702 }
5703 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5704 // The goto will generate the suspend check.
5705 return;
5706 }
5707 GenerateSuspendCheck(instruction, nullptr);
5708}
5709
5710void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
5711 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005712 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005713 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
5714 if (slow_path == nullptr) {
5715 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
5716 instruction->SetSlowPath(slow_path);
5717 codegen_->AddSlowPath(slow_path);
5718 if (successor != nullptr) {
5719 DCHECK(successor->IsLoopHeader());
5720 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5721 }
5722 } else {
5723 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5724 }
5725
Andreas Gampe542451c2016-07-26 09:02:02 -07005726 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86PointerSize>().Int32Value()),
Roland Levillain7c1559a2015-12-15 10:55:36 +00005727 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005728 if (successor == nullptr) {
5729 __ j(kNotEqual, slow_path->GetEntryLabel());
5730 __ Bind(slow_path->GetReturnLabel());
5731 } else {
5732 __ j(kEqual, codegen_->GetLabelOf(successor));
5733 __ jmp(slow_path->GetEntryLabel());
5734 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005735}
5736
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005737X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
5738 return codegen_->GetAssembler();
5739}
5740
Mark Mendell7c8d0092015-01-26 11:21:33 -05005741void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005742 ScratchRegisterScope ensure_scratch(
5743 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5744 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5745 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5746 __ movl(temp_reg, Address(ESP, src + stack_offset));
5747 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005748}
5749
5750void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005751 ScratchRegisterScope ensure_scratch(
5752 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5753 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5754 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5755 __ movl(temp_reg, Address(ESP, src + stack_offset));
5756 __ movl(Address(ESP, dst + stack_offset), temp_reg);
5757 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
5758 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005759}
5760
5761void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005762 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005763 Location source = move->GetSource();
5764 Location destination = move->GetDestination();
5765
5766 if (source.IsRegister()) {
5767 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005768 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005769 } else if (destination.IsFpuRegister()) {
5770 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005771 } else {
5772 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005773 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005774 }
David Brazdil74eb1b22015-12-14 11:44:01 +00005775 } else if (source.IsRegisterPair()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005776 size_t elem_size = DataType::Size(DataType::Type::kInt32);
David Brazdil74eb1b22015-12-14 11:44:01 +00005777 // Create stack space for 2 elements.
5778 __ subl(ESP, Immediate(2 * elem_size));
5779 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
5780 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
5781 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
5782 // And remove the temporary stack space we allocated.
5783 __ addl(ESP, Immediate(2 * elem_size));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005784 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005785 if (destination.IsRegister()) {
5786 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
5787 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005788 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005789 } else if (destination.IsRegisterPair()) {
5790 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
5791 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
5792 __ psrlq(src_reg, Immediate(32));
5793 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005794 } else if (destination.IsStackSlot()) {
5795 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07005796 } else if (destination.IsDoubleStackSlot()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005797 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07005798 } else {
5799 DCHECK(destination.IsSIMDStackSlot());
5800 __ movups(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005801 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005802 } else if (source.IsStackSlot()) {
5803 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005804 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005805 } else if (destination.IsFpuRegister()) {
5806 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005807 } else {
5808 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005809 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
5810 }
5811 } else if (source.IsDoubleStackSlot()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005812 if (destination.IsRegisterPair()) {
5813 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
5814 __ movl(destination.AsRegisterPairHigh<Register>(),
5815 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
5816 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005817 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
5818 } else {
5819 DCHECK(destination.IsDoubleStackSlot()) << destination;
5820 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005821 }
Aart Bik5576f372017-03-23 16:17:37 -07005822 } else if (source.IsSIMDStackSlot()) {
5823 DCHECK(destination.IsFpuRegister());
5824 __ movups(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005825 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005826 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005827 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005828 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005829 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005830 if (value == 0) {
5831 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
5832 } else {
5833 __ movl(destination.AsRegister<Register>(), Immediate(value));
5834 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005835 } else {
5836 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05005837 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005838 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005839 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005840 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005841 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005842 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005843 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005844 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5845 if (value == 0) {
5846 // Easy handling of 0.0.
5847 __ xorps(dest, dest);
5848 } else {
5849 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005850 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5851 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
5852 __ movl(temp, Immediate(value));
5853 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005854 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005855 } else {
5856 DCHECK(destination.IsStackSlot()) << destination;
5857 __ movl(Address(ESP, destination.GetStackIndex()), imm);
5858 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005859 } else if (constant->IsLongConstant()) {
5860 int64_t value = constant->AsLongConstant()->GetValue();
5861 int32_t low_value = Low32Bits(value);
5862 int32_t high_value = High32Bits(value);
5863 Immediate low(low_value);
5864 Immediate high(high_value);
5865 if (destination.IsDoubleStackSlot()) {
5866 __ movl(Address(ESP, destination.GetStackIndex()), low);
5867 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5868 } else {
5869 __ movl(destination.AsRegisterPairLow<Register>(), low);
5870 __ movl(destination.AsRegisterPairHigh<Register>(), high);
5871 }
5872 } else {
5873 DCHECK(constant->IsDoubleConstant());
5874 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005875 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005876 int32_t low_value = Low32Bits(value);
5877 int32_t high_value = High32Bits(value);
5878 Immediate low(low_value);
5879 Immediate high(high_value);
5880 if (destination.IsFpuRegister()) {
5881 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5882 if (value == 0) {
5883 // Easy handling of 0.0.
5884 __ xorpd(dest, dest);
5885 } else {
5886 __ pushl(high);
5887 __ pushl(low);
5888 __ movsd(dest, Address(ESP, 0));
5889 __ addl(ESP, Immediate(8));
5890 }
5891 } else {
5892 DCHECK(destination.IsDoubleStackSlot()) << destination;
5893 __ movl(Address(ESP, destination.GetStackIndex()), low);
5894 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5895 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005896 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005897 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005898 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005899 }
5900}
5901
Mark Mendella5c19ce2015-04-01 12:51:05 -04005902void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005903 Register suggested_scratch = reg == EAX ? EBX : EAX;
5904 ScratchRegisterScope ensure_scratch(
5905 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
5906
5907 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5908 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
5909 __ movl(Address(ESP, mem + stack_offset), reg);
5910 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005911}
5912
Mark Mendell7c8d0092015-01-26 11:21:33 -05005913void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005914 ScratchRegisterScope ensure_scratch(
5915 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5916
5917 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5918 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5919 __ movl(temp_reg, Address(ESP, mem + stack_offset));
5920 __ movss(Address(ESP, mem + stack_offset), reg);
5921 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005922}
5923
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005924void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005925 ScratchRegisterScope ensure_scratch1(
5926 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005927
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005928 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
5929 ScratchRegisterScope ensure_scratch2(
5930 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005931
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005932 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
5933 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
5934 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
5935 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
5936 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
5937 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005938}
5939
5940void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005941 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005942 Location source = move->GetSource();
5943 Location destination = move->GetDestination();
5944
5945 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04005946 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
5947 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
5948 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
5949 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
5950 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005951 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005952 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005953 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005954 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005955 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5956 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005957 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
5958 // Use XOR Swap algorithm to avoid a temporary.
5959 DCHECK_NE(source.reg(), destination.reg());
5960 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5961 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5962 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5963 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
5964 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
5965 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
5966 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005967 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
5968 // Take advantage of the 16 bytes in the XMM register.
5969 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
5970 Address stack(ESP, destination.GetStackIndex());
5971 // Load the double into the high doubleword.
5972 __ movhpd(reg, stack);
5973
5974 // Store the low double into the destination.
5975 __ movsd(stack, reg);
5976
5977 // Move the high double to the low double.
5978 __ psrldq(reg, Immediate(8));
5979 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
5980 // Take advantage of the 16 bytes in the XMM register.
5981 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
5982 Address stack(ESP, source.GetStackIndex());
5983 // Load the double into the high doubleword.
5984 __ movhpd(reg, stack);
5985
5986 // Store the low double into the destination.
5987 __ movsd(stack, reg);
5988
5989 // Move the high double to the low double.
5990 __ psrldq(reg, Immediate(8));
5991 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
5992 Exchange(destination.GetStackIndex(), source.GetStackIndex());
5993 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005994 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005995 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005996 }
5997}
5998
5999void ParallelMoveResolverX86::SpillScratch(int reg) {
6000 __ pushl(static_cast<Register>(reg));
6001}
6002
6003void ParallelMoveResolverX86::RestoreScratch(int reg) {
6004 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006005}
6006
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006007HLoadClass::LoadKind CodeGeneratorX86::GetSupportedLoadClassKind(
6008 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006009 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006010 case HLoadClass::LoadKind::kInvalid:
6011 LOG(FATAL) << "UNREACHABLE";
6012 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006013 case HLoadClass::LoadKind::kReferrersClass:
6014 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006015 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006016 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006017 case HLoadClass::LoadKind::kBssEntry:
Vladimir Marko764d4542017-05-16 10:31:41 +01006018 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006019 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006020 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006021 DCHECK(Runtime::Current()->UseJitCompilation());
6022 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01006023 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006024 case HLoadClass::LoadKind::kRuntimeCall:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006025 break;
6026 }
6027 return desired_class_load_kind;
6028}
6029
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006030void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00006031 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006032 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006033 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00006034 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006035 cls,
6036 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00006037 Location::RegisterLocation(EAX));
Vladimir Markoea4c1262017-02-06 19:59:33 +00006038 DCHECK_EQ(calling_convention.GetRegisterAt(0), EAX);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006039 return;
6040 }
Vladimir Marko41559982017-01-06 14:04:23 +00006041 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006042
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006043 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
6044 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006045 ? LocationSummary::kCallOnSlowPath
6046 : LocationSummary::kNoCall;
6047 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006048 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006049 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006050 }
6051
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006052 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006053 load_kind == HLoadClass::LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006054 load_kind == HLoadClass::LoadKind::kBootImageClassTable ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006055 load_kind == HLoadClass::LoadKind::kBssEntry) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006056 locations->SetInAt(0, Location::RequiresRegister());
6057 }
6058 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006059 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
6060 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6061 // Rely on the type resolution and/or initialization to save everything.
6062 RegisterSet caller_saves = RegisterSet::Empty();
6063 InvokeRuntimeCallingConvention calling_convention;
6064 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6065 locations->SetCustomSlowPathCallerSaves(caller_saves);
6066 } else {
6067 // For non-Baker read barrier we have a temp-clobbering call.
6068 }
6069 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006070}
6071
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006072Label* CodeGeneratorX86::NewJitRootClassPatch(const DexFile& dex_file,
6073 dex::TypeIndex dex_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006074 Handle<mirror::Class> handle) {
6075 jit_class_roots_.Overwrite(TypeReference(&dex_file, dex_index),
6076 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006077 // Add a patch entry and return the label.
6078 jit_class_patches_.emplace_back(dex_file, dex_index.index_);
6079 PatchInfo<Label>* info = &jit_class_patches_.back();
6080 return &info->label;
6081}
6082
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006083// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6084// move.
6085void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00006086 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006087 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00006088 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01006089 return;
6090 }
Vladimir Marko41559982017-01-06 14:04:23 +00006091 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01006092
Vladimir Marko41559982017-01-06 14:04:23 +00006093 LocationSummary* locations = cls->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006094 Location out_loc = locations->Out();
6095 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006096
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006097 bool generate_null_check = false;
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006098 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
6099 ? kWithoutReadBarrier
6100 : kCompilerReadBarrierOption;
Vladimir Marko41559982017-01-06 14:04:23 +00006101 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006102 case HLoadClass::LoadKind::kReferrersClass: {
6103 DCHECK(!cls->CanCallRuntime());
6104 DCHECK(!cls->MustGenerateClinitCheck());
6105 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6106 Register current_method = locations->InAt(0).AsRegister<Register>();
6107 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006108 cls,
6109 out_loc,
6110 Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()),
Roland Levillain00468f32016-10-27 18:02:48 +01006111 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006112 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006113 break;
6114 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006115 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006116 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006117 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006118 Register method_address = locations->InAt(0).AsRegister<Register>();
6119 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Marko1998cd02017-01-13 13:02:58 +00006120 codegen_->RecordBootTypePatch(cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006121 break;
6122 }
6123 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006124 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006125 uint32_t address = dchecked_integral_cast<uint32_t>(
6126 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
6127 DCHECK_NE(address, 0u);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006128 __ movl(out, Immediate(address));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006129 break;
6130 }
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006131 case HLoadClass::LoadKind::kBootImageClassTable: {
6132 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6133 Register method_address = locations->InAt(0).AsRegister<Register>();
6134 __ movl(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
6135 codegen_->RecordBootTypePatch(cls);
6136 // Extract the reference from the slot data, i.e. clear the hash bits.
6137 int32_t masked_hash = ClassTable::TableSlot::MaskHash(
6138 ComputeModifiedUtf8Hash(cls->GetDexFile().StringByTypeIdx(cls->GetTypeIndex())));
6139 if (masked_hash != 0) {
6140 __ subl(out, Immediate(masked_hash));
6141 }
6142 break;
6143 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006144 case HLoadClass::LoadKind::kBssEntry: {
6145 Register method_address = locations->InAt(0).AsRegister<Register>();
6146 Address address(method_address, CodeGeneratorX86::kDummy32BitOffset);
6147 Label* fixup_label = codegen_->NewTypeBssEntryPatch(cls);
6148 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
6149 generate_null_check = true;
6150 break;
6151 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006152 case HLoadClass::LoadKind::kJitTableAddress: {
6153 Address address = Address::Absolute(CodeGeneratorX86::kDummy32BitOffset);
6154 Label* fixup_label = codegen_->NewJitRootClassPatch(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006155 cls->GetDexFile(), cls->GetTypeIndex(), cls->GetClass());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006156 // /* GcRoot<mirror::Class> */ out = *address
Vladimir Markoea4c1262017-02-06 19:59:33 +00006157 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006158 break;
6159 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006160 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006161 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00006162 LOG(FATAL) << "UNREACHABLE";
6163 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006164 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006165
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006166 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6167 DCHECK(cls->CanCallRuntime());
6168 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
6169 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
6170 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006171
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006172 if (generate_null_check) {
6173 __ testl(out, out);
6174 __ j(kEqual, slow_path->GetEntryLabel());
6175 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006176
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006177 if (cls->MustGenerateClinitCheck()) {
6178 GenerateClassInitializationCheck(slow_path, out);
6179 } else {
6180 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006181 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006182 }
6183}
6184
6185void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
6186 LocationSummary* locations =
6187 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
6188 locations->SetInAt(0, Location::RequiresRegister());
6189 if (check->HasUses()) {
6190 locations->SetOut(Location::SameAsFirstInput());
6191 }
6192}
6193
6194void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006195 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07006196 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006197 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006198 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00006199 GenerateClassInitializationCheck(slow_path,
6200 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006201}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006202
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006203void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07006204 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006205 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
6206 Immediate(mirror::Class::kStatusInitialized));
6207 __ j(kLess, slow_path->GetEntryLabel());
6208 __ Bind(slow_path->GetExitLabel());
6209 // No need for memory fence, thanks to the X86 memory model.
6210}
6211
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006212HLoadString::LoadKind CodeGeneratorX86::GetSupportedLoadStringKind(
6213 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006214 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006215 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006216 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00006217 case HLoadString::LoadKind::kBssEntry:
Vladimir Marko764d4542017-05-16 10:31:41 +01006218 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006219 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006220 case HLoadString::LoadKind::kJitTableAddress:
6221 DCHECK(Runtime::Current()->UseJitCompilation());
6222 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01006223 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006224 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006225 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006226 }
6227 return desired_string_load_kind;
6228}
6229
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006230void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006231 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006232 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006233 HLoadString::LoadKind load_kind = load->GetLoadKind();
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006234 if (load_kind == HLoadString::LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006235 load_kind == HLoadString::LoadKind::kBootImageInternTable ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00006236 load_kind == HLoadString::LoadKind::kBssEntry) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006237 locations->SetInAt(0, Location::RequiresRegister());
6238 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006239 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006240 locations->SetOut(Location::RegisterLocation(EAX));
6241 } else {
6242 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006243 if (load_kind == HLoadString::LoadKind::kBssEntry) {
6244 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00006245 // Rely on the pResolveString to save everything.
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006246 RegisterSet caller_saves = RegisterSet::Empty();
6247 InvokeRuntimeCallingConvention calling_convention;
6248 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6249 locations->SetCustomSlowPathCallerSaves(caller_saves);
6250 } else {
6251 // For non-Baker read barrier we have a temp-clobbering call.
6252 }
6253 }
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006254 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006255}
6256
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006257Label* CodeGeneratorX86::NewJitRootStringPatch(const DexFile& dex_file,
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006258 dex::StringIndex dex_index,
6259 Handle<mirror::String> handle) {
6260 jit_string_roots_.Overwrite(
6261 StringReference(&dex_file, dex_index), reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006262 // Add a patch entry and return the label.
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006263 jit_string_patches_.emplace_back(dex_file, dex_index.index_);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006264 PatchInfo<Label>* info = &jit_string_patches_.back();
6265 return &info->label;
6266}
6267
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006268// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6269// move.
6270void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006271 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006272 Location out_loc = locations->Out();
6273 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006274
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006275 switch (load->GetLoadKind()) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006276 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006277 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006278 Register method_address = locations->InAt(0).AsRegister<Register>();
6279 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Markoaad75c62016-10-03 08:46:48 +00006280 codegen_->RecordBootStringPatch(load);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006281 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006282 }
6283 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006284 uint32_t address = dchecked_integral_cast<uint32_t>(
6285 reinterpret_cast<uintptr_t>(load->GetString().Get()));
6286 DCHECK_NE(address, 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006287 __ movl(out, Immediate(address));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006288 return;
6289 }
6290 case HLoadString::LoadKind::kBootImageInternTable: {
6291 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6292 Register method_address = locations->InAt(0).AsRegister<Register>();
6293 __ movl(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
6294 codegen_->RecordBootStringPatch(load);
6295 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006296 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00006297 case HLoadString::LoadKind::kBssEntry: {
6298 Register method_address = locations->InAt(0).AsRegister<Register>();
6299 Address address = Address(method_address, CodeGeneratorX86::kDummy32BitOffset);
6300 Label* fixup_label = codegen_->NewStringBssEntryPatch(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006301 // /* GcRoot<mirror::String> */ out = *address /* PC-relative */
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006302 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
Vladimir Markoaad75c62016-10-03 08:46:48 +00006303 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
6304 codegen_->AddSlowPath(slow_path);
6305 __ testl(out, out);
6306 __ j(kEqual, slow_path->GetEntryLabel());
6307 __ Bind(slow_path->GetExitLabel());
6308 return;
6309 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006310 case HLoadString::LoadKind::kJitTableAddress: {
6311 Address address = Address::Absolute(CodeGeneratorX86::kDummy32BitOffset);
6312 Label* fixup_label = codegen_->NewJitRootStringPatch(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006313 load->GetDexFile(), load->GetStringIndex(), load->GetString());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006314 // /* GcRoot<mirror::String> */ out = *address
6315 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
6316 return;
6317 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006318 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006319 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006320 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006321
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006322 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006323 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006324 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006325 __ movl(calling_convention.GetRegisterAt(0), Immediate(load->GetStringIndex().index_));
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006326 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6327 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006328}
6329
David Brazdilcb1c0552015-08-04 16:22:25 +01006330static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006331 return Address::Absolute(Thread::ExceptionOffset<kX86PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01006332}
6333
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006334void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
6335 LocationSummary* locations =
6336 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
6337 locations->SetOut(Location::RequiresRegister());
6338}
6339
6340void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01006341 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
6342}
6343
6344void LocationsBuilderX86::VisitClearException(HClearException* clear) {
6345 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
6346}
6347
6348void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6349 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006350}
6351
6352void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
6353 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006354 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006355 InvokeRuntimeCallingConvention calling_convention;
6356 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6357}
6358
6359void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006360 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006361 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006362}
6363
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006364// Temp is used for read barrier.
6365static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
6366 if (kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00006367 !kUseBakerReadBarrier &&
6368 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain7c1559a2015-12-15 10:55:36 +00006369 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006370 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
6371 return 1;
6372 }
6373 return 0;
6374}
6375
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006376// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006377// interface pointer, one for loading the current interface.
6378// The other checks have one temp for loading the object's class.
6379static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
6380 if (type_check_kind == TypeCheckKind::kInterfaceCheck && !kPoisonHeapReferences) {
6381 return 2;
6382 }
6383 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006384}
6385
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006386void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006387 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006388 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01006389 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006390 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006391 case TypeCheckKind::kExactCheck:
6392 case TypeCheckKind::kAbstractClassCheck:
6393 case TypeCheckKind::kClassHierarchyCheck:
6394 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006395 call_kind =
6396 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01006397 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006398 break;
6399 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006400 case TypeCheckKind::kUnresolvedCheck:
6401 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006402 call_kind = LocationSummary::kCallOnSlowPath;
6403 break;
6404 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006405
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006406 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01006407 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006408 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006409 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006410 locations->SetInAt(0, Location::RequiresRegister());
6411 locations->SetInAt(1, Location::Any());
6412 // Note that TypeCheckSlowPathX86 uses this "out" register too.
6413 locations->SetOut(Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006414 // When read barriers are enabled, we need a temporary register for some cases.
6415 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006416}
6417
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006418void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006419 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006420 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006421 Location obj_loc = locations->InAt(0);
6422 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006423 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006424 Location out_loc = locations->Out();
6425 Register out = out_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006426 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
6427 DCHECK_LE(num_temps, 1u);
6428 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006429 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006430 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6431 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6432 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07006433 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006434 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006435
6436 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006437 // Avoid null check if we know obj is not null.
6438 if (instruction->MustDoNullCheck()) {
6439 __ testl(obj, obj);
6440 __ j(kEqual, &zero);
6441 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006442
Roland Levillain7c1559a2015-12-15 10:55:36 +00006443 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006444 case TypeCheckKind::kExactCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006445 // /* HeapReference<Class> */ out = obj->klass_
6446 GenerateReferenceLoadTwoRegisters(instruction,
6447 out_loc,
6448 obj_loc,
6449 class_offset,
6450 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006451 if (cls.IsRegister()) {
6452 __ cmpl(out, cls.AsRegister<Register>());
6453 } else {
6454 DCHECK(cls.IsStackSlot()) << cls;
6455 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6456 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006457
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006458 // Classes must be equal for the instanceof to succeed.
6459 __ j(kNotEqual, &zero);
6460 __ movl(out, Immediate(1));
6461 __ jmp(&done);
6462 break;
6463 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006464
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006465 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006466 // /* HeapReference<Class> */ out = obj->klass_
6467 GenerateReferenceLoadTwoRegisters(instruction,
6468 out_loc,
6469 obj_loc,
6470 class_offset,
6471 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006472 // If the class is abstract, we eagerly fetch the super class of the
6473 // object to avoid doing a comparison we know will fail.
6474 NearLabel loop;
6475 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006476 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006477 GenerateReferenceLoadOneRegister(instruction,
6478 out_loc,
6479 super_offset,
6480 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006481 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006482 __ testl(out, out);
6483 // If `out` is null, we use it for the result, and jump to `done`.
6484 __ j(kEqual, &done);
6485 if (cls.IsRegister()) {
6486 __ cmpl(out, cls.AsRegister<Register>());
6487 } else {
6488 DCHECK(cls.IsStackSlot()) << cls;
6489 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6490 }
6491 __ j(kNotEqual, &loop);
6492 __ movl(out, Immediate(1));
6493 if (zero.IsLinked()) {
6494 __ jmp(&done);
6495 }
6496 break;
6497 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006498
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006499 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006500 // /* HeapReference<Class> */ out = obj->klass_
6501 GenerateReferenceLoadTwoRegisters(instruction,
6502 out_loc,
6503 obj_loc,
6504 class_offset,
6505 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006506 // Walk over the class hierarchy to find a match.
6507 NearLabel loop, success;
6508 __ Bind(&loop);
6509 if (cls.IsRegister()) {
6510 __ cmpl(out, cls.AsRegister<Register>());
6511 } else {
6512 DCHECK(cls.IsStackSlot()) << cls;
6513 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6514 }
6515 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006516 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006517 GenerateReferenceLoadOneRegister(instruction,
6518 out_loc,
6519 super_offset,
6520 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006521 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006522 __ testl(out, out);
6523 __ j(kNotEqual, &loop);
6524 // If `out` is null, we use it for the result, and jump to `done`.
6525 __ jmp(&done);
6526 __ Bind(&success);
6527 __ movl(out, Immediate(1));
6528 if (zero.IsLinked()) {
6529 __ jmp(&done);
6530 }
6531 break;
6532 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006533
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006534 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006535 // /* HeapReference<Class> */ out = obj->klass_
6536 GenerateReferenceLoadTwoRegisters(instruction,
6537 out_loc,
6538 obj_loc,
6539 class_offset,
6540 kCompilerReadBarrierOption);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006541 // Do an exact check.
6542 NearLabel exact_check;
6543 if (cls.IsRegister()) {
6544 __ cmpl(out, cls.AsRegister<Register>());
6545 } else {
6546 DCHECK(cls.IsStackSlot()) << cls;
6547 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6548 }
6549 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006550 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006551 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006552 GenerateReferenceLoadOneRegister(instruction,
6553 out_loc,
6554 component_offset,
6555 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006556 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006557 __ testl(out, out);
6558 // If `out` is null, we use it for the result, and jump to `done`.
6559 __ j(kEqual, &done);
6560 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
6561 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006562 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006563 __ movl(out, Immediate(1));
6564 __ jmp(&done);
6565 break;
6566 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006567
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006568 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006569 // No read barrier since the slow path will retry upon failure.
6570 // /* HeapReference<Class> */ out = obj->klass_
6571 GenerateReferenceLoadTwoRegisters(instruction,
6572 out_loc,
6573 obj_loc,
6574 class_offset,
6575 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006576 if (cls.IsRegister()) {
6577 __ cmpl(out, cls.AsRegister<Register>());
6578 } else {
6579 DCHECK(cls.IsStackSlot()) << cls;
6580 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6581 }
6582 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006583 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6584 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006585 codegen_->AddSlowPath(slow_path);
6586 __ j(kNotEqual, slow_path->GetEntryLabel());
6587 __ movl(out, Immediate(1));
6588 if (zero.IsLinked()) {
6589 __ jmp(&done);
6590 }
6591 break;
6592 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006593
Calin Juravle98893e12015-10-02 21:05:03 +01006594 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006595 case TypeCheckKind::kInterfaceCheck: {
6596 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006597 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00006598 // cases.
6599 //
6600 // We cannot directly call the InstanceofNonTrivial runtime
6601 // entry point without resorting to a type checking slow path
6602 // here (i.e. by calling InvokeRuntime directly), as it would
6603 // require to assign fixed registers for the inputs of this
6604 // HInstanceOf instruction (following the runtime calling
6605 // convention), which might be cluttered by the potential first
6606 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006607 //
6608 // TODO: Introduce a new runtime entry point taking the object
6609 // to test (instead of its class) as argument, and let it deal
6610 // with the read barrier issues. This will let us refactor this
6611 // case of the `switch` code as it was previously (with a direct
6612 // call to the runtime not using a type checking slow path).
6613 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006614 DCHECK(locations->OnlyCallsOnSlowPath());
6615 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6616 /* is_fatal */ false);
6617 codegen_->AddSlowPath(slow_path);
6618 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006619 if (zero.IsLinked()) {
6620 __ jmp(&done);
6621 }
6622 break;
6623 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006624 }
6625
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006626 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006627 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006628 __ xorl(out, out);
6629 }
6630
6631 if (done.IsLinked()) {
6632 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006633 }
6634
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006635 if (slow_path != nullptr) {
6636 __ Bind(slow_path->GetExitLabel());
6637 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006638}
6639
Mathieu Chartier5ac321b2016-11-09 16:33:54 -08006640static bool IsTypeCheckSlowPathFatal(TypeCheckKind type_check_kind, bool throws_into_catch) {
6641 switch (type_check_kind) {
6642 case TypeCheckKind::kExactCheck:
6643 case TypeCheckKind::kAbstractClassCheck:
6644 case TypeCheckKind::kClassHierarchyCheck:
6645 case TypeCheckKind::kArrayObjectCheck:
6646 return !throws_into_catch && !kEmitCompilerReadBarrier;
6647 case TypeCheckKind::kInterfaceCheck:
6648 return !throws_into_catch && !kEmitCompilerReadBarrier && !kPoisonHeapReferences;
6649 case TypeCheckKind::kArrayCheck:
6650 case TypeCheckKind::kUnresolvedCheck:
6651 return false;
6652 }
6653 LOG(FATAL) << "Unreachable";
6654 UNREACHABLE();
6655}
6656
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006657void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006658 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006659 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Mathieu Chartier5ac321b2016-11-09 16:33:54 -08006660 LocationSummary::CallKind call_kind =
6661 IsTypeCheckSlowPathFatal(type_check_kind, throws_into_catch)
6662 ? LocationSummary::kNoCall
6663 : LocationSummary::kCallOnSlowPath;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006664 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6665 locations->SetInAt(0, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006666 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
6667 // Require a register for the interface check since there is a loop that compares the class to
6668 // a memory address.
6669 locations->SetInAt(1, Location::RequiresRegister());
6670 } else {
6671 locations->SetInAt(1, Location::Any());
6672 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006673 // Note that TypeCheckSlowPathX86 uses this "temp" register too.
6674 locations->AddTemp(Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006675 // When read barriers are enabled, we need an additional temporary register for some cases.
6676 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
6677}
6678
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006679void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006680 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006681 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006682 Location obj_loc = locations->InAt(0);
6683 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006684 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006685 Location temp_loc = locations->GetTemp(0);
6686 Register temp = temp_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006687 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
6688 DCHECK_GE(num_temps, 1u);
6689 DCHECK_LE(num_temps, 2u);
6690 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
6691 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6692 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6693 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6694 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
6695 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
6696 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
6697 const uint32_t object_array_data_offset =
6698 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006699
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006700 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
6701 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
6702 // read barriers is done for performance and code size reasons.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006703 bool is_type_check_slow_path_fatal =
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006704 IsTypeCheckSlowPathFatal(type_check_kind, instruction->CanThrowIntoCatchBlock());
6705
Roland Levillain0d5a2812015-11-13 10:07:31 +00006706 SlowPathCode* type_check_slow_path =
6707 new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6708 is_type_check_slow_path_fatal);
6709 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006710
Roland Levillain0d5a2812015-11-13 10:07:31 +00006711 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006712 // Avoid null check if we know obj is not null.
6713 if (instruction->MustDoNullCheck()) {
6714 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006715 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006716 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006717
Roland Levillain0d5a2812015-11-13 10:07:31 +00006718 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006719 case TypeCheckKind::kExactCheck:
6720 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006721 // /* HeapReference<Class> */ temp = obj->klass_
6722 GenerateReferenceLoadTwoRegisters(instruction,
6723 temp_loc,
6724 obj_loc,
6725 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006726 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006727
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006728 if (cls.IsRegister()) {
6729 __ cmpl(temp, cls.AsRegister<Register>());
6730 } else {
6731 DCHECK(cls.IsStackSlot()) << cls;
6732 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6733 }
6734 // Jump to slow path for throwing the exception or doing a
6735 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006736 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006737 break;
6738 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006739
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006740 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006741 // /* HeapReference<Class> */ temp = obj->klass_
6742 GenerateReferenceLoadTwoRegisters(instruction,
6743 temp_loc,
6744 obj_loc,
6745 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006746 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006747
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006748 // If the class is abstract, we eagerly fetch the super class of the
6749 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006750 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006751 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006752 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006753 GenerateReferenceLoadOneRegister(instruction,
6754 temp_loc,
6755 super_offset,
6756 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006757 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006758
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006759 // If the class reference currently in `temp` is null, jump to the slow path to throw the
6760 // exception.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006761 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006762 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006763
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006764 // Otherwise, compare the classes
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006765 if (cls.IsRegister()) {
6766 __ cmpl(temp, cls.AsRegister<Register>());
6767 } else {
6768 DCHECK(cls.IsStackSlot()) << cls;
6769 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6770 }
6771 __ j(kNotEqual, &loop);
6772 break;
6773 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006774
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006775 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006776 // /* HeapReference<Class> */ temp = obj->klass_
6777 GenerateReferenceLoadTwoRegisters(instruction,
6778 temp_loc,
6779 obj_loc,
6780 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006781 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006782
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006783 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006784 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006785 __ Bind(&loop);
6786 if (cls.IsRegister()) {
6787 __ cmpl(temp, cls.AsRegister<Register>());
6788 } else {
6789 DCHECK(cls.IsStackSlot()) << cls;
6790 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6791 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006792 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006793
Roland Levillain0d5a2812015-11-13 10:07:31 +00006794 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006795 GenerateReferenceLoadOneRegister(instruction,
6796 temp_loc,
6797 super_offset,
6798 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006799 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006800
6801 // If the class reference currently in `temp` is not null, jump
6802 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006803 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006804 __ j(kNotZero, &loop);
6805 // Otherwise, jump to the slow path to throw the exception.;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006806 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006807 break;
6808 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006809
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006810 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006811 // /* HeapReference<Class> */ temp = obj->klass_
6812 GenerateReferenceLoadTwoRegisters(instruction,
6813 temp_loc,
6814 obj_loc,
6815 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006816 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006817
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006818 // Do an exact check.
6819 if (cls.IsRegister()) {
6820 __ cmpl(temp, cls.AsRegister<Register>());
6821 } else {
6822 DCHECK(cls.IsStackSlot()) << cls;
6823 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6824 }
6825 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006826
6827 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006828 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006829 GenerateReferenceLoadOneRegister(instruction,
6830 temp_loc,
6831 component_offset,
6832 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006833 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006834
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006835 // If the component type is null (i.e. the object not an array), jump to the slow path to
6836 // throw the exception. Otherwise proceed with the check.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006837 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006838 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006839
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006840 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006841 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006842 break;
6843 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006844
Calin Juravle98893e12015-10-02 21:05:03 +01006845 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006846 // We always go into the type check slow path for the unresolved check case.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006847 // We cannot directly call the CheckCast runtime entry point
6848 // without resorting to a type checking slow path here (i.e. by
6849 // calling InvokeRuntime directly), as it would require to
6850 // assign fixed registers for the inputs of this HInstanceOf
6851 // instruction (following the runtime calling convention), which
6852 // might be cluttered by the potential first read barrier
6853 // emission at the beginning of this method.
6854 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006855 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006856
6857 case TypeCheckKind::kInterfaceCheck: {
6858 // Fast path for the interface check. Since we compare with a memory location in the inner
6859 // loop we would need to have cls poisoned. However unpoisoning cls would reset the
6860 // conditional flags and cause the conditional jump to be incorrect. Therefore we just jump
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006861 // to the slow path if we are running under poisoning.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006862 if (!kPoisonHeapReferences) {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006863 // Try to avoid read barriers to improve the fast path. We can not get false positives by
6864 // doing this.
6865 // /* HeapReference<Class> */ temp = obj->klass_
6866 GenerateReferenceLoadTwoRegisters(instruction,
6867 temp_loc,
6868 obj_loc,
6869 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006870 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006871
6872 // /* HeapReference<Class> */ temp = temp->iftable_
6873 GenerateReferenceLoadTwoRegisters(instruction,
6874 temp_loc,
6875 temp_loc,
6876 iftable_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006877 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08006878 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006879 __ movl(maybe_temp2_loc.AsRegister<Register>(), Address(temp, array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08006880 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006881 NearLabel start_loop;
6882 __ Bind(&start_loop);
Mathieu Chartier6beced42016-11-15 15:51:31 -08006883 // Need to subtract first to handle the empty array case.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006884 __ subl(maybe_temp2_loc.AsRegister<Register>(), Immediate(2));
Mathieu Chartier6beced42016-11-15 15:51:31 -08006885 __ j(kNegative, type_check_slow_path->GetEntryLabel());
6886 // Go to next interface if the classes do not match.
6887 __ cmpl(cls.AsRegister<Register>(),
6888 CodeGeneratorX86::ArrayAddress(temp,
6889 maybe_temp2_loc,
6890 TIMES_4,
6891 object_array_data_offset));
6892 __ j(kNotEqual, &start_loop);
6893 } else {
6894 __ jmp(type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006895 }
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006896 break;
6897 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006898 }
6899 __ Bind(&done);
6900
Roland Levillain0d5a2812015-11-13 10:07:31 +00006901 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006902}
6903
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006904void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
6905 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006906 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006907 InvokeRuntimeCallingConvention calling_convention;
6908 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6909}
6910
6911void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006912 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject
6913 : kQuickUnlockObject,
Alexandre Rames8158f282015-08-07 10:26:17 +01006914 instruction,
Serban Constantinescuba45db02016-07-12 22:53:02 +01006915 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006916 if (instruction->IsEnter()) {
6917 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6918 } else {
6919 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6920 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006921}
6922
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006923void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6924void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6925void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6926
6927void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6928 LocationSummary* locations =
6929 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006930 DCHECK(instruction->GetResultType() == DataType::Type::kInt32
6931 || instruction->GetResultType() == DataType::Type::kInt64);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006932 locations->SetInAt(0, Location::RequiresRegister());
6933 locations->SetInAt(1, Location::Any());
6934 locations->SetOut(Location::SameAsFirstInput());
6935}
6936
6937void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
6938 HandleBitwiseOperation(instruction);
6939}
6940
6941void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
6942 HandleBitwiseOperation(instruction);
6943}
6944
6945void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
6946 HandleBitwiseOperation(instruction);
6947}
6948
6949void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6950 LocationSummary* locations = instruction->GetLocations();
6951 Location first = locations->InAt(0);
6952 Location second = locations->InAt(1);
6953 DCHECK(first.Equals(locations->Out()));
6954
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006955 if (instruction->GetResultType() == DataType::Type::kInt32) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006956 if (second.IsRegister()) {
6957 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006958 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006959 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006960 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006961 } else {
6962 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006963 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006964 }
6965 } else if (second.IsConstant()) {
6966 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006967 __ andl(first.AsRegister<Register>(),
6968 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006969 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006970 __ orl(first.AsRegister<Register>(),
6971 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006972 } else {
6973 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00006974 __ xorl(first.AsRegister<Register>(),
6975 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006976 }
6977 } else {
6978 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006979 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006980 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006981 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006982 } else {
6983 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006984 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006985 }
6986 }
6987 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006988 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006989 if (second.IsRegisterPair()) {
6990 if (instruction->IsAnd()) {
6991 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6992 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6993 } else if (instruction->IsOr()) {
6994 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6995 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6996 } else {
6997 DCHECK(instruction->IsXor());
6998 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6999 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7000 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007001 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007002 if (instruction->IsAnd()) {
7003 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7004 __ andl(first.AsRegisterPairHigh<Register>(),
7005 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7006 } else if (instruction->IsOr()) {
7007 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7008 __ orl(first.AsRegisterPairHigh<Register>(),
7009 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7010 } else {
7011 DCHECK(instruction->IsXor());
7012 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7013 __ xorl(first.AsRegisterPairHigh<Register>(),
7014 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7015 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007016 } else {
7017 DCHECK(second.IsConstant()) << second;
7018 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007019 int32_t low_value = Low32Bits(value);
7020 int32_t high_value = High32Bits(value);
7021 Immediate low(low_value);
7022 Immediate high(high_value);
7023 Register first_low = first.AsRegisterPairLow<Register>();
7024 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007025 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007026 if (low_value == 0) {
7027 __ xorl(first_low, first_low);
7028 } else if (low_value != -1) {
7029 __ andl(first_low, low);
7030 }
7031 if (high_value == 0) {
7032 __ xorl(first_high, first_high);
7033 } else if (high_value != -1) {
7034 __ andl(first_high, high);
7035 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007036 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007037 if (low_value != 0) {
7038 __ orl(first_low, low);
7039 }
7040 if (high_value != 0) {
7041 __ orl(first_high, high);
7042 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007043 } else {
7044 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007045 if (low_value != 0) {
7046 __ xorl(first_low, low);
7047 }
7048 if (high_value != 0) {
7049 __ xorl(first_high, high);
7050 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007051 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007052 }
7053 }
7054}
7055
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007056void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(
7057 HInstruction* instruction,
7058 Location out,
7059 uint32_t offset,
7060 Location maybe_temp,
7061 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007062 Register out_reg = out.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007063 if (read_barrier_option == kWithReadBarrier) {
7064 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007065 if (kUseBakerReadBarrier) {
7066 // Load with fast path based Baker's read barrier.
7067 // /* HeapReference<Object> */ out = *(out + offset)
7068 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00007069 instruction, out, out_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007070 } else {
7071 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007072 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain7c1559a2015-12-15 10:55:36 +00007073 // in the following move operation, as we will need it for the
7074 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00007075 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007076 __ movl(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007077 // /* HeapReference<Object> */ out = *(out + offset)
7078 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007079 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007080 }
7081 } else {
7082 // Plain load with no read barrier.
7083 // /* HeapReference<Object> */ out = *(out + offset)
7084 __ movl(out_reg, Address(out_reg, offset));
7085 __ MaybeUnpoisonHeapReference(out_reg);
7086 }
7087}
7088
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007089void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(
7090 HInstruction* instruction,
7091 Location out,
7092 Location obj,
7093 uint32_t offset,
7094 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007095 Register out_reg = out.AsRegister<Register>();
7096 Register obj_reg = obj.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007097 if (read_barrier_option == kWithReadBarrier) {
7098 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007099 if (kUseBakerReadBarrier) {
7100 // Load with fast path based Baker's read barrier.
7101 // /* HeapReference<Object> */ out = *(obj + offset)
7102 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00007103 instruction, out, obj_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007104 } else {
7105 // Load with slow path based read barrier.
7106 // /* HeapReference<Object> */ out = *(obj + offset)
7107 __ movl(out_reg, Address(obj_reg, offset));
7108 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
7109 }
7110 } else {
7111 // Plain load with no read barrier.
7112 // /* HeapReference<Object> */ out = *(obj + offset)
7113 __ movl(out_reg, Address(obj_reg, offset));
7114 __ MaybeUnpoisonHeapReference(out_reg);
7115 }
7116}
7117
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007118void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(
7119 HInstruction* instruction,
7120 Location root,
7121 const Address& address,
7122 Label* fixup_label,
7123 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007124 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007125 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07007126 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007127 if (kUseBakerReadBarrier) {
7128 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
7129 // Baker's read barrier are used:
7130 //
Roland Levillaind966ce72017-02-09 16:20:14 +00007131 // root = obj.field;
7132 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
7133 // if (temp != null) {
7134 // root = temp(root)
Roland Levillain7c1559a2015-12-15 10:55:36 +00007135 // }
7136
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007137 // /* GcRoot<mirror::Object> */ root = *address
7138 __ movl(root_reg, address);
7139 if (fixup_label != nullptr) {
7140 __ Bind(fixup_label);
7141 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007142 static_assert(
7143 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
7144 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
7145 "have different sizes.");
7146 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
7147 "art::mirror::CompressedReference<mirror::Object> and int32_t "
7148 "have different sizes.");
7149
Vladimir Marko953437b2016-08-24 08:30:46 +00007150 // Slow path marking the GC root `root`.
7151 SlowPathCode* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007152 instruction, root, /* unpoison_ref_before_marking */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007153 codegen_->AddSlowPath(slow_path);
7154
Roland Levillaind966ce72017-02-09 16:20:14 +00007155 // Test the entrypoint (`Thread::Current()->pReadBarrierMarkReg ## root.reg()`).
7156 const int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +01007157 Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(root.reg());
Roland Levillaind966ce72017-02-09 16:20:14 +00007158 __ fs()->cmpl(Address::Absolute(entry_point_offset), Immediate(0));
7159 // The entrypoint is null when the GC is not marking.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007160 __ j(kNotEqual, slow_path->GetEntryLabel());
7161 __ Bind(slow_path->GetExitLabel());
7162 } else {
7163 // GC root loaded through a slow path for read barriers other
7164 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007165 // /* GcRoot<mirror::Object>* */ root = address
7166 __ leal(root_reg, address);
7167 if (fixup_label != nullptr) {
7168 __ Bind(fixup_label);
7169 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007170 // /* mirror::Object* */ root = root->Read()
7171 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
7172 }
7173 } else {
7174 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007175 // /* GcRoot<mirror::Object> */ root = *address
7176 __ movl(root_reg, address);
7177 if (fixup_label != nullptr) {
7178 __ Bind(fixup_label);
7179 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007180 // Note that GC roots are not affected by heap poisoning, thus we
7181 // do not have to unpoison `root_reg` here.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007182 }
7183}
7184
7185void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
7186 Location ref,
7187 Register obj,
7188 uint32_t offset,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007189 bool needs_null_check) {
7190 DCHECK(kEmitCompilerReadBarrier);
7191 DCHECK(kUseBakerReadBarrier);
7192
7193 // /* HeapReference<Object> */ ref = *(obj + offset)
7194 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007195 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007196}
7197
7198void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
7199 Location ref,
7200 Register obj,
7201 uint32_t data_offset,
7202 Location index,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007203 bool needs_null_check) {
7204 DCHECK(kEmitCompilerReadBarrier);
7205 DCHECK(kUseBakerReadBarrier);
7206
Roland Levillain3d312422016-06-23 13:53:42 +01007207 static_assert(
7208 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
7209 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00007210 // /* HeapReference<Object> */ ref =
7211 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007212 Address src = CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007213 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007214}
7215
7216void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
7217 Location ref,
7218 Register obj,
7219 const Address& src,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007220 bool needs_null_check,
7221 bool always_update_field,
7222 Register* temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007223 DCHECK(kEmitCompilerReadBarrier);
7224 DCHECK(kUseBakerReadBarrier);
7225
7226 // In slow path based read barriers, the read barrier call is
7227 // inserted after the original load. However, in fast path based
7228 // Baker's read barriers, we need to perform the load of
7229 // mirror::Object::monitor_ *before* the original reference load.
7230 // This load-load ordering is required by the read barrier.
7231 // The fast path/slow path (for Baker's algorithm) should look like:
7232 //
7233 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7234 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7235 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007236 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain7c1559a2015-12-15 10:55:36 +00007237 // if (is_gray) {
7238 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7239 // }
7240 //
7241 // Note: the original implementation in ReadBarrier::Barrier is
7242 // slightly more complex as:
7243 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007244 // the high-bits of rb_state, which are expected to be all zeroes
7245 // (we use CodeGeneratorX86::GenerateMemoryBarrier instead here,
7246 // which is a no-op thanks to the x86 memory model);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007247 // - it performs additional checks that we do not do here for
7248 // performance reasons.
7249
7250 Register ref_reg = ref.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +00007251 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7252
Vladimir Marko953437b2016-08-24 08:30:46 +00007253 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007254 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
7255 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko953437b2016-08-24 08:30:46 +00007256 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
7257 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
7258 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
7259
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007260 // if (rb_state == ReadBarrier::GrayState())
Vladimir Marko953437b2016-08-24 08:30:46 +00007261 // ref = ReadBarrier::Mark(ref);
7262 // At this point, just do the "if" and make sure that flags are preserved until the branch.
7263 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain7c1559a2015-12-15 10:55:36 +00007264 if (needs_null_check) {
7265 MaybeRecordImplicitNullCheck(instruction);
7266 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007267
7268 // Load fence to prevent load-load reordering.
7269 // Note that this is a no-op, thanks to the x86 memory model.
7270 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
7271
7272 // The actual reference load.
7273 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00007274 __ movl(ref_reg, src); // Flags are unaffected.
7275
7276 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
7277 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007278 SlowPathCode* slow_path;
7279 if (always_update_field) {
7280 DCHECK(temp != nullptr);
7281 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkAndUpdateFieldSlowPathX86(
7282 instruction, ref, obj, src, /* unpoison_ref_before_marking */ true, *temp);
7283 } else {
7284 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(
7285 instruction, ref, /* unpoison_ref_before_marking */ true);
7286 }
Vladimir Marko953437b2016-08-24 08:30:46 +00007287 AddSlowPath(slow_path);
7288
7289 // We have done the "if" of the gray bit check above, now branch based on the flags.
7290 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain7c1559a2015-12-15 10:55:36 +00007291
7292 // Object* ref = ref_addr->AsMirrorPtr()
7293 __ MaybeUnpoisonHeapReference(ref_reg);
7294
Roland Levillain7c1559a2015-12-15 10:55:36 +00007295 __ Bind(slow_path->GetExitLabel());
7296}
7297
7298void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
7299 Location out,
7300 Location ref,
7301 Location obj,
7302 uint32_t offset,
7303 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007304 DCHECK(kEmitCompilerReadBarrier);
7305
Roland Levillain7c1559a2015-12-15 10:55:36 +00007306 // Insert a slow path based read barrier *after* the reference load.
7307 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007308 // If heap poisoning is enabled, the unpoisoning of the loaded
7309 // reference will be carried out by the runtime within the slow
7310 // path.
7311 //
7312 // Note that `ref` currently does not get unpoisoned (when heap
7313 // poisoning is enabled), which is alright as the `ref` argument is
7314 // not used by the artReadBarrierSlow entry point.
7315 //
7316 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
7317 SlowPathCode* slow_path = new (GetGraph()->GetArena())
7318 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
7319 AddSlowPath(slow_path);
7320
Roland Levillain0d5a2812015-11-13 10:07:31 +00007321 __ jmp(slow_path->GetEntryLabel());
7322 __ Bind(slow_path->GetExitLabel());
7323}
7324
Roland Levillain7c1559a2015-12-15 10:55:36 +00007325void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7326 Location out,
7327 Location ref,
7328 Location obj,
7329 uint32_t offset,
7330 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007331 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007332 // Baker's read barriers shall be handled by the fast path
7333 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
7334 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007335 // If heap poisoning is enabled, unpoisoning will be taken care of
7336 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007337 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007338 } else if (kPoisonHeapReferences) {
7339 __ UnpoisonHeapReference(out.AsRegister<Register>());
7340 }
7341}
7342
Roland Levillain7c1559a2015-12-15 10:55:36 +00007343void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7344 Location out,
7345 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007346 DCHECK(kEmitCompilerReadBarrier);
7347
Roland Levillain7c1559a2015-12-15 10:55:36 +00007348 // Insert a slow path based read barrier *after* the GC root load.
7349 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007350 // Note that GC roots are not affected by heap poisoning, so we do
7351 // not need to do anything special for this here.
7352 SlowPathCode* slow_path =
7353 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86(instruction, out, root);
7354 AddSlowPath(slow_path);
7355
Roland Levillain0d5a2812015-11-13 10:07:31 +00007356 __ jmp(slow_path->GetEntryLabel());
7357 __ Bind(slow_path->GetExitLabel());
7358}
7359
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007360void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007361 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007362 LOG(FATAL) << "Unreachable";
7363}
7364
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007365void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007366 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007367 LOG(FATAL) << "Unreachable";
7368}
7369
Mark Mendellfe57faa2015-09-18 09:26:15 -04007370// Simple implementation of packed switch - generate cascaded compare/jumps.
7371void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7372 LocationSummary* locations =
7373 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7374 locations->SetInAt(0, Location::RequiresRegister());
7375}
7376
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007377void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
7378 int32_t lower_bound,
7379 uint32_t num_entries,
7380 HBasicBlock* switch_block,
7381 HBasicBlock* default_block) {
7382 // Figure out the correct compare values and jump conditions.
7383 // Handle the first compare/branch as a special case because it might
7384 // jump to the default case.
7385 DCHECK_GT(num_entries, 2u);
7386 Condition first_condition;
7387 uint32_t index;
7388 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
7389 if (lower_bound != 0) {
7390 first_condition = kLess;
7391 __ cmpl(value_reg, Immediate(lower_bound));
7392 __ j(first_condition, codegen_->GetLabelOf(default_block));
7393 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007394
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007395 index = 1;
7396 } else {
7397 // Handle all the compare/jumps below.
7398 first_condition = kBelow;
7399 index = 0;
7400 }
7401
7402 // Handle the rest of the compare/jumps.
7403 for (; index + 1 < num_entries; index += 2) {
7404 int32_t compare_to_value = lower_bound + index + 1;
7405 __ cmpl(value_reg, Immediate(compare_to_value));
7406 // Jump to successors[index] if value < case_value[index].
7407 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
7408 // Jump to successors[index + 1] if value == case_value[index + 1].
7409 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
7410 }
7411
7412 if (index != num_entries) {
7413 // There are an odd number of entries. Handle the last one.
7414 DCHECK_EQ(index + 1, num_entries);
7415 __ cmpl(value_reg, Immediate(lower_bound + index));
7416 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007417 }
7418
7419 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007420 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
7421 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007422 }
7423}
7424
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007425void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7426 int32_t lower_bound = switch_instr->GetStartValue();
7427 uint32_t num_entries = switch_instr->GetNumEntries();
7428 LocationSummary* locations = switch_instr->GetLocations();
7429 Register value_reg = locations->InAt(0).AsRegister<Register>();
7430
7431 GenPackedSwitchWithCompares(value_reg,
7432 lower_bound,
7433 num_entries,
7434 switch_instr->GetBlock(),
7435 switch_instr->GetDefaultBlock());
7436}
7437
Mark Mendell805b3b52015-09-18 14:10:29 -04007438void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7439 LocationSummary* locations =
7440 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7441 locations->SetInAt(0, Location::RequiresRegister());
7442
7443 // Constant area pointer.
7444 locations->SetInAt(1, Location::RequiresRegister());
7445
7446 // And the temporary we need.
7447 locations->AddTemp(Location::RequiresRegister());
7448}
7449
7450void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7451 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007452 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04007453 LocationSummary* locations = switch_instr->GetLocations();
7454 Register value_reg = locations->InAt(0).AsRegister<Register>();
7455 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7456
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007457 if (num_entries <= kPackedSwitchJumpTableThreshold) {
7458 GenPackedSwitchWithCompares(value_reg,
7459 lower_bound,
7460 num_entries,
7461 switch_instr->GetBlock(),
7462 default_block);
7463 return;
7464 }
7465
Mark Mendell805b3b52015-09-18 14:10:29 -04007466 // Optimizing has a jump area.
7467 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7468 Register constant_area = locations->InAt(1).AsRegister<Register>();
7469
7470 // Remove the bias, if needed.
7471 if (lower_bound != 0) {
7472 __ leal(temp_reg, Address(value_reg, -lower_bound));
7473 value_reg = temp_reg;
7474 }
7475
7476 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007477 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04007478 __ cmpl(value_reg, Immediate(num_entries - 1));
7479 __ j(kAbove, codegen_->GetLabelOf(default_block));
7480
7481 // We are in the range of the table.
7482 // Load (target-constant_area) from the jump table, indexing by the value.
7483 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
7484
7485 // Compute the actual target address by adding in constant_area.
7486 __ addl(temp_reg, constant_area);
7487
7488 // And jump.
7489 __ jmp(temp_reg);
7490}
7491
Mark Mendell0616ae02015-04-17 12:49:27 -04007492void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
7493 HX86ComputeBaseMethodAddress* insn) {
7494 LocationSummary* locations =
7495 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7496 locations->SetOut(Location::RequiresRegister());
7497}
7498
7499void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
7500 HX86ComputeBaseMethodAddress* insn) {
7501 LocationSummary* locations = insn->GetLocations();
7502 Register reg = locations->Out().AsRegister<Register>();
7503
7504 // Generate call to next instruction.
7505 Label next_instruction;
7506 __ call(&next_instruction);
7507 __ Bind(&next_instruction);
7508
7509 // Remember this offset for later use with constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007510 codegen_->AddMethodAddressOffset(insn, GetAssembler()->CodeSize());
Mark Mendell0616ae02015-04-17 12:49:27 -04007511
7512 // Grab the return address off the stack.
7513 __ popl(reg);
7514}
7515
7516void LocationsBuilderX86::VisitX86LoadFromConstantTable(
7517 HX86LoadFromConstantTable* insn) {
7518 LocationSummary* locations =
7519 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7520
7521 locations->SetInAt(0, Location::RequiresRegister());
7522 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
7523
7524 // If we don't need to be materialized, we only need the inputs to be set.
David Brazdilb3e773e2016-01-26 11:28:37 +00007525 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007526 return;
7527 }
7528
7529 switch (insn->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007530 case DataType::Type::kFloat32:
7531 case DataType::Type::kFloat64:
Mark Mendell0616ae02015-04-17 12:49:27 -04007532 locations->SetOut(Location::RequiresFpuRegister());
7533 break;
7534
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007535 case DataType::Type::kInt32:
Mark Mendell0616ae02015-04-17 12:49:27 -04007536 locations->SetOut(Location::RequiresRegister());
7537 break;
7538
7539 default:
7540 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7541 }
7542}
7543
7544void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
David Brazdilb3e773e2016-01-26 11:28:37 +00007545 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007546 return;
7547 }
7548
7549 LocationSummary* locations = insn->GetLocations();
7550 Location out = locations->Out();
7551 Register const_area = locations->InAt(0).AsRegister<Register>();
7552 HConstant *value = insn->GetConstant();
7553
7554 switch (insn->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007555 case DataType::Type::kFloat32:
Mark Mendell0616ae02015-04-17 12:49:27 -04007556 __ movss(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007557 codegen_->LiteralFloatAddress(
7558 value->AsFloatConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04007559 break;
7560
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007561 case DataType::Type::kFloat64:
Mark Mendell0616ae02015-04-17 12:49:27 -04007562 __ movsd(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007563 codegen_->LiteralDoubleAddress(
7564 value->AsDoubleConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04007565 break;
7566
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007567 case DataType::Type::kInt32:
Mark Mendell0616ae02015-04-17 12:49:27 -04007568 __ movl(out.AsRegister<Register>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007569 codegen_->LiteralInt32Address(
7570 value->AsIntConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04007571 break;
7572
7573 default:
7574 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7575 }
7576}
7577
Mark Mendell0616ae02015-04-17 12:49:27 -04007578/**
7579 * Class to handle late fixup of offsets into constant area.
7580 */
Vladimir Marko5233f932015-09-29 19:01:15 +01007581class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04007582 public:
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007583 RIPFixup(CodeGeneratorX86& codegen,
7584 HX86ComputeBaseMethodAddress* base_method_address,
7585 size_t offset)
7586 : codegen_(&codegen),
7587 base_method_address_(base_method_address),
7588 offset_into_constant_area_(offset) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04007589
7590 protected:
7591 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
7592
7593 CodeGeneratorX86* codegen_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007594 HX86ComputeBaseMethodAddress* base_method_address_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007595
7596 private:
7597 void Process(const MemoryRegion& region, int pos) OVERRIDE {
7598 // Patch the correct offset for the instruction. The place to patch is the
7599 // last 4 bytes of the instruction.
7600 // The value to patch is the distance from the offset in the constant area
7601 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04007602 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007603 int32_t relative_position =
7604 constant_offset - codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell0616ae02015-04-17 12:49:27 -04007605
7606 // Patch in the right value.
7607 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
7608 }
7609
Mark Mendell0616ae02015-04-17 12:49:27 -04007610 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04007611 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007612};
7613
Mark Mendell805b3b52015-09-18 14:10:29 -04007614/**
7615 * Class to handle late fixup of offsets to a jump table that will be created in the
7616 * constant area.
7617 */
7618class JumpTableRIPFixup : public RIPFixup {
7619 public:
7620 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007621 : RIPFixup(codegen, switch_instr->GetBaseMethodAddress(), static_cast<size_t>(-1)),
7622 switch_instr_(switch_instr) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04007623
7624 void CreateJumpTable() {
7625 X86Assembler* assembler = codegen_->GetAssembler();
7626
7627 // Ensure that the reference to the jump table has the correct offset.
7628 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
7629 SetOffset(offset_in_constant_table);
7630
7631 // The label values in the jump table are computed relative to the
7632 // instruction addressing the constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007633 const int32_t relative_offset = codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell805b3b52015-09-18 14:10:29 -04007634
7635 // Populate the jump table with the correct values for the jump table.
7636 int32_t num_entries = switch_instr_->GetNumEntries();
7637 HBasicBlock* block = switch_instr_->GetBlock();
7638 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
7639 // The value that we want is the target offset - the position of the table.
7640 for (int32_t i = 0; i < num_entries; i++) {
7641 HBasicBlock* b = successors[i];
7642 Label* l = codegen_->GetLabelOf(b);
7643 DCHECK(l->IsBound());
7644 int32_t offset_to_block = l->Position() - relative_offset;
7645 assembler->AppendInt32(offset_to_block);
7646 }
7647 }
7648
7649 private:
7650 const HX86PackedSwitch* switch_instr_;
7651};
7652
7653void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
7654 // Generate the constant area if needed.
7655 X86Assembler* assembler = GetAssembler();
7656 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
7657 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
7658 // byte values.
7659 assembler->Align(4, 0);
7660 constant_area_start_ = assembler->CodeSize();
7661
7662 // Populate any jump tables.
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007663 for (JumpTableRIPFixup* jump_table : fixups_to_jump_tables_) {
Mark Mendell805b3b52015-09-18 14:10:29 -04007664 jump_table->CreateJumpTable();
7665 }
7666
7667 // And now add the constant area to the generated code.
7668 assembler->AddConstantArea();
7669 }
7670
7671 // And finish up.
7672 CodeGenerator::Finalize(allocator);
7673}
7674
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007675Address CodeGeneratorX86::LiteralDoubleAddress(double v,
7676 HX86ComputeBaseMethodAddress* method_base,
7677 Register reg) {
7678 AssemblerFixup* fixup =
7679 new (GetGraph()->GetArena()) RIPFixup(*this, method_base, __ AddDouble(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04007680 return Address(reg, kDummy32BitOffset, fixup);
7681}
7682
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007683Address CodeGeneratorX86::LiteralFloatAddress(float v,
7684 HX86ComputeBaseMethodAddress* method_base,
7685 Register reg) {
7686 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, method_base, __ AddFloat(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04007687 return Address(reg, kDummy32BitOffset, fixup);
7688}
7689
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007690Address CodeGeneratorX86::LiteralInt32Address(int32_t v,
7691 HX86ComputeBaseMethodAddress* method_base,
7692 Register reg) {
7693 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, method_base, __ AddInt32(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04007694 return Address(reg, kDummy32BitOffset, fixup);
7695}
7696
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007697Address CodeGeneratorX86::LiteralInt64Address(int64_t v,
7698 HX86ComputeBaseMethodAddress* method_base,
7699 Register reg) {
7700 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, method_base, __ AddInt64(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04007701 return Address(reg, kDummy32BitOffset, fixup);
7702}
7703
Aart Bika19616e2016-02-01 18:57:58 -08007704void CodeGeneratorX86::Load32BitValue(Register dest, int32_t value) {
7705 if (value == 0) {
7706 __ xorl(dest, dest);
7707 } else {
7708 __ movl(dest, Immediate(value));
7709 }
7710}
7711
7712void CodeGeneratorX86::Compare32BitValue(Register dest, int32_t value) {
7713 if (value == 0) {
7714 __ testl(dest, dest);
7715 } else {
7716 __ cmpl(dest, Immediate(value));
7717 }
7718}
7719
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007720void CodeGeneratorX86::GenerateIntCompare(Location lhs, Location rhs) {
7721 Register lhs_reg = lhs.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07007722 GenerateIntCompare(lhs_reg, rhs);
7723}
7724
7725void CodeGeneratorX86::GenerateIntCompare(Register lhs, Location rhs) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007726 if (rhs.IsConstant()) {
7727 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
jessicahandojo4877b792016-09-08 19:49:13 -07007728 Compare32BitValue(lhs, value);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007729 } else if (rhs.IsStackSlot()) {
jessicahandojo4877b792016-09-08 19:49:13 -07007730 __ cmpl(lhs, Address(ESP, rhs.GetStackIndex()));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007731 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07007732 __ cmpl(lhs, rhs.AsRegister<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007733 }
7734}
7735
7736Address CodeGeneratorX86::ArrayAddress(Register obj,
7737 Location index,
7738 ScaleFactor scale,
7739 uint32_t data_offset) {
7740 return index.IsConstant() ?
7741 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << scale) + data_offset) :
7742 Address(obj, index.AsRegister<Register>(), scale, data_offset);
7743}
7744
Mark Mendell805b3b52015-09-18 14:10:29 -04007745Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
7746 Register reg,
7747 Register value) {
7748 // Create a fixup to be used to create and address the jump table.
7749 JumpTableRIPFixup* table_fixup =
7750 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
7751
7752 // We have to populate the jump tables.
7753 fixups_to_jump_tables_.push_back(table_fixup);
7754
7755 // We want a scaled address, as we are extracting the correct offset from the table.
7756 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
7757}
7758
Andreas Gampe85b62f22015-09-09 13:15:38 -07007759// TODO: target as memory.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007760void CodeGeneratorX86::MoveFromReturnRegister(Location target, DataType::Type type) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07007761 if (!target.IsValid()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007762 DCHECK_EQ(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007763 return;
7764 }
7765
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007766 DCHECK_NE(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007767
7768 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
7769 if (target.Equals(return_loc)) {
7770 return;
7771 }
7772
7773 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7774 // with the else branch.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007775 if (type == DataType::Type::kInt64) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07007776 HParallelMove parallel_move(GetGraph()->GetArena());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007777 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), DataType::Type::kInt32, nullptr);
7778 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), DataType::Type::kInt32, nullptr);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007779 GetMoveResolver()->EmitNativeCode(&parallel_move);
7780 } else {
7781 // Let the parallel move resolver take care of all of this.
7782 HParallelMove parallel_move(GetGraph()->GetArena());
7783 parallel_move.AddMove(return_loc, target, type, nullptr);
7784 GetMoveResolver()->EmitNativeCode(&parallel_move);
7785 }
7786}
7787
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007788void CodeGeneratorX86::PatchJitRootUse(uint8_t* code,
7789 const uint8_t* roots_data,
7790 const PatchInfo<Label>& info,
7791 uint64_t index_in_table) const {
7792 uint32_t code_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
7793 uintptr_t address =
7794 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
7795 typedef __attribute__((__aligned__(1))) uint32_t unaligned_uint32_t;
7796 reinterpret_cast<unaligned_uint32_t*>(code + code_offset)[0] =
7797 dchecked_integral_cast<uint32_t>(address);
7798}
7799
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007800void CodeGeneratorX86::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
7801 for (const PatchInfo<Label>& info : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007802 const auto it = jit_string_roots_.find(
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007803 StringReference(&info.dex_file, dex::StringIndex(info.index)));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007804 DCHECK(it != jit_string_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007805 uint64_t index_in_table = it->second;
7806 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007807 }
7808
7809 for (const PatchInfo<Label>& info : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007810 const auto it = jit_class_roots_.find(
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007811 TypeReference(&info.dex_file, dex::TypeIndex(info.index)));
7812 DCHECK(it != jit_class_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007813 uint64_t index_in_table = it->second;
7814 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007815 }
7816}
7817
Roland Levillain4d027112015-07-01 15:41:14 +01007818#undef __
7819
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007820} // namespace x86
7821} // namespace art