blob: e45ad0a9a3fef543f91faac7097c341343b23b62 [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"
Mark Mendell09ed1a32015-03-25 08:30:06 -040026#include "intrinsics.h"
27#include "intrinsics_x86.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070028#include "lock_word.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070029#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070030#include "mirror/class-inl.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010031#include "thread.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000032#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010033#include "utils/stack_checks.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000034#include "utils/x86/assembler_x86.h"
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010035#include "utils/x86/managed_register_x86.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000036
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000037namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010038
Roland Levillain0d5a2812015-11-13 10:07:31 +000039template<class MirrorType>
40class GcRoot;
41
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000042namespace x86 {
43
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010044static constexpr int kCurrentMethodStackOffset = 0;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010045static constexpr Register kMethodRegisterArgument = EAX;
Mark Mendell5f874182015-03-04 15:42:45 -050046static constexpr Register kCoreCalleeSaves[] = { EBP, ESI, EDI };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010047
Mark Mendell24f2dfa2015-01-14 19:51:45 -050048static constexpr int kC2ConditionMask = 0x400;
49
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000050static constexpr int kFakeReturnRegister = Register(8);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +000051
Roland Levillain7cbd27f2016-08-11 23:53:33 +010052// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
53#define __ down_cast<X86Assembler*>(codegen->GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -070054#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kX86PointerSize, x).Int32Value()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010055
Andreas Gampe85b62f22015-09-09 13:15:38 -070056class NullCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010057 public:
David Srbecky9cd6d372016-02-09 15:24:47 +000058 explicit NullCheckSlowPathX86(HNullCheck* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010059
Alexandre Rames2ed20af2015-03-06 13:55:35 +000060 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +010061 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010062 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000063 if (instruction_->CanThrowIntoCatchBlock()) {
64 // Live registers will be restored in the catch block if caught.
65 SaveLiveRegisters(codegen, instruction_->GetLocations());
66 }
Serban Constantinescuba45db02016-07-12 22:53:02 +010067 x86_codegen->InvokeRuntime(kQuickThrowNullPointer,
Alexandre Rames8158f282015-08-07 10:26:17 +010068 instruction_,
69 instruction_->GetDexPc(),
70 this);
Roland Levillain888d0672015-11-23 18:53:50 +000071 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +010072 }
73
Alexandre Rames8158f282015-08-07 10:26:17 +010074 bool IsFatal() const OVERRIDE { return true; }
75
Alexandre Rames9931f312015-06-19 14:47:01 +010076 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathX86"; }
77
Nicolas Geoffraye5038322014-07-04 09:41:32 +010078 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +010079 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86);
80};
81
Andreas Gampe85b62f22015-09-09 13:15:38 -070082class DivZeroCheckSlowPathX86 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +000083 public:
David Srbecky9cd6d372016-02-09 15:24:47 +000084 explicit DivZeroCheckSlowPathX86(HDivZeroCheck* instruction) : SlowPathCode(instruction) {}
Calin Juravled0d48522014-11-04 16:40:20 +000085
Alexandre Rames2ed20af2015-03-06 13:55:35 +000086 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +010087 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Calin Juravled0d48522014-11-04 16:40:20 +000088 __ Bind(GetEntryLabel());
Serban Constantinescuba45db02016-07-12 22:53:02 +010089 x86_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +000090 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Calin Juravled0d48522014-11-04 16:40:20 +000091 }
92
Alexandre Rames8158f282015-08-07 10:26:17 +010093 bool IsFatal() const OVERRIDE { return true; }
94
Alexandre Rames9931f312015-06-19 14:47:01 +010095 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathX86"; }
96
Calin Juravled0d48522014-11-04 16:40:20 +000097 private:
Calin Juravled0d48522014-11-04 16:40:20 +000098 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86);
99};
100
Andreas Gampe85b62f22015-09-09 13:15:38 -0700101class DivRemMinusOneSlowPathX86 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +0000102 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000103 DivRemMinusOneSlowPathX86(HInstruction* instruction, Register reg, bool is_div)
104 : SlowPathCode(instruction), reg_(reg), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000105
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000106 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +0000107 __ Bind(GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +0000108 if (is_div_) {
109 __ negl(reg_);
110 } else {
111 __ movl(reg_, Immediate(0));
112 }
Calin Juravled0d48522014-11-04 16:40:20 +0000113 __ jmp(GetExitLabel());
114 }
115
Alexandre Rames9931f312015-06-19 14:47:01 +0100116 const char* GetDescription() const OVERRIDE { return "DivRemMinusOneSlowPathX86"; }
117
Calin Juravled0d48522014-11-04 16:40:20 +0000118 private:
119 Register reg_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000120 bool is_div_;
121 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86);
Calin Juravled0d48522014-11-04 16:40:20 +0000122};
123
Andreas Gampe85b62f22015-09-09 13:15:38 -0700124class BoundsCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100125 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000126 explicit BoundsCheckSlowPathX86(HBoundsCheck* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100127
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000128 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100129 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100130 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100131 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000132 // We're moving two locations to locations that could overlap, so we need a parallel
133 // move resolver.
David Brazdil77a48ae2015-09-15 12:34:04 +0000134 if (instruction_->CanThrowIntoCatchBlock()) {
135 // Live registers will be restored in the catch block if caught.
136 SaveLiveRegisters(codegen, instruction_->GetLocations());
137 }
Mark Mendellee8d9712016-07-12 11:13:15 -0400138
139 // Are we using an array length from memory?
140 HInstruction* array_length = instruction_->InputAt(1);
141 Location length_loc = locations->InAt(1);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100142 InvokeRuntimeCallingConvention calling_convention;
Mark Mendellee8d9712016-07-12 11:13:15 -0400143 if (array_length->IsArrayLength() && array_length->IsEmittedAtUseSite()) {
144 // Load the array length into our temporary.
145 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
146 Location array_loc = array_length->GetLocations()->InAt(0);
147 Address array_len(array_loc.AsRegister<Register>(), len_offset);
148 length_loc = Location::RegisterLocation(calling_convention.GetRegisterAt(1));
149 // Check for conflicts with index.
150 if (length_loc.Equals(locations->InAt(0))) {
151 // We know we aren't using parameter 2.
152 length_loc = Location::RegisterLocation(calling_convention.GetRegisterAt(2));
153 }
154 __ movl(length_loc.AsRegister<Register>(), array_len);
jessicahandojo4877b792016-09-08 19:49:13 -0700155 if (mirror::kUseStringCompression) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +0100156 __ shrl(length_loc.AsRegister<Register>(), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -0700157 }
Mark Mendellee8d9712016-07-12 11:13:15 -0400158 }
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000159 x86_codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100160 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000161 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100162 Primitive::kPrimInt,
Mark Mendellee8d9712016-07-12 11:13:15 -0400163 length_loc,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100164 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
165 Primitive::kPrimInt);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100166 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
167 ? kQuickThrowStringBounds
168 : kQuickThrowArrayBounds;
169 x86_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100170 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Roland Levillain888d0672015-11-23 18:53:50 +0000171 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100172 }
173
Alexandre Rames8158f282015-08-07 10:26:17 +0100174 bool IsFatal() const OVERRIDE { return true; }
175
Alexandre Rames9931f312015-06-19 14:47:01 +0100176 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathX86"; }
177
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100178 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100179 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
180};
181
Andreas Gampe85b62f22015-09-09 13:15:38 -0700182class SuspendCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000183 public:
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000184 SuspendCheckSlowPathX86(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000185 : SlowPathCode(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000186
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000187 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bikb13c65b2017-03-21 20:14:07 -0700188 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100189 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000190 __ Bind(GetEntryLabel());
Aart Bik24b905f2017-04-06 09:59:06 -0700191 SaveLiveRegisters(codegen, locations); // Only saves full width XMM for SIMD.
Serban Constantinescuba45db02016-07-12 22:53:02 +0100192 x86_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000193 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Aart Bik24b905f2017-04-06 09:59:06 -0700194 RestoreLiveRegisters(codegen, locations); // Only restores full width XMM for SIMD.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100195 if (successor_ == nullptr) {
196 __ jmp(GetReturnLabel());
197 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100198 __ jmp(x86_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100199 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000200 }
201
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100202 Label* GetReturnLabel() {
203 DCHECK(successor_ == nullptr);
204 return &return_label_;
205 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000206
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100207 HBasicBlock* GetSuccessor() const {
208 return successor_;
209 }
210
Alexandre Rames9931f312015-06-19 14:47:01 +0100211 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathX86"; }
212
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000213 private:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100214 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000215 Label return_label_;
216
217 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
218};
219
Vladimir Markoaad75c62016-10-03 08:46:48 +0000220class LoadStringSlowPathX86 : public SlowPathCode {
221 public:
222 explicit LoadStringSlowPathX86(HLoadString* instruction): SlowPathCode(instruction) {}
223
224 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
225 LocationSummary* locations = instruction_->GetLocations();
226 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
227
228 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
229 __ Bind(GetEntryLabel());
230 SaveLiveRegisters(codegen, locations);
231
232 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000233 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
234 __ movl(calling_convention.GetRegisterAt(0), Immediate(string_index.index_));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000235 x86_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
236 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
237 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
238 RestoreLiveRegisters(codegen, locations);
239
240 // Store the resolved String to the BSS entry.
241 Register method_address = locations->InAt(0).AsRegister<Register>();
242 __ movl(Address(method_address, CodeGeneratorX86::kDummy32BitOffset),
243 locations->Out().AsRegister<Register>());
244 Label* fixup_label = x86_codegen->NewStringBssEntryPatch(instruction_->AsLoadString());
245 __ Bind(fixup_label);
246
247 __ jmp(GetExitLabel());
248 }
249
250 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathX86"; }
251
252 private:
253 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86);
254};
255
Andreas Gampe85b62f22015-09-09 13:15:38 -0700256class LoadClassSlowPathX86 : public SlowPathCode {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000257 public:
258 LoadClassSlowPathX86(HLoadClass* cls,
259 HInstruction* at,
260 uint32_t dex_pc,
261 bool do_clinit)
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000262 : SlowPathCode(at), cls_(cls), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000263 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
264 }
265
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000266 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000267 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000268 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
269 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000270 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000271
272 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000273 dex::TypeIndex type_index = cls_->GetTypeIndex();
274 __ movl(calling_convention.GetRegisterAt(0), Immediate(type_index.index_));
Serban Constantinescuba45db02016-07-12 22:53:02 +0100275 x86_codegen->InvokeRuntime(do_clinit_ ? kQuickInitializeStaticStorage
276 : kQuickInitializeType,
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000277 instruction_,
278 dex_pc_,
279 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000280 if (do_clinit_) {
281 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
282 } else {
283 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
284 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000285
286 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000287 Location out = locations->Out();
288 if (out.IsValid()) {
289 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
290 x86_codegen->Move32(out, Location::RegisterLocation(EAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000291 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000292 RestoreLiveRegisters(codegen, locations);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000293 // For HLoadClass/kBssEntry, store the resolved Class to the BSS entry.
294 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
295 if (cls_ == instruction_ && cls_->GetLoadKind() == HLoadClass::LoadKind::kBssEntry) {
296 DCHECK(out.IsValid());
297 Register method_address = locations->InAt(0).AsRegister<Register>();
298 __ movl(Address(method_address, CodeGeneratorX86::kDummy32BitOffset),
299 locations->Out().AsRegister<Register>());
300 Label* fixup_label = x86_codegen->NewTypeBssEntryPatch(cls_);
301 __ Bind(fixup_label);
302 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000303 __ jmp(GetExitLabel());
304 }
305
Alexandre Rames9931f312015-06-19 14:47:01 +0100306 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathX86"; }
307
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000308 private:
309 // The class this slow path will load.
310 HLoadClass* const cls_;
311
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000312 // The dex PC of `at_`.
313 const uint32_t dex_pc_;
314
315 // Whether to initialize the class.
316 const bool do_clinit_;
317
318 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
319};
320
Andreas Gampe85b62f22015-09-09 13:15:38 -0700321class TypeCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000322 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000323 TypeCheckSlowPathX86(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000324 : SlowPathCode(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000325
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000326 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000327 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000328 DCHECK(instruction_->IsCheckCast()
329 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000330
331 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
332 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000333
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000334 if (!is_fatal_) {
335 SaveLiveRegisters(codegen, locations);
336 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000337
338 // We're moving two locations to locations that could overlap, so we need a parallel
339 // move resolver.
340 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800341 x86_codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800342 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
343 Primitive::kPrimNot,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800344 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800345 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
346 Primitive::kPrimNot);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000347 if (instruction_->IsInstanceOf()) {
Serban Constantinescuba45db02016-07-12 22:53:02 +0100348 x86_codegen->InvokeRuntime(kQuickInstanceofNonTrivial,
Alexandre Rames8158f282015-08-07 10:26:17 +0100349 instruction_,
350 instruction_->GetDexPc(),
351 this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800352 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000353 } else {
354 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800355 x86_codegen->InvokeRuntime(kQuickCheckInstanceOf,
356 instruction_,
357 instruction_->GetDexPc(),
358 this);
359 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000360 }
361
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000362 if (!is_fatal_) {
363 if (instruction_->IsInstanceOf()) {
364 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
365 }
366 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray75374372015-09-17 17:12:19 +0000367
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000368 __ jmp(GetExitLabel());
369 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000370 }
371
Alexandre Rames9931f312015-06-19 14:47:01 +0100372 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathX86"; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000373 bool IsFatal() const OVERRIDE { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100374
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000375 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000376 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000377
378 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
379};
380
Andreas Gampe85b62f22015-09-09 13:15:38 -0700381class DeoptimizationSlowPathX86 : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700382 public:
Aart Bik42249c32016-01-07 15:33:50 -0800383 explicit DeoptimizationSlowPathX86(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000384 : SlowPathCode(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700385
386 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +0100387 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700388 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100389 LocationSummary* locations = instruction_->GetLocations();
390 SaveLiveRegisters(codegen, locations);
391 InvokeRuntimeCallingConvention calling_convention;
392 x86_codegen->Load32BitValue(
393 calling_convention.GetRegisterAt(0),
394 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescuba45db02016-07-12 22:53:02 +0100395 x86_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100396 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700397 }
398
Alexandre Rames9931f312015-06-19 14:47:01 +0100399 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86"; }
400
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700401 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700402 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86);
403};
404
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100405class ArraySetSlowPathX86 : public SlowPathCode {
406 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000407 explicit ArraySetSlowPathX86(HInstruction* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100408
409 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
410 LocationSummary* locations = instruction_->GetLocations();
411 __ Bind(GetEntryLabel());
412 SaveLiveRegisters(codegen, locations);
413
414 InvokeRuntimeCallingConvention calling_convention;
415 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
416 parallel_move.AddMove(
417 locations->InAt(0),
418 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
419 Primitive::kPrimNot,
420 nullptr);
421 parallel_move.AddMove(
422 locations->InAt(1),
423 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
424 Primitive::kPrimInt,
425 nullptr);
426 parallel_move.AddMove(
427 locations->InAt(2),
428 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
429 Primitive::kPrimNot,
430 nullptr);
431 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
432
433 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100434 x86_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000435 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100436 RestoreLiveRegisters(codegen, locations);
437 __ jmp(GetExitLabel());
438 }
439
440 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathX86"; }
441
442 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100443 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathX86);
444};
445
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100446// Slow path marking an object reference `ref` during a read
447// barrier. The field `obj.field` in the object `obj` holding this
448// reference does not get updated by this slow path after marking (see
449// ReadBarrierMarkAndUpdateFieldSlowPathX86 below for that).
450//
451// This means that after the execution of this slow path, `ref` will
452// always be up-to-date, but `obj.field` may not; i.e., after the
453// flip, `ref` will be a to-space reference, but `obj.field` will
454// probably still be a from-space reference (unless it gets updated by
455// another thread, or if another thread installed another object
456// reference (different from `ref`) in `obj.field`).
Roland Levillain7c1559a2015-12-15 10:55:36 +0000457class ReadBarrierMarkSlowPathX86 : public SlowPathCode {
458 public:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100459 ReadBarrierMarkSlowPathX86(HInstruction* instruction,
460 Location ref,
461 bool unpoison_ref_before_marking)
462 : SlowPathCode(instruction),
463 ref_(ref),
464 unpoison_ref_before_marking_(unpoison_ref_before_marking) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000465 DCHECK(kEmitCompilerReadBarrier);
466 }
467
468 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathX86"; }
469
470 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
471 LocationSummary* locations = instruction_->GetLocations();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100472 Register ref_reg = ref_.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +0000473 DCHECK(locations->CanCall());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100474 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
Roland Levillain7c1559a2015-12-15 10:55:36 +0000475 DCHECK(instruction_->IsInstanceFieldGet() ||
476 instruction_->IsStaticFieldGet() ||
477 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100478 instruction_->IsArraySet() ||
Roland Levillain7c1559a2015-12-15 10:55:36 +0000479 instruction_->IsLoadClass() ||
480 instruction_->IsLoadString() ||
481 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100482 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100483 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
484 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain7c1559a2015-12-15 10:55:36 +0000485 << "Unexpected instruction in read barrier marking slow path: "
486 << instruction_->DebugName();
487
488 __ Bind(GetEntryLabel());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100489 if (unpoison_ref_before_marking_) {
Vladimir Marko953437b2016-08-24 08:30:46 +0000490 // Object* ref = ref_addr->AsMirrorPtr()
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100491 __ MaybeUnpoisonHeapReference(ref_reg);
Vladimir Marko953437b2016-08-24 08:30:46 +0000492 }
Roland Levillain4359e612016-07-20 11:32:19 +0100493 // No need to save live registers; it's taken care of by the
494 // entrypoint. Also, there is no need to update the stack mask,
495 // as this runtime call will not trigger a garbage collection.
Roland Levillain7c1559a2015-12-15 10:55:36 +0000496 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100497 DCHECK_NE(ref_reg, ESP);
498 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCpuRegisters) << ref_reg;
Roland Levillain02b75802016-07-13 11:54:35 +0100499 // "Compact" slow path, saving two moves.
500 //
501 // Instead of using the standard runtime calling convention (input
502 // and output in EAX):
503 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100504 // EAX <- ref
Roland Levillain02b75802016-07-13 11:54:35 +0100505 // EAX <- ReadBarrierMark(EAX)
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100506 // ref <- EAX
Roland Levillain02b75802016-07-13 11:54:35 +0100507 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100508 // we just use rX (the register containing `ref`) as input and output
Roland Levillain02b75802016-07-13 11:54:35 +0100509 // of a dedicated entrypoint:
510 //
511 // rX <- ReadBarrierMarkRegX(rX)
512 //
Roland Levillain97c46462017-05-11 14:04:03 +0100513 int32_t entry_point_offset = Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(ref_reg);
Roland Levillaindec8f632016-07-22 17:10:06 +0100514 // This runtime call does not require a stack map.
515 x86_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillain7c1559a2015-12-15 10:55:36 +0000516 __ jmp(GetExitLabel());
517 }
518
519 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100520 // The location (register) of the marked object reference.
521 const Location ref_;
522 // Should the reference in `ref_` be unpoisoned prior to marking it?
523 const bool unpoison_ref_before_marking_;
Roland Levillain7c1559a2015-12-15 10:55:36 +0000524
525 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86);
526};
527
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100528// Slow path marking an object reference `ref` during a read barrier,
529// and if needed, atomically updating the field `obj.field` in the
530// object `obj` holding this reference after marking (contrary to
531// ReadBarrierMarkSlowPathX86 above, which never tries to update
532// `obj.field`).
533//
534// This means that after the execution of this slow path, both `ref`
535// and `obj.field` will be up-to-date; i.e., after the flip, both will
536// hold the same to-space reference (unless another thread installed
537// another object reference (different from `ref`) in `obj.field`).
538class ReadBarrierMarkAndUpdateFieldSlowPathX86 : public SlowPathCode {
539 public:
540 ReadBarrierMarkAndUpdateFieldSlowPathX86(HInstruction* instruction,
541 Location ref,
542 Register obj,
543 const Address& field_addr,
544 bool unpoison_ref_before_marking,
545 Register temp)
546 : SlowPathCode(instruction),
547 ref_(ref),
548 obj_(obj),
549 field_addr_(field_addr),
550 unpoison_ref_before_marking_(unpoison_ref_before_marking),
551 temp_(temp) {
552 DCHECK(kEmitCompilerReadBarrier);
553 }
554
555 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkAndUpdateFieldSlowPathX86"; }
556
557 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
558 LocationSummary* locations = instruction_->GetLocations();
559 Register ref_reg = ref_.AsRegister<Register>();
560 DCHECK(locations->CanCall());
561 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
562 // This slow path is only used by the UnsafeCASObject intrinsic.
563 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
564 << "Unexpected instruction in read barrier marking and field updating slow path: "
565 << instruction_->DebugName();
566 DCHECK(instruction_->GetLocations()->Intrinsified());
567 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
568
569 __ Bind(GetEntryLabel());
570 if (unpoison_ref_before_marking_) {
571 // Object* ref = ref_addr->AsMirrorPtr()
572 __ MaybeUnpoisonHeapReference(ref_reg);
573 }
574
575 // Save the old (unpoisoned) reference.
576 __ movl(temp_, ref_reg);
577
578 // No need to save live registers; it's taken care of by the
579 // entrypoint. Also, there is no need to update the stack mask,
580 // as this runtime call will not trigger a garbage collection.
581 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
582 DCHECK_NE(ref_reg, ESP);
583 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCpuRegisters) << ref_reg;
584 // "Compact" slow path, saving two moves.
585 //
586 // Instead of using the standard runtime calling convention (input
587 // and output in EAX):
588 //
589 // EAX <- ref
590 // EAX <- ReadBarrierMark(EAX)
591 // ref <- EAX
592 //
593 // we just use rX (the register containing `ref`) as input and output
594 // of a dedicated entrypoint:
595 //
596 // rX <- ReadBarrierMarkRegX(rX)
597 //
Roland Levillain97c46462017-05-11 14:04:03 +0100598 int32_t entry_point_offset = Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(ref_reg);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100599 // This runtime call does not require a stack map.
600 x86_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
601
602 // If the new reference is different from the old reference,
603 // update the field in the holder (`*field_addr`).
604 //
605 // Note that this field could also hold a different object, if
606 // another thread had concurrently changed it. In that case, the
607 // LOCK CMPXCHGL instruction in the compare-and-set (CAS)
608 // operation below would abort the CAS, leaving the field as-is.
609 NearLabel done;
610 __ cmpl(temp_, ref_reg);
611 __ j(kEqual, &done);
612
613 // Update the the holder's field atomically. This may fail if
614 // mutator updates before us, but it's OK. This is achieved
615 // using a strong compare-and-set (CAS) operation with relaxed
616 // memory synchronization ordering, where the expected value is
617 // the old reference and the desired value is the new reference.
618 // This operation is implemented with a 32-bit LOCK CMPXLCHG
619 // instruction, which requires the expected value (the old
620 // reference) to be in EAX. Save EAX beforehand, and move the
621 // expected value (stored in `temp_`) into EAX.
622 __ pushl(EAX);
623 __ movl(EAX, temp_);
624
625 // Convenience aliases.
626 Register base = obj_;
627 Register expected = EAX;
628 Register value = ref_reg;
629
630 bool base_equals_value = (base == value);
631 if (kPoisonHeapReferences) {
632 if (base_equals_value) {
633 // If `base` and `value` are the same register location, move
634 // `value` to a temporary register. This way, poisoning
635 // `value` won't invalidate `base`.
636 value = temp_;
637 __ movl(value, base);
638 }
639
640 // Check that the register allocator did not assign the location
641 // of `expected` (EAX) to `value` nor to `base`, so that heap
642 // poisoning (when enabled) works as intended below.
643 // - If `value` were equal to `expected`, both references would
644 // be poisoned twice, meaning they would not be poisoned at
645 // all, as heap poisoning uses address negation.
646 // - If `base` were equal to `expected`, poisoning `expected`
647 // would invalidate `base`.
648 DCHECK_NE(value, expected);
649 DCHECK_NE(base, expected);
650
651 __ PoisonHeapReference(expected);
652 __ PoisonHeapReference(value);
653 }
654
655 __ LockCmpxchgl(field_addr_, value);
656
657 // If heap poisoning is enabled, we need to unpoison the values
658 // that were poisoned earlier.
659 if (kPoisonHeapReferences) {
660 if (base_equals_value) {
661 // `value` has been moved to a temporary register, no need
662 // to unpoison it.
663 } else {
664 __ UnpoisonHeapReference(value);
665 }
666 // No need to unpoison `expected` (EAX), as it is be overwritten below.
667 }
668
669 // Restore EAX.
670 __ popl(EAX);
671
672 __ Bind(&done);
673 __ jmp(GetExitLabel());
674 }
675
676 private:
677 // The location (register) of the marked object reference.
678 const Location ref_;
679 // The register containing the object holding the marked object reference field.
680 const Register obj_;
681 // The address of the marked reference field. The base of this address must be `obj_`.
682 const Address field_addr_;
683
684 // Should the reference in `ref_` be unpoisoned prior to marking it?
685 const bool unpoison_ref_before_marking_;
686
687 const Register temp_;
688
689 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathX86);
690};
691
Roland Levillain0d5a2812015-11-13 10:07:31 +0000692// Slow path generating a read barrier for a heap reference.
693class ReadBarrierForHeapReferenceSlowPathX86 : public SlowPathCode {
694 public:
695 ReadBarrierForHeapReferenceSlowPathX86(HInstruction* instruction,
696 Location out,
697 Location ref,
698 Location obj,
699 uint32_t offset,
700 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000701 : SlowPathCode(instruction),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000702 out_(out),
703 ref_(ref),
704 obj_(obj),
705 offset_(offset),
706 index_(index) {
707 DCHECK(kEmitCompilerReadBarrier);
708 // If `obj` is equal to `out` or `ref`, it means the initial object
709 // has been overwritten by (or after) the heap object reference load
710 // to be instrumented, e.g.:
711 //
712 // __ movl(out, Address(out, offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000713 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000714 //
715 // In that case, we have lost the information about the original
716 // object, and the emitted read barrier cannot work properly.
717 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
718 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
719 }
720
721 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
722 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
723 LocationSummary* locations = instruction_->GetLocations();
724 Register reg_out = out_.AsRegister<Register>();
725 DCHECK(locations->CanCall());
726 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain3d312422016-06-23 13:53:42 +0100727 DCHECK(instruction_->IsInstanceFieldGet() ||
728 instruction_->IsStaticFieldGet() ||
729 instruction_->IsArrayGet() ||
730 instruction_->IsInstanceOf() ||
731 instruction_->IsCheckCast() ||
Andreas Gamped9911ee2017-03-27 13:27:24 -0700732 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain7c1559a2015-12-15 10:55:36 +0000733 << "Unexpected instruction in read barrier for heap reference slow path: "
734 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000735
736 __ Bind(GetEntryLabel());
737 SaveLiveRegisters(codegen, locations);
738
739 // We may have to change the index's value, but as `index_` is a
740 // constant member (like other "inputs" of this slow path),
741 // introduce a copy of it, `index`.
742 Location index = index_;
743 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100744 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain0d5a2812015-11-13 10:07:31 +0000745 if (instruction_->IsArrayGet()) {
746 // Compute the actual memory offset and store it in `index`.
747 Register index_reg = index_.AsRegister<Register>();
748 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
749 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
750 // We are about to change the value of `index_reg` (see the
751 // calls to art::x86::X86Assembler::shll and
752 // art::x86::X86Assembler::AddImmediate below), but it has
753 // not been saved by the previous call to
754 // art::SlowPathCode::SaveLiveRegisters, as it is a
755 // callee-save register --
756 // art::SlowPathCode::SaveLiveRegisters does not consider
757 // callee-save registers, as it has been designed with the
758 // assumption that callee-save registers are supposed to be
759 // handled by the called function. So, as a callee-save
760 // register, `index_reg` _would_ eventually be saved onto
761 // the stack, but it would be too late: we would have
762 // changed its value earlier. Therefore, we manually save
763 // it here into another freely available register,
764 // `free_reg`, chosen of course among the caller-save
765 // registers (as a callee-save `free_reg` register would
766 // exhibit the same problem).
767 //
768 // Note we could have requested a temporary register from
769 // the register allocator instead; but we prefer not to, as
770 // this is a slow path, and we know we can find a
771 // caller-save register that is available.
772 Register free_reg = FindAvailableCallerSaveRegister(codegen);
773 __ movl(free_reg, index_reg);
774 index_reg = free_reg;
775 index = Location::RegisterLocation(index_reg);
776 } else {
777 // The initial register stored in `index_` has already been
778 // saved in the call to art::SlowPathCode::SaveLiveRegisters
779 // (as it is not a callee-save register), so we can freely
780 // use it.
781 }
782 // Shifting the index value contained in `index_reg` by the scale
783 // factor (2) cannot overflow in practice, as the runtime is
784 // unable to allocate object arrays with a size larger than
785 // 2^26 - 1 (that is, 2^28 - 4 bytes).
786 __ shll(index_reg, Immediate(TIMES_4));
787 static_assert(
788 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
789 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
790 __ AddImmediate(index_reg, Immediate(offset_));
791 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100792 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
793 // intrinsics, `index_` is not shifted by a scale factor of 2
794 // (as in the case of ArrayGet), as it is actually an offset
795 // to an object field within an object.
796 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000797 DCHECK(instruction_->GetLocations()->Intrinsified());
798 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
799 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
800 << instruction_->AsInvoke()->GetIntrinsic();
801 DCHECK_EQ(offset_, 0U);
802 DCHECK(index_.IsRegisterPair());
803 // UnsafeGet's offset location is a register pair, the low
804 // part contains the correct offset.
805 index = index_.ToLow();
806 }
807 }
808
809 // We're moving two or three locations to locations that could
810 // overlap, so we need a parallel move resolver.
811 InvokeRuntimeCallingConvention calling_convention;
812 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
813 parallel_move.AddMove(ref_,
814 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
815 Primitive::kPrimNot,
816 nullptr);
817 parallel_move.AddMove(obj_,
818 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
819 Primitive::kPrimNot,
820 nullptr);
821 if (index.IsValid()) {
822 parallel_move.AddMove(index,
823 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
824 Primitive::kPrimInt,
825 nullptr);
826 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
827 } else {
828 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
829 __ movl(calling_convention.GetRegisterAt(2), Immediate(offset_));
830 }
Serban Constantinescuba45db02016-07-12 22:53:02 +0100831 x86_codegen->InvokeRuntime(kQuickReadBarrierSlow, instruction_, instruction_->GetDexPc(), this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000832 CheckEntrypointTypes<
833 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
834 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
835
836 RestoreLiveRegisters(codegen, locations);
837 __ jmp(GetExitLabel());
838 }
839
840 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathX86"; }
841
842 private:
843 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
844 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
845 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
846 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
847 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
848 return static_cast<Register>(i);
849 }
850 }
851 // We shall never fail to find a free caller-save register, as
852 // there are more than two core caller-save registers on x86
853 // (meaning it is possible to find one which is different from
854 // `ref` and `obj`).
855 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
856 LOG(FATAL) << "Could not find a free caller-save register";
857 UNREACHABLE();
858 }
859
Roland Levillain0d5a2812015-11-13 10:07:31 +0000860 const Location out_;
861 const Location ref_;
862 const Location obj_;
863 const uint32_t offset_;
864 // An additional location containing an index to an array.
865 // Only used for HArrayGet and the UnsafeGetObject &
866 // UnsafeGetObjectVolatile intrinsics.
867 const Location index_;
868
869 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86);
870};
871
872// Slow path generating a read barrier for a GC root.
873class ReadBarrierForRootSlowPathX86 : public SlowPathCode {
874 public:
875 ReadBarrierForRootSlowPathX86(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000876 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000877 DCHECK(kEmitCompilerReadBarrier);
878 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000879
880 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
881 LocationSummary* locations = instruction_->GetLocations();
882 Register reg_out = out_.AsRegister<Register>();
883 DCHECK(locations->CanCall());
884 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000885 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
886 << "Unexpected instruction in read barrier for GC root slow path: "
887 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000888
889 __ Bind(GetEntryLabel());
890 SaveLiveRegisters(codegen, locations);
891
892 InvokeRuntimeCallingConvention calling_convention;
893 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
894 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100895 x86_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000896 instruction_,
897 instruction_->GetDexPc(),
898 this);
899 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
900 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
901
902 RestoreLiveRegisters(codegen, locations);
903 __ jmp(GetExitLabel());
904 }
905
906 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathX86"; }
907
908 private:
Roland Levillain0d5a2812015-11-13 10:07:31 +0000909 const Location out_;
910 const Location root_;
911
912 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86);
913};
914
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100915#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100916// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
917#define __ down_cast<X86Assembler*>(GetAssembler())-> // NOLINT
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100918
Aart Bike9f37602015-10-09 11:15:55 -0700919inline Condition X86Condition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700920 switch (cond) {
921 case kCondEQ: return kEqual;
922 case kCondNE: return kNotEqual;
923 case kCondLT: return kLess;
924 case kCondLE: return kLessEqual;
925 case kCondGT: return kGreater;
926 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700927 case kCondB: return kBelow;
928 case kCondBE: return kBelowEqual;
929 case kCondA: return kAbove;
930 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700931 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100932 LOG(FATAL) << "Unreachable";
933 UNREACHABLE();
934}
935
Aart Bike9f37602015-10-09 11:15:55 -0700936// Maps signed condition to unsigned condition and FP condition to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100937inline Condition X86UnsignedOrFPCondition(IfCondition cond) {
938 switch (cond) {
939 case kCondEQ: return kEqual;
940 case kCondNE: return kNotEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700941 // Signed to unsigned, and FP to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100942 case kCondLT: return kBelow;
943 case kCondLE: return kBelowEqual;
944 case kCondGT: return kAbove;
945 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700946 // Unsigned remain unchanged.
947 case kCondB: return kBelow;
948 case kCondBE: return kBelowEqual;
949 case kCondA: return kAbove;
950 case kCondAE: return kAboveEqual;
Roland Levillain4fa13f62015-07-06 18:11:54 +0100951 }
952 LOG(FATAL) << "Unreachable";
953 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700954}
955
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100956void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100957 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100958}
959
960void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100961 stream << XmmRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100962}
963
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100964size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
965 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
966 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100967}
968
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100969size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
970 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
971 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100972}
973
Mark Mendell7c8d0092015-01-26 11:21:33 -0500974size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Aart Bikb13c65b2017-03-21 20:14:07 -0700975 if (GetGraph()->HasSIMD()) {
Aart Bik5576f372017-03-23 16:17:37 -0700976 __ movups(Address(ESP, stack_index), XmmRegister(reg_id));
Aart Bikb13c65b2017-03-21 20:14:07 -0700977 } else {
978 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
979 }
Mark Mendell7c8d0092015-01-26 11:21:33 -0500980 return GetFloatingPointSpillSlotSize();
981}
982
983size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Aart Bikb13c65b2017-03-21 20:14:07 -0700984 if (GetGraph()->HasSIMD()) {
Aart Bik5576f372017-03-23 16:17:37 -0700985 __ movups(XmmRegister(reg_id), Address(ESP, stack_index));
Aart Bikb13c65b2017-03-21 20:14:07 -0700986 } else {
987 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
988 }
Mark Mendell7c8d0092015-01-26 11:21:33 -0500989 return GetFloatingPointSpillSlotSize();
990}
991
Calin Juravle175dc732015-08-25 15:42:32 +0100992void CodeGeneratorX86::InvokeRuntime(QuickEntrypointEnum entrypoint,
993 HInstruction* instruction,
994 uint32_t dex_pc,
995 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +0100996 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100997 GenerateInvokeRuntime(GetThreadOffset<kX86PointerSize>(entrypoint).Int32Value());
998 if (EntrypointRequiresStackMap(entrypoint)) {
999 RecordPcInfo(instruction, dex_pc, slow_path);
1000 }
Alexandre Rames8158f282015-08-07 10:26:17 +01001001}
1002
Roland Levillaindec8f632016-07-22 17:10:06 +01001003void CodeGeneratorX86::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1004 HInstruction* instruction,
1005 SlowPathCode* slow_path) {
1006 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +01001007 GenerateInvokeRuntime(entry_point_offset);
1008}
1009
1010void CodeGeneratorX86::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001011 __ fs()->call(Address::Absolute(entry_point_offset));
1012}
1013
Mark Mendellfb8d2792015-03-31 22:16:59 -04001014CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +00001015 const X86InstructionSetFeatures& isa_features,
1016 const CompilerOptions& compiler_options,
1017 OptimizingCompilerStats* stats)
Mark Mendell5f874182015-03-04 15:42:45 -05001018 : CodeGenerator(graph,
1019 kNumberOfCpuRegisters,
1020 kNumberOfXmmRegisters,
1021 kNumberOfRegisterPairs,
1022 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1023 arraysize(kCoreCalleeSaves))
1024 | (1 << kFakeReturnRegister),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001025 0,
1026 compiler_options,
1027 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +01001028 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001029 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001030 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -04001031 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001032 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +00001033 isa_features_(isa_features),
Vladimir Marko65979462017-05-19 17:25:12 +01001034 boot_image_method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001035 method_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +00001036 boot_image_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1037 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko65979462017-05-19 17:25:12 +01001038 string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001039 string_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001040 jit_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001041 jit_class_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko93205e32016-04-13 11:59:46 +01001042 constant_area_start_(-1),
1043 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001044 method_address_offset_(std::less<uint32_t>(),
1045 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001046 // Use a fake return address register to mimic Quick.
1047 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001048}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001049
David Brazdil58282f42016-01-14 12:45:10 +00001050void CodeGeneratorX86::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001051 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001052 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001053}
1054
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001055InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001056 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001057 assembler_(codegen->GetAssembler()),
1058 codegen_(codegen) {}
1059
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001060static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001061 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001062}
1063
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001064void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001065 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001066 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +00001067 bool skip_overflow_check =
1068 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001069 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +00001070
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001071 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +01001072 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001073 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +01001074 }
1075
Mark Mendell5f874182015-03-04 15:42:45 -05001076 if (HasEmptyFrame()) {
1077 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001078 }
Mark Mendell5f874182015-03-04 15:42:45 -05001079
1080 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
1081 Register reg = kCoreCalleeSaves[i];
1082 if (allocated_registers_.ContainsCoreRegister(reg)) {
1083 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001084 __ cfi().AdjustCFAOffset(kX86WordSize);
1085 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -05001086 }
1087 }
1088
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001089 int adjust = GetFrameSize() - FrameEntrySpillSize();
1090 __ subl(ESP, Immediate(adjust));
1091 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001092 // Save the current method if we need it. Note that we do not
1093 // do this in HCurrentMethod, as the instruction might have been removed
1094 // in the SSA graph.
1095 if (RequiresCurrentMethod()) {
1096 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
1097 }
Nicolas Geoffrayf7893532017-06-15 12:34:36 +01001098
1099 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1100 // Initialize should_deoptimize flag to 0.
1101 __ movl(Address(ESP, GetStackOffsetOfShouldDeoptimizeFlag()), Immediate(0));
1102 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001103}
1104
1105void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001106 __ cfi().RememberState();
1107 if (!HasEmptyFrame()) {
1108 int adjust = GetFrameSize() - FrameEntrySpillSize();
1109 __ addl(ESP, Immediate(adjust));
1110 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -05001111
David Srbeckyc34dc932015-04-12 09:27:43 +01001112 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1113 Register reg = kCoreCalleeSaves[i];
1114 if (allocated_registers_.ContainsCoreRegister(reg)) {
1115 __ popl(reg);
1116 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
1117 __ cfi().Restore(DWARFReg(reg));
1118 }
Mark Mendell5f874182015-03-04 15:42:45 -05001119 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001120 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001121 __ ret();
1122 __ cfi().RestoreState();
1123 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001124}
1125
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001126void CodeGeneratorX86::Bind(HBasicBlock* block) {
1127 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001128}
1129
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001130Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(Primitive::Type type) const {
1131 switch (type) {
1132 case Primitive::kPrimBoolean:
1133 case Primitive::kPrimByte:
1134 case Primitive::kPrimChar:
1135 case Primitive::kPrimShort:
1136 case Primitive::kPrimInt:
1137 case Primitive::kPrimNot:
1138 return Location::RegisterLocation(EAX);
1139
1140 case Primitive::kPrimLong:
1141 return Location::RegisterPairLocation(EAX, EDX);
1142
1143 case Primitive::kPrimVoid:
1144 return Location::NoLocation();
1145
1146 case Primitive::kPrimDouble:
1147 case Primitive::kPrimFloat:
1148 return Location::FpuRegisterLocation(XMM0);
1149 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001150
1151 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001152}
1153
1154Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
1155 return Location::RegisterLocation(kMethodRegisterArgument);
1156}
1157
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001158Location InvokeDexCallingConventionVisitorX86::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001159 switch (type) {
1160 case Primitive::kPrimBoolean:
1161 case Primitive::kPrimByte:
1162 case Primitive::kPrimChar:
1163 case Primitive::kPrimShort:
1164 case Primitive::kPrimInt:
1165 case Primitive::kPrimNot: {
1166 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001167 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001168 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001169 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001170 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001171 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001172 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001173 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001174
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001175 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001176 uint32_t index = gp_index_;
1177 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001178 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001179 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001180 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
1181 calling_convention.GetRegisterPairAt(index));
1182 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001183 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001184 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1185 }
1186 }
1187
1188 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001189 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001190 stack_index_++;
1191 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1192 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1193 } else {
1194 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1195 }
1196 }
1197
1198 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001199 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001200 stack_index_ += 2;
1201 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1202 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1203 } else {
1204 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001205 }
1206 }
1207
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001208 case Primitive::kPrimVoid:
1209 LOG(FATAL) << "Unexpected parameter type " << type;
1210 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001211 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00001212 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001213}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001214
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001215void CodeGeneratorX86::Move32(Location destination, Location source) {
1216 if (source.Equals(destination)) {
1217 return;
1218 }
1219 if (destination.IsRegister()) {
1220 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001221 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001222 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001223 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001224 } else {
1225 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001226 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001227 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001228 } else if (destination.IsFpuRegister()) {
1229 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001230 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001231 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001232 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001233 } else {
1234 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001235 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001236 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001237 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001238 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001239 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001240 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001241 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001242 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05001243 } else if (source.IsConstant()) {
1244 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001245 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05001246 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001247 } else {
1248 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001249 __ pushl(Address(ESP, source.GetStackIndex()));
1250 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001251 }
1252 }
1253}
1254
1255void CodeGeneratorX86::Move64(Location destination, Location source) {
1256 if (source.Equals(destination)) {
1257 return;
1258 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001259 if (destination.IsRegisterPair()) {
1260 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001261 EmitParallelMoves(
1262 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1263 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001264 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001265 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001266 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1267 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001268 } else if (source.IsFpuRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001269 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
1270 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
1271 __ psrlq(src_reg, Immediate(32));
1272 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001273 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001274 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001275 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001276 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
1277 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001278 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
1279 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001280 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05001281 if (source.IsFpuRegister()) {
1282 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
1283 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001284 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001285 } else if (source.IsRegisterPair()) {
1286 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
1287 // Create stack space for 2 elements.
1288 __ subl(ESP, Immediate(2 * elem_size));
1289 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
1290 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
1291 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
1292 // And remove the temporary stack space we allocated.
1293 __ addl(ESP, Immediate(2 * elem_size));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001294 } else {
1295 LOG(FATAL) << "Unimplemented";
1296 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001297 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001298 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001299 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001300 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001301 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001302 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001303 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001304 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001305 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001306 } else if (source.IsConstant()) {
1307 HConstant* constant = source.GetConstant();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001308 DCHECK(constant->IsLongConstant() || constant->IsDoubleConstant());
1309 int64_t value = GetInt64ValueOf(constant);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001310 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001311 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
1312 Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001313 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001314 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001315 EmitParallelMoves(
1316 Location::StackSlot(source.GetStackIndex()),
1317 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001318 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001319 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001320 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
1321 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001322 }
1323 }
1324}
1325
Calin Juravle175dc732015-08-25 15:42:32 +01001326void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
1327 DCHECK(location.IsRegister());
1328 __ movl(location.AsRegister<Register>(), Immediate(value));
1329}
1330
Calin Juravlee460d1d2015-09-29 04:52:17 +01001331void CodeGeneratorX86::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001332 HParallelMove move(GetGraph()->GetArena());
1333 if (dst_type == Primitive::kPrimLong && !src.IsConstant() && !src.IsFpuRegister()) {
1334 move.AddMove(src.ToLow(), dst.ToLow(), Primitive::kPrimInt, nullptr);
1335 move.AddMove(src.ToHigh(), dst.ToHigh(), Primitive::kPrimInt, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001336 } else {
David Brazdil74eb1b22015-12-14 11:44:01 +00001337 move.AddMove(src, dst, dst_type, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001338 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001339 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001340}
1341
1342void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1343 if (location.IsRegister()) {
1344 locations->AddTemp(location);
1345 } else if (location.IsRegisterPair()) {
1346 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1347 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1348 } else {
1349 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1350 }
1351}
1352
David Brazdilfc6a86a2015-06-26 10:33:45 +00001353void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001354 DCHECK(!successor->IsExitBlock());
1355
1356 HBasicBlock* block = got->GetBlock();
1357 HInstruction* previous = got->GetPrevious();
1358
1359 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001360 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001361 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1362 return;
1363 }
1364
1365 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1366 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1367 }
1368 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001369 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001370 }
1371}
1372
David Brazdilfc6a86a2015-06-26 10:33:45 +00001373void LocationsBuilderX86::VisitGoto(HGoto* got) {
1374 got->SetLocations(nullptr);
1375}
1376
1377void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1378 HandleGoto(got, got->GetSuccessor());
1379}
1380
1381void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1382 try_boundary->SetLocations(nullptr);
1383}
1384
1385void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1386 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1387 if (!successor->IsExitBlock()) {
1388 HandleGoto(try_boundary, successor);
1389 }
1390}
1391
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001392void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001393 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001394}
1395
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001396void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001397}
1398
Mark Mendell152408f2015-12-31 12:28:50 -05001399template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001400void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001401 LabelType* true_label,
1402 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001403 if (cond->IsFPConditionTrueIfNaN()) {
1404 __ j(kUnordered, true_label);
1405 } else if (cond->IsFPConditionFalseIfNaN()) {
1406 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001407 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001408 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001409}
1410
Mark Mendell152408f2015-12-31 12:28:50 -05001411template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001412void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001413 LabelType* true_label,
1414 LabelType* false_label) {
Mark Mendellc4701932015-04-10 13:18:51 -04001415 LocationSummary* locations = cond->GetLocations();
1416 Location left = locations->InAt(0);
1417 Location right = locations->InAt(1);
1418 IfCondition if_cond = cond->GetCondition();
1419
Mark Mendellc4701932015-04-10 13:18:51 -04001420 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001421 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001422 IfCondition true_high_cond = if_cond;
1423 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001424 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001425
1426 // Set the conditions for the test, remembering that == needs to be
1427 // decided using the low words.
1428 switch (if_cond) {
1429 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001430 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001431 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001432 break;
1433 case kCondLT:
1434 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001435 break;
1436 case kCondLE:
1437 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001438 break;
1439 case kCondGT:
1440 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001441 break;
1442 case kCondGE:
1443 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001444 break;
Aart Bike9f37602015-10-09 11:15:55 -07001445 case kCondB:
1446 false_high_cond = kCondA;
1447 break;
1448 case kCondBE:
1449 true_high_cond = kCondB;
1450 break;
1451 case kCondA:
1452 false_high_cond = kCondB;
1453 break;
1454 case kCondAE:
1455 true_high_cond = kCondA;
1456 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001457 }
1458
1459 if (right.IsConstant()) {
1460 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001461 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001462 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001463
Aart Bika19616e2016-02-01 18:57:58 -08001464 codegen_->Compare32BitValue(left_high, val_high);
Mark Mendellc4701932015-04-10 13:18:51 -04001465 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001466 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001467 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001468 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001469 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001470 __ j(X86Condition(true_high_cond), true_label);
1471 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001472 }
1473 // Must be equal high, so compare the lows.
Aart Bika19616e2016-02-01 18:57:58 -08001474 codegen_->Compare32BitValue(left_low, val_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001475 } else if (right.IsRegisterPair()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001476 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001477 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001478
1479 __ cmpl(left_high, right_high);
1480 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001481 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001482 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001483 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001484 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001485 __ j(X86Condition(true_high_cond), true_label);
1486 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001487 }
1488 // Must be equal high, so compare the lows.
1489 __ cmpl(left_low, right_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001490 } else {
1491 DCHECK(right.IsDoubleStackSlot());
1492 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
1493 if (if_cond == kCondNE) {
1494 __ j(X86Condition(true_high_cond), true_label);
1495 } else if (if_cond == kCondEQ) {
1496 __ j(X86Condition(false_high_cond), false_label);
1497 } else {
1498 __ j(X86Condition(true_high_cond), true_label);
1499 __ j(X86Condition(false_high_cond), false_label);
1500 }
1501 // Must be equal high, so compare the lows.
1502 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Mark Mendellc4701932015-04-10 13:18:51 -04001503 }
1504 // The last comparison might be unsigned.
1505 __ j(final_condition, true_label);
1506}
1507
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001508void InstructionCodeGeneratorX86::GenerateFPCompare(Location lhs,
1509 Location rhs,
1510 HInstruction* insn,
1511 bool is_double) {
1512 HX86LoadFromConstantTable* const_area = insn->InputAt(1)->AsX86LoadFromConstantTable();
1513 if (is_double) {
1514 if (rhs.IsFpuRegister()) {
1515 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1516 } else if (const_area != nullptr) {
1517 DCHECK(const_area->IsEmittedAtUseSite());
1518 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(),
1519 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001520 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
1521 const_area->GetBaseMethodAddress(),
1522 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001523 } else {
1524 DCHECK(rhs.IsDoubleStackSlot());
1525 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1526 }
1527 } else {
1528 if (rhs.IsFpuRegister()) {
1529 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1530 } else if (const_area != nullptr) {
1531 DCHECK(const_area->IsEmittedAtUseSite());
1532 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(),
1533 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001534 const_area->GetConstant()->AsFloatConstant()->GetValue(),
1535 const_area->GetBaseMethodAddress(),
1536 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001537 } else {
1538 DCHECK(rhs.IsStackSlot());
1539 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1540 }
1541 }
1542}
1543
Mark Mendell152408f2015-12-31 12:28:50 -05001544template<class LabelType>
David Brazdil0debae72015-11-12 18:37:00 +00001545void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendell152408f2015-12-31 12:28:50 -05001546 LabelType* true_target_in,
1547 LabelType* false_target_in) {
David Brazdil0debae72015-11-12 18:37:00 +00001548 // Generated branching requires both targets to be explicit. If either of the
1549 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Mark Mendell152408f2015-12-31 12:28:50 -05001550 LabelType fallthrough_target;
1551 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1552 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
David Brazdil0debae72015-11-12 18:37:00 +00001553
Mark Mendellc4701932015-04-10 13:18:51 -04001554 LocationSummary* locations = condition->GetLocations();
1555 Location left = locations->InAt(0);
1556 Location right = locations->InAt(1);
1557
Mark Mendellc4701932015-04-10 13:18:51 -04001558 Primitive::Type type = condition->InputAt(0)->GetType();
1559 switch (type) {
1560 case Primitive::kPrimLong:
1561 GenerateLongComparesAndJumps(condition, true_target, false_target);
1562 break;
1563 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001564 GenerateFPCompare(left, right, condition, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001565 GenerateFPJumps(condition, true_target, false_target);
1566 break;
1567 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001568 GenerateFPCompare(left, right, condition, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001569 GenerateFPJumps(condition, true_target, false_target);
1570 break;
1571 default:
1572 LOG(FATAL) << "Unexpected compare type " << type;
1573 }
1574
David Brazdil0debae72015-11-12 18:37:00 +00001575 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001576 __ jmp(false_target);
1577 }
David Brazdil0debae72015-11-12 18:37:00 +00001578
1579 if (fallthrough_target.IsLinked()) {
1580 __ Bind(&fallthrough_target);
1581 }
Mark Mendellc4701932015-04-10 13:18:51 -04001582}
1583
David Brazdil0debae72015-11-12 18:37:00 +00001584static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1585 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1586 // are set only strictly before `branch`. We can't use the eflags on long/FP
1587 // conditions if they are materialized due to the complex branching.
1588 return cond->IsCondition() &&
1589 cond->GetNext() == branch &&
1590 cond->InputAt(0)->GetType() != Primitive::kPrimLong &&
1591 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1592}
1593
Mark Mendell152408f2015-12-31 12:28:50 -05001594template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001595void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001596 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001597 LabelType* true_target,
1598 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001599 HInstruction* cond = instruction->InputAt(condition_input_index);
1600
1601 if (true_target == nullptr && false_target == nullptr) {
1602 // Nothing to do. The code always falls through.
1603 return;
1604 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001605 // Constant condition, statically compared against "true" (integer value 1).
1606 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001607 if (true_target != nullptr) {
1608 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001609 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001610 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001611 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001612 if (false_target != nullptr) {
1613 __ jmp(false_target);
1614 }
1615 }
1616 return;
1617 }
1618
1619 // The following code generates these patterns:
1620 // (1) true_target == nullptr && false_target != nullptr
1621 // - opposite condition true => branch to false_target
1622 // (2) true_target != nullptr && false_target == nullptr
1623 // - condition true => branch to true_target
1624 // (3) true_target != nullptr && false_target != nullptr
1625 // - condition true => branch to true_target
1626 // - branch to false_target
1627 if (IsBooleanValueOrMaterializedCondition(cond)) {
1628 if (AreEflagsSetFrom(cond, instruction)) {
1629 if (true_target == nullptr) {
1630 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
1631 } else {
1632 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
1633 }
1634 } else {
1635 // Materialized condition, compare against 0.
1636 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1637 if (lhs.IsRegister()) {
1638 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1639 } else {
1640 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1641 }
1642 if (true_target == nullptr) {
1643 __ j(kEqual, false_target);
1644 } else {
1645 __ j(kNotEqual, true_target);
1646 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001647 }
1648 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001649 // Condition has not been materialized, use its inputs as the comparison and
1650 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001651 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001652
1653 // If this is a long or FP comparison that has been folded into
1654 // the HCondition, generate the comparison directly.
1655 Primitive::Type type = condition->InputAt(0)->GetType();
1656 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1657 GenerateCompareTestAndBranch(condition, true_target, false_target);
1658 return;
1659 }
1660
1661 Location lhs = condition->GetLocations()->InAt(0);
1662 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001663 // LHS is guaranteed to be in a register (see LocationsBuilderX86::HandleCondition).
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001664 codegen_->GenerateIntCompare(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00001665 if (true_target == nullptr) {
1666 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1667 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001668 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001669 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001670 }
David Brazdil0debae72015-11-12 18:37:00 +00001671
1672 // If neither branch falls through (case 3), the conditional branch to `true_target`
1673 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1674 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001675 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001676 }
1677}
1678
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001679void LocationsBuilderX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001680 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1681 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001682 locations->SetInAt(0, Location::Any());
1683 }
1684}
1685
1686void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001687 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1688 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1689 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1690 nullptr : codegen_->GetLabelOf(true_successor);
1691 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1692 nullptr : codegen_->GetLabelOf(false_successor);
1693 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001694}
1695
1696void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1697 LocationSummary* locations = new (GetGraph()->GetArena())
1698 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01001699 InvokeRuntimeCallingConvention calling_convention;
1700 RegisterSet caller_saves = RegisterSet::Empty();
1701 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1702 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00001703 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001704 locations->SetInAt(0, Location::Any());
1705 }
1706}
1707
1708void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001709 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001710 GenerateTestAndBranch<Label>(deoptimize,
1711 /* condition_input_index */ 0,
1712 slow_path->GetEntryLabel(),
1713 /* false_target */ nullptr);
1714}
1715
Mingyao Yang063fc772016-08-02 11:02:54 -07001716void LocationsBuilderX86::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
1717 LocationSummary* locations = new (GetGraph()->GetArena())
1718 LocationSummary(flag, LocationSummary::kNoCall);
1719 locations->SetOut(Location::RequiresRegister());
1720}
1721
1722void InstructionCodeGeneratorX86::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
1723 __ movl(flag->GetLocations()->Out().AsRegister<Register>(),
1724 Address(ESP, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
1725}
1726
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001727static bool SelectCanUseCMOV(HSelect* select) {
1728 // There are no conditional move instructions for XMMs.
1729 if (Primitive::IsFloatingPointType(select->GetType())) {
1730 return false;
1731 }
1732
1733 // A FP condition doesn't generate the single CC that we need.
1734 // In 32 bit mode, a long condition doesn't generate a single CC either.
1735 HInstruction* condition = select->GetCondition();
1736 if (condition->IsCondition()) {
1737 Primitive::Type compare_type = condition->InputAt(0)->GetType();
1738 if (compare_type == Primitive::kPrimLong ||
1739 Primitive::IsFloatingPointType(compare_type)) {
1740 return false;
1741 }
1742 }
1743
1744 // We can generate a CMOV for this Select.
1745 return true;
1746}
1747
David Brazdil74eb1b22015-12-14 11:44:01 +00001748void LocationsBuilderX86::VisitSelect(HSelect* select) {
1749 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001750 if (Primitive::IsFloatingPointType(select->GetType())) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001751 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001752 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001753 } else {
1754 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001755 if (SelectCanUseCMOV(select)) {
1756 if (select->InputAt(1)->IsConstant()) {
1757 // Cmov can't handle a constant value.
1758 locations->SetInAt(1, Location::RequiresRegister());
1759 } else {
1760 locations->SetInAt(1, Location::Any());
1761 }
1762 } else {
1763 locations->SetInAt(1, Location::Any());
1764 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001765 }
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001766 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1767 locations->SetInAt(2, Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00001768 }
1769 locations->SetOut(Location::SameAsFirstInput());
1770}
1771
1772void InstructionCodeGeneratorX86::VisitSelect(HSelect* select) {
1773 LocationSummary* locations = select->GetLocations();
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001774 DCHECK(locations->InAt(0).Equals(locations->Out()));
1775 if (SelectCanUseCMOV(select)) {
1776 // If both the condition and the source types are integer, we can generate
1777 // a CMOV to implement Select.
1778
1779 HInstruction* select_condition = select->GetCondition();
1780 Condition cond = kNotEqual;
1781
1782 // Figure out how to test the 'condition'.
1783 if (select_condition->IsCondition()) {
1784 HCondition* condition = select_condition->AsCondition();
1785 if (!condition->IsEmittedAtUseSite()) {
1786 // This was a previously materialized condition.
1787 // Can we use the existing condition code?
1788 if (AreEflagsSetFrom(condition, select)) {
1789 // Materialization was the previous instruction. Condition codes are right.
1790 cond = X86Condition(condition->GetCondition());
1791 } else {
1792 // No, we have to recreate the condition code.
1793 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1794 __ testl(cond_reg, cond_reg);
1795 }
1796 } else {
1797 // We can't handle FP or long here.
1798 DCHECK_NE(condition->InputAt(0)->GetType(), Primitive::kPrimLong);
1799 DCHECK(!Primitive::IsFloatingPointType(condition->InputAt(0)->GetType()));
1800 LocationSummary* cond_locations = condition->GetLocations();
Roland Levillain0b671c02016-08-19 12:02:34 +01001801 codegen_->GenerateIntCompare(cond_locations->InAt(0), cond_locations->InAt(1));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001802 cond = X86Condition(condition->GetCondition());
1803 }
1804 } else {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01001805 // Must be a Boolean condition, which needs to be compared to 0.
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001806 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1807 __ testl(cond_reg, cond_reg);
1808 }
1809
1810 // If the condition is true, overwrite the output, which already contains false.
1811 Location false_loc = locations->InAt(0);
1812 Location true_loc = locations->InAt(1);
1813 if (select->GetType() == Primitive::kPrimLong) {
1814 // 64 bit conditional move.
1815 Register false_high = false_loc.AsRegisterPairHigh<Register>();
1816 Register false_low = false_loc.AsRegisterPairLow<Register>();
1817 if (true_loc.IsRegisterPair()) {
1818 __ cmovl(cond, false_high, true_loc.AsRegisterPairHigh<Register>());
1819 __ cmovl(cond, false_low, true_loc.AsRegisterPairLow<Register>());
1820 } else {
1821 __ cmovl(cond, false_high, Address(ESP, true_loc.GetHighStackIndex(kX86WordSize)));
1822 __ cmovl(cond, false_low, Address(ESP, true_loc.GetStackIndex()));
1823 }
1824 } else {
1825 // 32 bit conditional move.
1826 Register false_reg = false_loc.AsRegister<Register>();
1827 if (true_loc.IsRegister()) {
1828 __ cmovl(cond, false_reg, true_loc.AsRegister<Register>());
1829 } else {
1830 __ cmovl(cond, false_reg, Address(ESP, true_loc.GetStackIndex()));
1831 }
1832 }
1833 } else {
1834 NearLabel false_target;
1835 GenerateTestAndBranch<NearLabel>(
1836 select, /* condition_input_index */ 2, /* true_target */ nullptr, &false_target);
1837 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1838 __ Bind(&false_target);
1839 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001840}
1841
David Srbecky0cf44932015-12-09 14:09:59 +00001842void LocationsBuilderX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1843 new (GetGraph()->GetArena()) LocationSummary(info);
1844}
1845
David Srbeckyd28f4a02016-03-14 17:14:24 +00001846void InstructionCodeGeneratorX86::VisitNativeDebugInfo(HNativeDebugInfo*) {
1847 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001848}
1849
1850void CodeGeneratorX86::GenerateNop() {
1851 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001852}
1853
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001854void LocationsBuilderX86::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001855 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001856 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001857 // Handle the long/FP comparisons made in instruction simplification.
1858 switch (cond->InputAt(0)->GetType()) {
1859 case Primitive::kPrimLong: {
1860 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell8659e842016-02-16 10:41:46 -05001861 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001862 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001863 locations->SetOut(Location::RequiresRegister());
1864 }
1865 break;
1866 }
1867 case Primitive::kPrimFloat:
1868 case Primitive::kPrimDouble: {
1869 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001870 if (cond->InputAt(1)->IsX86LoadFromConstantTable()) {
1871 DCHECK(cond->InputAt(1)->IsEmittedAtUseSite());
1872 } else if (cond->InputAt(1)->IsConstant()) {
1873 locations->SetInAt(1, Location::RequiresFpuRegister());
1874 } else {
1875 locations->SetInAt(1, Location::Any());
1876 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001877 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001878 locations->SetOut(Location::RequiresRegister());
1879 }
1880 break;
1881 }
1882 default:
1883 locations->SetInAt(0, Location::RequiresRegister());
1884 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001885 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001886 // We need a byte register.
1887 locations->SetOut(Location::RegisterLocation(ECX));
1888 }
1889 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001890 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001891}
1892
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001893void InstructionCodeGeneratorX86::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001894 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001895 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001896 }
Mark Mendellc4701932015-04-10 13:18:51 -04001897
1898 LocationSummary* locations = cond->GetLocations();
1899 Location lhs = locations->InAt(0);
1900 Location rhs = locations->InAt(1);
1901 Register reg = locations->Out().AsRegister<Register>();
Mark Mendell152408f2015-12-31 12:28:50 -05001902 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001903
1904 switch (cond->InputAt(0)->GetType()) {
1905 default: {
1906 // Integer case.
1907
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01001908 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04001909 __ xorl(reg, reg);
Roland Levillain0b671c02016-08-19 12:02:34 +01001910 codegen_->GenerateIntCompare(lhs, rhs);
Aart Bike9f37602015-10-09 11:15:55 -07001911 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001912 return;
1913 }
1914 case Primitive::kPrimLong:
1915 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1916 break;
1917 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001918 GenerateFPCompare(lhs, rhs, cond, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001919 GenerateFPJumps(cond, &true_label, &false_label);
1920 break;
1921 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001922 GenerateFPCompare(lhs, rhs, cond, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001923 GenerateFPJumps(cond, &true_label, &false_label);
1924 break;
1925 }
1926
1927 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001928 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001929
Roland Levillain4fa13f62015-07-06 18:11:54 +01001930 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001931 __ Bind(&false_label);
1932 __ xorl(reg, reg);
1933 __ jmp(&done_label);
1934
Roland Levillain4fa13f62015-07-06 18:11:54 +01001935 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001936 __ Bind(&true_label);
1937 __ movl(reg, Immediate(1));
1938 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001939}
1940
1941void LocationsBuilderX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001942 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001943}
1944
1945void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001946 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001947}
1948
1949void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001950 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001951}
1952
1953void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001954 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001955}
1956
1957void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001958 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001959}
1960
1961void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001962 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001963}
1964
1965void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001966 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001967}
1968
1969void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001970 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001971}
1972
1973void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001974 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001975}
1976
1977void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001978 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001979}
1980
1981void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001982 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001983}
1984
1985void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001986 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001987}
1988
Aart Bike9f37602015-10-09 11:15:55 -07001989void LocationsBuilderX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001990 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001991}
1992
1993void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001994 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001995}
1996
1997void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001998 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001999}
2000
2001void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002002 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002003}
2004
2005void LocationsBuilderX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002006 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002007}
2008
2009void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002010 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002011}
2012
2013void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002014 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002015}
2016
2017void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002018 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002019}
2020
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002021void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002022 LocationSummary* locations =
2023 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002024 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002025}
2026
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002027void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002028 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002029}
2030
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002031void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
2032 LocationSummary* locations =
2033 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2034 locations->SetOut(Location::ConstantLocation(constant));
2035}
2036
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002037void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002038 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002039}
2040
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002041void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002042 LocationSummary* locations =
2043 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002044 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002045}
2046
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002047void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002048 // Will be generated at use site.
2049}
2050
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002051void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
2052 LocationSummary* locations =
2053 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2054 locations->SetOut(Location::ConstantLocation(constant));
2055}
2056
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002057void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002058 // Will be generated at use site.
2059}
2060
2061void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
2062 LocationSummary* locations =
2063 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2064 locations->SetOut(Location::ConstantLocation(constant));
2065}
2066
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002067void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002068 // Will be generated at use site.
2069}
2070
Igor Murashkind01745e2017-04-05 16:40:31 -07002071void LocationsBuilderX86::VisitConstructorFence(HConstructorFence* constructor_fence) {
2072 constructor_fence->SetLocations(nullptr);
2073}
2074
2075void InstructionCodeGeneratorX86::VisitConstructorFence(
2076 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
2077 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
2078}
2079
Calin Juravle27df7582015-04-17 19:12:31 +01002080void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2081 memory_barrier->SetLocations(nullptr);
2082}
2083
2084void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00002085 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002086}
2087
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002088void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002089 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002090}
2091
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002092void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002093 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002094}
2095
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002096void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002097 LocationSummary* locations =
2098 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002099 switch (ret->InputAt(0)->GetType()) {
2100 case Primitive::kPrimBoolean:
2101 case Primitive::kPrimByte:
2102 case Primitive::kPrimChar:
2103 case Primitive::kPrimShort:
2104 case Primitive::kPrimInt:
2105 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002106 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002107 break;
2108
2109 case Primitive::kPrimLong:
2110 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002111 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002112 break;
2113
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002114 case Primitive::kPrimFloat:
2115 case Primitive::kPrimDouble:
2116 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002117 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002118 break;
2119
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002120 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002121 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002122 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002123}
2124
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002125void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002126 if (kIsDebugBuild) {
2127 switch (ret->InputAt(0)->GetType()) {
2128 case Primitive::kPrimBoolean:
2129 case Primitive::kPrimByte:
2130 case Primitive::kPrimChar:
2131 case Primitive::kPrimShort:
2132 case Primitive::kPrimInt:
2133 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002134 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002135 break;
2136
2137 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002138 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
2139 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002140 break;
2141
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002142 case Primitive::kPrimFloat:
2143 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002144 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002145 break;
2146
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002147 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002148 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002149 }
2150 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002151 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002152}
2153
Calin Juravle175dc732015-08-25 15:42:32 +01002154void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2155 // The trampoline uses the same calling convention as dex calling conventions,
2156 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2157 // the method_idx.
2158 HandleInvoke(invoke);
2159}
2160
2161void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2162 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2163}
2164
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002165void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002166 // Explicit clinit checks triggered by static invokes must have been pruned by
2167 // art::PrepareForRegisterAllocation.
2168 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002169
Mark Mendellfb8d2792015-03-31 22:16:59 -04002170 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002171 if (intrinsic.TryDispatch(invoke)) {
Vladimir Marko65979462017-05-19 17:25:12 +01002172 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeMethodLoadKind()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00002173 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002174 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04002175 return;
2176 }
2177
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002178 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002179
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002180 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
Vladimir Marko65979462017-05-19 17:25:12 +01002181 if (invoke->HasPcRelativeMethodLoadKind()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00002182 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002183 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002184}
2185
Mark Mendell09ed1a32015-03-25 08:30:06 -04002186static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
2187 if (invoke->GetLocations()->Intrinsified()) {
2188 IntrinsicCodeGeneratorX86 intrinsic(codegen);
2189 intrinsic.Dispatch(invoke);
2190 return true;
2191 }
2192 return false;
2193}
2194
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002195void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002196 // Explicit clinit checks triggered by static invokes must have been pruned by
2197 // art::PrepareForRegisterAllocation.
2198 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002199
Mark Mendell09ed1a32015-03-25 08:30:06 -04002200 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2201 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002202 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002203
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002204 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04002205 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002206 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002207}
2208
2209void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00002210 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
2211 if (intrinsic.TryDispatch(invoke)) {
2212 return;
2213 }
2214
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002215 HandleInvoke(invoke);
2216}
2217
2218void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002219 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002220 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002221}
2222
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002223void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002224 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2225 return;
2226 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002227
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002228 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002229 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002230}
2231
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002232void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002233 // This call to HandleInvoke allocates a temporary (core) register
2234 // which is also used to transfer the hidden argument from FP to
2235 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002236 HandleInvoke(invoke);
2237 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002238 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002239}
2240
2241void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
2242 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002243 LocationSummary* locations = invoke->GetLocations();
2244 Register temp = locations->GetTemp(0).AsRegister<Register>();
2245 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002246 Location receiver = locations->InAt(0);
2247 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2248
Roland Levillain0d5a2812015-11-13 10:07:31 +00002249 // Set the hidden argument. This is safe to do this here, as XMM7
2250 // won't be modified thereafter, before the `call` instruction.
2251 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002252 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002253 __ movd(hidden_reg, temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002254
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002255 if (receiver.IsStackSlot()) {
2256 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002257 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002258 __ movl(temp, Address(temp, class_offset));
2259 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002260 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002261 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002262 }
Roland Levillain4d027112015-07-01 15:41:14 +01002263 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002264 // Instead of simply (possibly) unpoisoning `temp` here, we should
2265 // emit a read barrier for the previous class reference load.
2266 // However this is not required in practice, as this is an
2267 // intermediate/temporary reference and because the current
2268 // concurrent copying collector keeps the from-space memory
2269 // intact/accessible until the end of the marking phase (the
2270 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002271 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002272 // temp = temp->GetAddressOfIMT()
2273 __ movl(temp,
2274 Address(temp, mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002275 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002276 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002277 invoke->GetImtIndex(), kX86PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002278 __ movl(temp, Address(temp, method_offset));
2279 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002280 __ call(Address(temp,
Andreas Gampe542451c2016-07-26 09:02:02 -07002281 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002282
2283 DCHECK(!codegen_->IsLeafMethod());
2284 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2285}
2286
Orion Hodsonac141392017-01-13 11:53:47 +00002287void LocationsBuilderX86::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2288 HandleInvoke(invoke);
2289}
2290
2291void InstructionCodeGeneratorX86::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2292 codegen_->GenerateInvokePolymorphicCall(invoke);
2293}
2294
Roland Levillain88cb1752014-10-20 16:36:47 +01002295void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2296 LocationSummary* locations =
2297 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2298 switch (neg->GetResultType()) {
2299 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002300 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002301 locations->SetInAt(0, Location::RequiresRegister());
2302 locations->SetOut(Location::SameAsFirstInput());
2303 break;
2304
Roland Levillain88cb1752014-10-20 16:36:47 +01002305 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00002306 locations->SetInAt(0, Location::RequiresFpuRegister());
2307 locations->SetOut(Location::SameAsFirstInput());
2308 locations->AddTemp(Location::RequiresRegister());
2309 locations->AddTemp(Location::RequiresFpuRegister());
2310 break;
2311
Roland Levillain88cb1752014-10-20 16:36:47 +01002312 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002313 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002314 locations->SetOut(Location::SameAsFirstInput());
2315 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002316 break;
2317
2318 default:
2319 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2320 }
2321}
2322
2323void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2324 LocationSummary* locations = neg->GetLocations();
2325 Location out = locations->Out();
2326 Location in = locations->InAt(0);
2327 switch (neg->GetResultType()) {
2328 case Primitive::kPrimInt:
2329 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002330 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002331 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002332 break;
2333
2334 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002335 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002336 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002337 __ negl(out.AsRegisterPairLow<Register>());
2338 // Negation is similar to subtraction from zero. The least
2339 // significant byte triggers a borrow when it is different from
2340 // zero; to take it into account, add 1 to the most significant
2341 // byte if the carry flag (CF) is set to 1 after the first NEGL
2342 // operation.
2343 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2344 __ negl(out.AsRegisterPairHigh<Register>());
2345 break;
2346
Roland Levillain5368c212014-11-27 15:03:41 +00002347 case Primitive::kPrimFloat: {
2348 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002349 Register constant = locations->GetTemp(0).AsRegister<Register>();
2350 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002351 // Implement float negation with an exclusive or with value
2352 // 0x80000000 (mask for bit 31, representing the sign of a
2353 // single-precision floating-point number).
2354 __ movl(constant, Immediate(INT32_C(0x80000000)));
2355 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002356 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002357 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002358 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002359
Roland Levillain5368c212014-11-27 15:03:41 +00002360 case Primitive::kPrimDouble: {
2361 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002362 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002363 // Implement double negation with an exclusive or with value
2364 // 0x8000000000000000 (mask for bit 63, representing the sign of
2365 // a double-precision floating-point number).
2366 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002367 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002368 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002369 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002370
2371 default:
2372 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2373 }
2374}
2375
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002376void LocationsBuilderX86::VisitX86FPNeg(HX86FPNeg* neg) {
2377 LocationSummary* locations =
2378 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2379 DCHECK(Primitive::IsFloatingPointType(neg->GetType()));
2380 locations->SetInAt(0, Location::RequiresFpuRegister());
2381 locations->SetInAt(1, Location::RequiresRegister());
2382 locations->SetOut(Location::SameAsFirstInput());
2383 locations->AddTemp(Location::RequiresFpuRegister());
2384}
2385
2386void InstructionCodeGeneratorX86::VisitX86FPNeg(HX86FPNeg* neg) {
2387 LocationSummary* locations = neg->GetLocations();
2388 Location out = locations->Out();
2389 DCHECK(locations->InAt(0).Equals(out));
2390
2391 Register constant_area = locations->InAt(1).AsRegister<Register>();
2392 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2393 if (neg->GetType() == Primitive::kPrimFloat) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00002394 __ movss(mask, codegen_->LiteralInt32Address(INT32_C(0x80000000),
2395 neg->GetBaseMethodAddress(),
2396 constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002397 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
2398 } else {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00002399 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000),
2400 neg->GetBaseMethodAddress(),
2401 constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002402 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
2403 }
2404}
2405
Roland Levillaindff1f282014-11-05 14:15:05 +00002406void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002407 Primitive::Type result_type = conversion->GetResultType();
2408 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002409 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002410
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002411 // The float-to-long and double-to-long type conversions rely on a
2412 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002413 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002414 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2415 && result_type == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002416 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002417 : LocationSummary::kNoCall;
2418 LocationSummary* locations =
2419 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2420
David Brazdilb2bd1c52015-03-25 11:17:37 +00002421 // The Java language does not allow treating boolean as an integral type but
2422 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002423
Roland Levillaindff1f282014-11-05 14:15:05 +00002424 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002425 case Primitive::kPrimByte:
2426 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002427 case Primitive::kPrimLong: {
2428 // Type conversion from long to byte is a result of code transformations.
2429 HInstruction* input = conversion->InputAt(0);
2430 Location input_location = input->IsConstant()
2431 ? Location::ConstantLocation(input->AsConstant())
2432 : Location::RegisterPairLocation(EAX, EDX);
2433 locations->SetInAt(0, input_location);
2434 // Make the output overlap to please the register allocator. This greatly simplifies
2435 // the validation of the linear scan implementation
2436 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2437 break;
2438 }
David Brazdil46e2a392015-03-16 17:31:52 +00002439 case Primitive::kPrimBoolean:
2440 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002441 case Primitive::kPrimShort:
2442 case Primitive::kPrimInt:
2443 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002444 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05002445 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2446 // Make the output overlap to please the register allocator. This greatly simplifies
2447 // the validation of the linear scan implementation
2448 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002449 break;
2450
2451 default:
2452 LOG(FATAL) << "Unexpected type conversion from " << input_type
2453 << " to " << result_type;
2454 }
2455 break;
2456
Roland Levillain01a8d712014-11-14 16:27:39 +00002457 case Primitive::kPrimShort:
2458 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002459 case Primitive::kPrimLong:
2460 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002461 case Primitive::kPrimBoolean:
2462 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002463 case Primitive::kPrimByte:
2464 case Primitive::kPrimInt:
2465 case Primitive::kPrimChar:
2466 // Processing a Dex `int-to-short' instruction.
2467 locations->SetInAt(0, Location::Any());
2468 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2469 break;
2470
2471 default:
2472 LOG(FATAL) << "Unexpected type conversion from " << input_type
2473 << " to " << result_type;
2474 }
2475 break;
2476
Roland Levillain946e1432014-11-11 17:35:19 +00002477 case Primitive::kPrimInt:
2478 switch (input_type) {
2479 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002480 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002481 locations->SetInAt(0, Location::Any());
2482 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2483 break;
2484
2485 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002486 // Processing a Dex `float-to-int' instruction.
2487 locations->SetInAt(0, Location::RequiresFpuRegister());
2488 locations->SetOut(Location::RequiresRegister());
2489 locations->AddTemp(Location::RequiresFpuRegister());
2490 break;
2491
Roland Levillain946e1432014-11-11 17:35:19 +00002492 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002493 // Processing a Dex `double-to-int' instruction.
2494 locations->SetInAt(0, Location::RequiresFpuRegister());
2495 locations->SetOut(Location::RequiresRegister());
2496 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002497 break;
2498
2499 default:
2500 LOG(FATAL) << "Unexpected type conversion from " << input_type
2501 << " to " << result_type;
2502 }
2503 break;
2504
Roland Levillaindff1f282014-11-05 14:15:05 +00002505 case Primitive::kPrimLong:
2506 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002507 case Primitive::kPrimBoolean:
2508 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002509 case Primitive::kPrimByte:
2510 case Primitive::kPrimShort:
2511 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002512 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002513 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002514 locations->SetInAt(0, Location::RegisterLocation(EAX));
2515 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2516 break;
2517
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002518 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00002519 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002520 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00002521 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002522 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2523 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2524
Vladimir Marko949c91f2015-01-27 10:48:44 +00002525 // The runtime helper puts the result in EAX, EDX.
2526 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002527 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002528 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002529
2530 default:
2531 LOG(FATAL) << "Unexpected type conversion from " << input_type
2532 << " to " << result_type;
2533 }
2534 break;
2535
Roland Levillain981e4542014-11-14 11:47:14 +00002536 case Primitive::kPrimChar:
2537 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002538 case Primitive::kPrimLong:
2539 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002540 case Primitive::kPrimBoolean:
2541 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002542 case Primitive::kPrimByte:
2543 case Primitive::kPrimShort:
2544 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002545 // Processing a Dex `int-to-char' instruction.
2546 locations->SetInAt(0, Location::Any());
2547 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2548 break;
2549
2550 default:
2551 LOG(FATAL) << "Unexpected type conversion from " << input_type
2552 << " to " << result_type;
2553 }
2554 break;
2555
Roland Levillaindff1f282014-11-05 14:15:05 +00002556 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002557 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002558 case Primitive::kPrimBoolean:
2559 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002560 case Primitive::kPrimByte:
2561 case Primitive::kPrimShort:
2562 case Primitive::kPrimInt:
2563 case Primitive::kPrimChar:
2564 // Processing a Dex `int-to-float' instruction.
2565 locations->SetInAt(0, Location::RequiresRegister());
2566 locations->SetOut(Location::RequiresFpuRegister());
2567 break;
2568
2569 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002570 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002571 locations->SetInAt(0, Location::Any());
2572 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002573 break;
2574
Roland Levillaincff13742014-11-17 14:32:17 +00002575 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002576 // Processing a Dex `double-to-float' instruction.
2577 locations->SetInAt(0, Location::RequiresFpuRegister());
2578 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002579 break;
2580
2581 default:
2582 LOG(FATAL) << "Unexpected type conversion from " << input_type
2583 << " to " << result_type;
2584 };
2585 break;
2586
Roland Levillaindff1f282014-11-05 14:15:05 +00002587 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002588 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002589 case Primitive::kPrimBoolean:
2590 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002591 case Primitive::kPrimByte:
2592 case Primitive::kPrimShort:
2593 case Primitive::kPrimInt:
2594 case Primitive::kPrimChar:
2595 // Processing a Dex `int-to-double' instruction.
2596 locations->SetInAt(0, Location::RequiresRegister());
2597 locations->SetOut(Location::RequiresFpuRegister());
2598 break;
2599
2600 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002601 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002602 locations->SetInAt(0, Location::Any());
2603 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002604 break;
2605
Roland Levillaincff13742014-11-17 14:32:17 +00002606 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002607 // Processing a Dex `float-to-double' instruction.
2608 locations->SetInAt(0, Location::RequiresFpuRegister());
2609 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002610 break;
2611
2612 default:
2613 LOG(FATAL) << "Unexpected type conversion from " << input_type
2614 << " to " << result_type;
2615 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002616 break;
2617
2618 default:
2619 LOG(FATAL) << "Unexpected type conversion from " << input_type
2620 << " to " << result_type;
2621 }
2622}
2623
2624void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2625 LocationSummary* locations = conversion->GetLocations();
2626 Location out = locations->Out();
2627 Location in = locations->InAt(0);
2628 Primitive::Type result_type = conversion->GetResultType();
2629 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002630 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002631 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002632 case Primitive::kPrimByte:
2633 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002634 case Primitive::kPrimLong:
2635 // Type conversion from long to byte is a result of code transformations.
2636 if (in.IsRegisterPair()) {
2637 __ movsxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2638 } else {
2639 DCHECK(in.GetConstant()->IsLongConstant());
2640 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2641 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2642 }
2643 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002644 case Primitive::kPrimBoolean:
2645 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002646 case Primitive::kPrimShort:
2647 case Primitive::kPrimInt:
2648 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002649 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002650 if (in.IsRegister()) {
2651 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002652 } else {
2653 DCHECK(in.GetConstant()->IsIntConstant());
2654 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2655 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2656 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002657 break;
2658
2659 default:
2660 LOG(FATAL) << "Unexpected type conversion from " << input_type
2661 << " to " << result_type;
2662 }
2663 break;
2664
Roland Levillain01a8d712014-11-14 16:27:39 +00002665 case Primitive::kPrimShort:
2666 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002667 case Primitive::kPrimLong:
2668 // Type conversion from long to short is a result of code transformations.
2669 if (in.IsRegisterPair()) {
2670 __ movsxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2671 } else if (in.IsDoubleStackSlot()) {
2672 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2673 } else {
2674 DCHECK(in.GetConstant()->IsLongConstant());
2675 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2676 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
2677 }
2678 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002679 case Primitive::kPrimBoolean:
2680 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002681 case Primitive::kPrimByte:
2682 case Primitive::kPrimInt:
2683 case Primitive::kPrimChar:
2684 // Processing a Dex `int-to-short' instruction.
2685 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002686 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002687 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002688 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002689 } else {
2690 DCHECK(in.GetConstant()->IsIntConstant());
2691 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002692 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002693 }
2694 break;
2695
2696 default:
2697 LOG(FATAL) << "Unexpected type conversion from " << input_type
2698 << " to " << result_type;
2699 }
2700 break;
2701
Roland Levillain946e1432014-11-11 17:35:19 +00002702 case Primitive::kPrimInt:
2703 switch (input_type) {
2704 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002705 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002706 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002707 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002708 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002709 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002710 } else {
2711 DCHECK(in.IsConstant());
2712 DCHECK(in.GetConstant()->IsLongConstant());
2713 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002714 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002715 }
2716 break;
2717
Roland Levillain3f8f9362014-12-02 17:45:01 +00002718 case Primitive::kPrimFloat: {
2719 // Processing a Dex `float-to-int' instruction.
2720 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2721 Register output = out.AsRegister<Register>();
2722 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002723 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002724
2725 __ movl(output, Immediate(kPrimIntMax));
2726 // temp = int-to-float(output)
2727 __ cvtsi2ss(temp, output);
2728 // if input >= temp goto done
2729 __ comiss(input, temp);
2730 __ j(kAboveEqual, &done);
2731 // if input == NaN goto nan
2732 __ j(kUnordered, &nan);
2733 // output = float-to-int-truncate(input)
2734 __ cvttss2si(output, input);
2735 __ jmp(&done);
2736 __ Bind(&nan);
2737 // output = 0
2738 __ xorl(output, output);
2739 __ Bind(&done);
2740 break;
2741 }
2742
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002743 case Primitive::kPrimDouble: {
2744 // Processing a Dex `double-to-int' instruction.
2745 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2746 Register output = out.AsRegister<Register>();
2747 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002748 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002749
2750 __ movl(output, Immediate(kPrimIntMax));
2751 // temp = int-to-double(output)
2752 __ cvtsi2sd(temp, output);
2753 // if input >= temp goto done
2754 __ comisd(input, temp);
2755 __ j(kAboveEqual, &done);
2756 // if input == NaN goto nan
2757 __ j(kUnordered, &nan);
2758 // output = double-to-int-truncate(input)
2759 __ cvttsd2si(output, input);
2760 __ jmp(&done);
2761 __ Bind(&nan);
2762 // output = 0
2763 __ xorl(output, output);
2764 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002765 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002766 }
Roland Levillain946e1432014-11-11 17:35:19 +00002767
2768 default:
2769 LOG(FATAL) << "Unexpected type conversion from " << input_type
2770 << " to " << result_type;
2771 }
2772 break;
2773
Roland Levillaindff1f282014-11-05 14:15:05 +00002774 case Primitive::kPrimLong:
2775 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002776 case Primitive::kPrimBoolean:
2777 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002778 case Primitive::kPrimByte:
2779 case Primitive::kPrimShort:
2780 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002781 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002782 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002783 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2784 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002785 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002786 __ cdq();
2787 break;
2788
2789 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002790 // Processing a Dex `float-to-long' instruction.
Serban Constantinescuba45db02016-07-12 22:53:02 +01002791 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002792 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002793 break;
2794
Roland Levillaindff1f282014-11-05 14:15:05 +00002795 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002796 // Processing a Dex `double-to-long' instruction.
Serban Constantinescuba45db02016-07-12 22:53:02 +01002797 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002798 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002799 break;
2800
2801 default:
2802 LOG(FATAL) << "Unexpected type conversion from " << input_type
2803 << " to " << result_type;
2804 }
2805 break;
2806
Roland Levillain981e4542014-11-14 11:47:14 +00002807 case Primitive::kPrimChar:
2808 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002809 case Primitive::kPrimLong:
2810 // Type conversion from long to short is a result of code transformations.
2811 if (in.IsRegisterPair()) {
2812 __ movzxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2813 } else if (in.IsDoubleStackSlot()) {
2814 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2815 } else {
2816 DCHECK(in.GetConstant()->IsLongConstant());
2817 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2818 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
2819 }
2820 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002821 case Primitive::kPrimBoolean:
2822 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002823 case Primitive::kPrimByte:
2824 case Primitive::kPrimShort:
2825 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002826 // Processing a Dex `Process a Dex `int-to-char'' instruction.
2827 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002828 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00002829 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002830 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00002831 } else {
2832 DCHECK(in.GetConstant()->IsIntConstant());
2833 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002834 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00002835 }
2836 break;
2837
2838 default:
2839 LOG(FATAL) << "Unexpected type conversion from " << input_type
2840 << " to " << result_type;
2841 }
2842 break;
2843
Roland Levillaindff1f282014-11-05 14:15:05 +00002844 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002845 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002846 case Primitive::kPrimBoolean:
2847 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002848 case Primitive::kPrimByte:
2849 case Primitive::kPrimShort:
2850 case Primitive::kPrimInt:
2851 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002852 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002853 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002854 break;
2855
Roland Levillain6d0e4832014-11-27 18:31:21 +00002856 case Primitive::kPrimLong: {
2857 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002858 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002859
Roland Levillain232ade02015-04-20 15:14:36 +01002860 // Create stack space for the call to
2861 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2862 // TODO: enhance register allocator to ask for stack temporaries.
2863 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
2864 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2865 __ subl(ESP, Immediate(adjustment));
2866 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002867
Roland Levillain232ade02015-04-20 15:14:36 +01002868 // Load the value to the FP stack, using temporaries if needed.
2869 PushOntoFPStack(in, 0, adjustment, false, true);
2870
2871 if (out.IsStackSlot()) {
2872 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2873 } else {
2874 __ fstps(Address(ESP, 0));
2875 Location stack_temp = Location::StackSlot(0);
2876 codegen_->Move32(out, stack_temp);
2877 }
2878
2879 // Remove the temporary stack space we allocated.
2880 if (adjustment != 0) {
2881 __ addl(ESP, Immediate(adjustment));
2882 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002883 break;
2884 }
2885
Roland Levillaincff13742014-11-17 14:32:17 +00002886 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002887 // Processing a Dex `double-to-float' instruction.
2888 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002889 break;
2890
2891 default:
2892 LOG(FATAL) << "Unexpected type conversion from " << input_type
2893 << " to " << result_type;
2894 };
2895 break;
2896
Roland Levillaindff1f282014-11-05 14:15:05 +00002897 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002898 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002899 case Primitive::kPrimBoolean:
2900 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002901 case Primitive::kPrimByte:
2902 case Primitive::kPrimShort:
2903 case Primitive::kPrimInt:
2904 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002905 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002906 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002907 break;
2908
Roland Levillain647b9ed2014-11-27 12:06:00 +00002909 case Primitive::kPrimLong: {
2910 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002911 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002912
Roland Levillain232ade02015-04-20 15:14:36 +01002913 // Create stack space for the call to
2914 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2915 // TODO: enhance register allocator to ask for stack temporaries.
2916 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
2917 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2918 __ subl(ESP, Immediate(adjustment));
2919 }
2920
2921 // Load the value to the FP stack, using temporaries if needed.
2922 PushOntoFPStack(in, 0, adjustment, false, true);
2923
2924 if (out.IsDoubleStackSlot()) {
2925 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2926 } else {
2927 __ fstpl(Address(ESP, 0));
2928 Location stack_temp = Location::DoubleStackSlot(0);
2929 codegen_->Move64(out, stack_temp);
2930 }
2931
2932 // Remove the temporary stack space we allocated.
2933 if (adjustment != 0) {
2934 __ addl(ESP, Immediate(adjustment));
2935 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002936 break;
2937 }
2938
Roland Levillaincff13742014-11-17 14:32:17 +00002939 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002940 // Processing a Dex `float-to-double' instruction.
2941 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002942 break;
2943
2944 default:
2945 LOG(FATAL) << "Unexpected type conversion from " << input_type
2946 << " to " << result_type;
2947 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002948 break;
2949
2950 default:
2951 LOG(FATAL) << "Unexpected type conversion from " << input_type
2952 << " to " << result_type;
2953 }
2954}
2955
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002956void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002957 LocationSummary* locations =
2958 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002959 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002960 case Primitive::kPrimInt: {
2961 locations->SetInAt(0, Location::RequiresRegister());
2962 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2963 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2964 break;
2965 }
2966
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002967 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002968 locations->SetInAt(0, Location::RequiresRegister());
2969 locations->SetInAt(1, Location::Any());
2970 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002971 break;
2972 }
2973
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002974 case Primitive::kPrimFloat:
2975 case Primitive::kPrimDouble: {
2976 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002977 if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2978 DCHECK(add->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002979 } else if (add->InputAt(1)->IsConstant()) {
2980 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002981 } else {
2982 locations->SetInAt(1, Location::Any());
2983 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002984 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002985 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002986 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002987
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002988 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002989 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2990 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002991 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002992}
2993
2994void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2995 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002996 Location first = locations->InAt(0);
2997 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002998 Location out = locations->Out();
2999
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003000 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003001 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003002 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05003003 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3004 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04003005 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
3006 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05003007 } else {
3008 __ leal(out.AsRegister<Register>(), Address(
3009 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
3010 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003011 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05003012 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
3013 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3014 __ addl(out.AsRegister<Register>(), Immediate(value));
3015 } else {
3016 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
3017 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003018 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05003019 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003020 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003021 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003022 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003023 }
3024
3025 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003026 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003027 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3028 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003029 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003030 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3031 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003032 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003033 } else {
3034 DCHECK(second.IsConstant()) << second;
3035 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3036 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3037 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003038 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003039 break;
3040 }
3041
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003042 case Primitive::kPrimFloat: {
3043 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003044 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04003045 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3046 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003047 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003048 __ addss(first.AsFpuRegister<XmmRegister>(),
3049 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003050 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3051 const_area->GetBaseMethodAddress(),
3052 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003053 } else {
3054 DCHECK(second.IsStackSlot());
3055 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003056 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003057 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003058 }
3059
3060 case Primitive::kPrimDouble: {
3061 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003062 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04003063 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3064 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003065 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003066 __ addsd(first.AsFpuRegister<XmmRegister>(),
3067 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003068 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3069 const_area->GetBaseMethodAddress(),
3070 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003071 } else {
3072 DCHECK(second.IsDoubleStackSlot());
3073 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003074 }
3075 break;
3076 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003077
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003078 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003079 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003080 }
3081}
3082
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003083void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003084 LocationSummary* locations =
3085 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003086 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003087 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003088 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003089 locations->SetInAt(0, Location::RequiresRegister());
3090 locations->SetInAt(1, Location::Any());
3091 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003092 break;
3093 }
Calin Juravle11351682014-10-23 15:38:15 +01003094 case Primitive::kPrimFloat:
3095 case Primitive::kPrimDouble: {
3096 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003097 if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3098 DCHECK(sub->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003099 } else if (sub->InputAt(1)->IsConstant()) {
3100 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003101 } else {
3102 locations->SetInAt(1, Location::Any());
3103 }
Calin Juravle11351682014-10-23 15:38:15 +01003104 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003105 break;
Calin Juravle11351682014-10-23 15:38:15 +01003106 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003107
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003108 default:
Calin Juravle11351682014-10-23 15:38:15 +01003109 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003110 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003111}
3112
3113void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
3114 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003115 Location first = locations->InAt(0);
3116 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01003117 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003118 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003119 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003120 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003121 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003122 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003123 __ subl(first.AsRegister<Register>(),
3124 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003125 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003126 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003127 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003128 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003129 }
3130
3131 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003132 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01003133 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3134 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003135 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01003136 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003137 __ sbbl(first.AsRegisterPairHigh<Register>(),
3138 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003139 } else {
3140 DCHECK(second.IsConstant()) << second;
3141 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3142 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3143 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003144 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003145 break;
3146 }
3147
Calin Juravle11351682014-10-23 15:38:15 +01003148 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003149 if (second.IsFpuRegister()) {
3150 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3151 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3152 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003153 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003154 __ subss(first.AsFpuRegister<XmmRegister>(),
3155 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003156 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3157 const_area->GetBaseMethodAddress(),
3158 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003159 } else {
3160 DCHECK(second.IsStackSlot());
3161 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3162 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003163 break;
Calin Juravle11351682014-10-23 15:38:15 +01003164 }
3165
3166 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003167 if (second.IsFpuRegister()) {
3168 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3169 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3170 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003171 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003172 __ subsd(first.AsFpuRegister<XmmRegister>(),
3173 codegen_->LiteralDoubleAddress(
3174 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003175 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003176 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3177 } else {
3178 DCHECK(second.IsDoubleStackSlot());
3179 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3180 }
Calin Juravle11351682014-10-23 15:38:15 +01003181 break;
3182 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003183
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003184 default:
Calin Juravle11351682014-10-23 15:38:15 +01003185 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003186 }
3187}
3188
Calin Juravle34bacdf2014-10-07 20:23:36 +01003189void LocationsBuilderX86::VisitMul(HMul* mul) {
3190 LocationSummary* locations =
3191 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3192 switch (mul->GetResultType()) {
3193 case Primitive::kPrimInt:
3194 locations->SetInAt(0, Location::RequiresRegister());
3195 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003196 if (mul->InputAt(1)->IsIntConstant()) {
3197 // Can use 3 operand multiply.
3198 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3199 } else {
3200 locations->SetOut(Location::SameAsFirstInput());
3201 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003202 break;
3203 case Primitive::kPrimLong: {
3204 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003205 locations->SetInAt(1, Location::Any());
3206 locations->SetOut(Location::SameAsFirstInput());
3207 // Needed for imul on 32bits with 64bits output.
3208 locations->AddTemp(Location::RegisterLocation(EAX));
3209 locations->AddTemp(Location::RegisterLocation(EDX));
3210 break;
3211 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003212 case Primitive::kPrimFloat:
3213 case Primitive::kPrimDouble: {
3214 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003215 if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3216 DCHECK(mul->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003217 } else if (mul->InputAt(1)->IsConstant()) {
3218 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003219 } else {
3220 locations->SetInAt(1, Location::Any());
3221 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003222 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003223 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003224 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003225
3226 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003227 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003228 }
3229}
3230
3231void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
3232 LocationSummary* locations = mul->GetLocations();
3233 Location first = locations->InAt(0);
3234 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003235 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003236
3237 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003238 case Primitive::kPrimInt:
3239 // The constant may have ended up in a register, so test explicitly to avoid
3240 // problems where the output may not be the same as the first operand.
3241 if (mul->InputAt(1)->IsIntConstant()) {
3242 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3243 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
3244 } else if (second.IsRegister()) {
3245 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003246 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003247 } else {
3248 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003249 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003250 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003251 }
3252 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003253
3254 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003255 Register in1_hi = first.AsRegisterPairHigh<Register>();
3256 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003257 Register eax = locations->GetTemp(0).AsRegister<Register>();
3258 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003259
3260 DCHECK_EQ(EAX, eax);
3261 DCHECK_EQ(EDX, edx);
3262
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003263 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01003264 // output: in1
3265 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3266 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3267 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003268 if (second.IsConstant()) {
3269 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003270
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003271 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3272 int32_t low_value = Low32Bits(value);
3273 int32_t high_value = High32Bits(value);
3274 Immediate low(low_value);
3275 Immediate high(high_value);
3276
3277 __ movl(eax, high);
3278 // eax <- in1.lo * in2.hi
3279 __ imull(eax, in1_lo);
3280 // in1.hi <- in1.hi * in2.lo
3281 __ imull(in1_hi, low);
3282 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3283 __ addl(in1_hi, eax);
3284 // move in2_lo to eax to prepare for double precision
3285 __ movl(eax, low);
3286 // edx:eax <- in1.lo * in2.lo
3287 __ mull(in1_lo);
3288 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3289 __ addl(in1_hi, edx);
3290 // in1.lo <- (in1.lo * in2.lo)[31:0];
3291 __ movl(in1_lo, eax);
3292 } else if (second.IsRegisterPair()) {
3293 Register in2_hi = second.AsRegisterPairHigh<Register>();
3294 Register in2_lo = second.AsRegisterPairLow<Register>();
3295
3296 __ movl(eax, in2_hi);
3297 // eax <- in1.lo * in2.hi
3298 __ imull(eax, in1_lo);
3299 // in1.hi <- in1.hi * in2.lo
3300 __ imull(in1_hi, in2_lo);
3301 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3302 __ addl(in1_hi, eax);
3303 // move in1_lo to eax to prepare for double precision
3304 __ movl(eax, in1_lo);
3305 // edx:eax <- in1.lo * in2.lo
3306 __ mull(in2_lo);
3307 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3308 __ addl(in1_hi, edx);
3309 // in1.lo <- (in1.lo * in2.lo)[31:0];
3310 __ movl(in1_lo, eax);
3311 } else {
3312 DCHECK(second.IsDoubleStackSlot()) << second;
3313 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
3314 Address in2_lo(ESP, second.GetStackIndex());
3315
3316 __ movl(eax, in2_hi);
3317 // eax <- in1.lo * in2.hi
3318 __ imull(eax, in1_lo);
3319 // in1.hi <- in1.hi * in2.lo
3320 __ imull(in1_hi, in2_lo);
3321 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3322 __ addl(in1_hi, eax);
3323 // move in1_lo to eax to prepare for double precision
3324 __ movl(eax, in1_lo);
3325 // edx:eax <- in1.lo * in2.lo
3326 __ mull(in2_lo);
3327 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3328 __ addl(in1_hi, edx);
3329 // in1.lo <- (in1.lo * in2.lo)[31:0];
3330 __ movl(in1_lo, eax);
3331 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003332
3333 break;
3334 }
3335
Calin Juravleb5bfa962014-10-21 18:02:24 +01003336 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003337 DCHECK(first.Equals(locations->Out()));
3338 if (second.IsFpuRegister()) {
3339 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3340 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3341 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003342 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003343 __ mulss(first.AsFpuRegister<XmmRegister>(),
3344 codegen_->LiteralFloatAddress(
3345 const_area->GetConstant()->AsFloatConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003346 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003347 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3348 } else {
3349 DCHECK(second.IsStackSlot());
3350 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3351 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003352 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003353 }
3354
3355 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003356 DCHECK(first.Equals(locations->Out()));
3357 if (second.IsFpuRegister()) {
3358 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3359 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3360 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003361 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003362 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3363 codegen_->LiteralDoubleAddress(
3364 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003365 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003366 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3367 } else {
3368 DCHECK(second.IsDoubleStackSlot());
3369 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3370 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003371 break;
3372 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003373
3374 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003375 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003376 }
3377}
3378
Roland Levillain232ade02015-04-20 15:14:36 +01003379void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3380 uint32_t temp_offset,
3381 uint32_t stack_adjustment,
3382 bool is_fp,
3383 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003384 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003385 DCHECK(!is_wide);
3386 if (is_fp) {
3387 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3388 } else {
3389 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3390 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003391 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003392 DCHECK(is_wide);
3393 if (is_fp) {
3394 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3395 } else {
3396 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3397 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003398 } else {
3399 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003400 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003401 Location stack_temp = Location::StackSlot(temp_offset);
3402 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003403 if (is_fp) {
3404 __ flds(Address(ESP, temp_offset));
3405 } else {
3406 __ filds(Address(ESP, temp_offset));
3407 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003408 } else {
3409 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3410 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003411 if (is_fp) {
3412 __ fldl(Address(ESP, temp_offset));
3413 } else {
3414 __ fildl(Address(ESP, temp_offset));
3415 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003416 }
3417 }
3418}
3419
3420void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
3421 Primitive::Type type = rem->GetResultType();
3422 bool is_float = type == Primitive::kPrimFloat;
3423 size_t elem_size = Primitive::ComponentSize(type);
3424 LocationSummary* locations = rem->GetLocations();
3425 Location first = locations->InAt(0);
3426 Location second = locations->InAt(1);
3427 Location out = locations->Out();
3428
3429 // Create stack space for 2 elements.
3430 // TODO: enhance register allocator to ask for stack temporaries.
3431 __ subl(ESP, Immediate(2 * elem_size));
3432
3433 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003434 const bool is_wide = !is_float;
3435 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
3436 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003437
3438 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003439 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003440 __ Bind(&retry);
3441 __ fprem();
3442
3443 // Move FP status to AX.
3444 __ fstsw();
3445
3446 // And see if the argument reduction is complete. This is signaled by the
3447 // C2 FPU flag bit set to 0.
3448 __ andl(EAX, Immediate(kC2ConditionMask));
3449 __ j(kNotEqual, &retry);
3450
3451 // We have settled on the final value. Retrieve it into an XMM register.
3452 // Store FP top of stack to real stack.
3453 if (is_float) {
3454 __ fsts(Address(ESP, 0));
3455 } else {
3456 __ fstl(Address(ESP, 0));
3457 }
3458
3459 // Pop the 2 items from the FP stack.
3460 __ fucompp();
3461
3462 // Load the value from the stack into an XMM register.
3463 DCHECK(out.IsFpuRegister()) << out;
3464 if (is_float) {
3465 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3466 } else {
3467 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3468 }
3469
3470 // And remove the temporary stack space we allocated.
3471 __ addl(ESP, Immediate(2 * elem_size));
3472}
3473
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003474
3475void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3476 DCHECK(instruction->IsDiv() || instruction->IsRem());
3477
3478 LocationSummary* locations = instruction->GetLocations();
3479 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003480 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003481
3482 Register out_register = locations->Out().AsRegister<Register>();
3483 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003484 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003485
3486 DCHECK(imm == 1 || imm == -1);
3487
3488 if (instruction->IsRem()) {
3489 __ xorl(out_register, out_register);
3490 } else {
3491 __ movl(out_register, input_register);
3492 if (imm == -1) {
3493 __ negl(out_register);
3494 }
3495 }
3496}
3497
3498
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003499void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003500 LocationSummary* locations = instruction->GetLocations();
3501
3502 Register out_register = locations->Out().AsRegister<Register>();
3503 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003504 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003505 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3506 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003507
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003508 Register num = locations->GetTemp(0).AsRegister<Register>();
3509
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003510 __ leal(num, Address(input_register, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003511 __ testl(input_register, input_register);
3512 __ cmovl(kGreaterEqual, num, input_register);
3513 int shift = CTZ(imm);
3514 __ sarl(num, Immediate(shift));
3515
3516 if (imm < 0) {
3517 __ negl(num);
3518 }
3519
3520 __ movl(out_register, num);
3521}
3522
3523void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3524 DCHECK(instruction->IsDiv() || instruction->IsRem());
3525
3526 LocationSummary* locations = instruction->GetLocations();
3527 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3528
3529 Register eax = locations->InAt(0).AsRegister<Register>();
3530 Register out = locations->Out().AsRegister<Register>();
3531 Register num;
3532 Register edx;
3533
3534 if (instruction->IsDiv()) {
3535 edx = locations->GetTemp(0).AsRegister<Register>();
3536 num = locations->GetTemp(1).AsRegister<Register>();
3537 } else {
3538 edx = locations->Out().AsRegister<Register>();
3539 num = locations->GetTemp(0).AsRegister<Register>();
3540 }
3541
3542 DCHECK_EQ(EAX, eax);
3543 DCHECK_EQ(EDX, edx);
3544 if (instruction->IsDiv()) {
3545 DCHECK_EQ(EAX, out);
3546 } else {
3547 DCHECK_EQ(EDX, out);
3548 }
3549
3550 int64_t magic;
3551 int shift;
3552 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3553
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003554 // Save the numerator.
3555 __ movl(num, eax);
3556
3557 // EAX = magic
3558 __ movl(eax, Immediate(magic));
3559
3560 // EDX:EAX = magic * numerator
3561 __ imull(num);
3562
3563 if (imm > 0 && magic < 0) {
3564 // EDX += num
3565 __ addl(edx, num);
3566 } else if (imm < 0 && magic > 0) {
3567 __ subl(edx, num);
3568 }
3569
3570 // Shift if needed.
3571 if (shift != 0) {
3572 __ sarl(edx, Immediate(shift));
3573 }
3574
3575 // EDX += 1 if EDX < 0
3576 __ movl(eax, edx);
3577 __ shrl(edx, Immediate(31));
3578 __ addl(edx, eax);
3579
3580 if (instruction->IsRem()) {
3581 __ movl(eax, num);
3582 __ imull(edx, Immediate(imm));
3583 __ subl(eax, edx);
3584 __ movl(edx, eax);
3585 } else {
3586 __ movl(eax, edx);
3587 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003588}
3589
Calin Juravlebacfec32014-11-14 15:54:36 +00003590void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3591 DCHECK(instruction->IsDiv() || instruction->IsRem());
3592
3593 LocationSummary* locations = instruction->GetLocations();
3594 Location out = locations->Out();
3595 Location first = locations->InAt(0);
3596 Location second = locations->InAt(1);
3597 bool is_div = instruction->IsDiv();
3598
3599 switch (instruction->GetResultType()) {
3600 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003601 DCHECK_EQ(EAX, first.AsRegister<Register>());
3602 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003603
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003604 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003605 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003606
3607 if (imm == 0) {
3608 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3609 } else if (imm == 1 || imm == -1) {
3610 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003611 } else if (is_div && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003612 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003613 } else {
3614 DCHECK(imm <= -2 || imm >= 2);
3615 GenerateDivRemWithAnyConstant(instruction);
3616 }
3617 } else {
David Srbecky9cd6d372016-02-09 15:24:47 +00003618 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(
3619 instruction, out.AsRegister<Register>(), is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003620 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003621
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003622 Register second_reg = second.AsRegister<Register>();
3623 // 0x80000000/-1 triggers an arithmetic exception!
3624 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3625 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003626
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003627 __ cmpl(second_reg, Immediate(-1));
3628 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003629
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003630 // edx:eax <- sign-extended of eax
3631 __ cdq();
3632 // eax = quotient, edx = remainder
3633 __ idivl(second_reg);
3634 __ Bind(slow_path->GetExitLabel());
3635 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003636 break;
3637 }
3638
3639 case Primitive::kPrimLong: {
3640 InvokeRuntimeCallingConvention calling_convention;
3641 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3642 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3643 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3644 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3645 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3646 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3647
3648 if (is_div) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003649 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003650 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003651 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003652 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003653 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003654 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003655 break;
3656 }
3657
3658 default:
3659 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3660 }
3661}
3662
Calin Juravle7c4954d2014-10-28 16:57:40 +00003663void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003664 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003665 ? LocationSummary::kCallOnMainOnly
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003666 : LocationSummary::kNoCall;
3667 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3668
Calin Juravle7c4954d2014-10-28 16:57:40 +00003669 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003670 case Primitive::kPrimInt: {
3671 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003672 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003673 locations->SetOut(Location::SameAsFirstInput());
3674 // Intel uses edx:eax as the dividend.
3675 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003676 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3677 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3678 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003679 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003680 locations->AddTemp(Location::RequiresRegister());
3681 }
Calin Juravled0d48522014-11-04 16:40:20 +00003682 break;
3683 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003684 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003685 InvokeRuntimeCallingConvention calling_convention;
3686 locations->SetInAt(0, Location::RegisterPairLocation(
3687 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3688 locations->SetInAt(1, Location::RegisterPairLocation(
3689 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3690 // Runtime helper puts the result in EAX, EDX.
3691 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003692 break;
3693 }
3694 case Primitive::kPrimFloat:
3695 case Primitive::kPrimDouble: {
3696 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003697 if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3698 DCHECK(div->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003699 } else if (div->InputAt(1)->IsConstant()) {
3700 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003701 } else {
3702 locations->SetInAt(1, Location::Any());
3703 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003704 locations->SetOut(Location::SameAsFirstInput());
3705 break;
3706 }
3707
3708 default:
3709 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3710 }
3711}
3712
3713void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3714 LocationSummary* locations = div->GetLocations();
3715 Location first = locations->InAt(0);
3716 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003717
3718 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003719 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00003720 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003721 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003722 break;
3723 }
3724
3725 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003726 if (second.IsFpuRegister()) {
3727 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3728 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3729 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003730 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003731 __ divss(first.AsFpuRegister<XmmRegister>(),
3732 codegen_->LiteralFloatAddress(
3733 const_area->GetConstant()->AsFloatConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003734 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003735 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3736 } else {
3737 DCHECK(second.IsStackSlot());
3738 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3739 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003740 break;
3741 }
3742
3743 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003744 if (second.IsFpuRegister()) {
3745 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3746 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3747 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003748 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003749 __ divsd(first.AsFpuRegister<XmmRegister>(),
3750 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003751 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3752 const_area->GetBaseMethodAddress(),
3753 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003754 } else {
3755 DCHECK(second.IsDoubleStackSlot());
3756 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3757 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003758 break;
3759 }
3760
3761 default:
3762 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3763 }
3764}
3765
Calin Juravlebacfec32014-11-14 15:54:36 +00003766void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003767 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003768
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003769 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003770 ? LocationSummary::kCallOnMainOnly
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003771 : LocationSummary::kNoCall;
3772 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003773
Calin Juravled2ec87d2014-12-08 14:24:46 +00003774 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003775 case Primitive::kPrimInt: {
3776 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003777 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003778 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003779 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3780 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3781 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003782 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003783 locations->AddTemp(Location::RequiresRegister());
3784 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003785 break;
3786 }
3787 case Primitive::kPrimLong: {
3788 InvokeRuntimeCallingConvention calling_convention;
3789 locations->SetInAt(0, Location::RegisterPairLocation(
3790 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3791 locations->SetInAt(1, Location::RegisterPairLocation(
3792 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3793 // Runtime helper puts the result in EAX, EDX.
3794 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3795 break;
3796 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003797 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003798 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003799 locations->SetInAt(0, Location::Any());
3800 locations->SetInAt(1, Location::Any());
3801 locations->SetOut(Location::RequiresFpuRegister());
3802 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003803 break;
3804 }
3805
3806 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003807 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003808 }
3809}
3810
3811void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
3812 Primitive::Type type = rem->GetResultType();
3813 switch (type) {
3814 case Primitive::kPrimInt:
3815 case Primitive::kPrimLong: {
3816 GenerateDivRemIntegral(rem);
3817 break;
3818 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003819 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00003820 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003821 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003822 break;
3823 }
3824 default:
3825 LOG(FATAL) << "Unexpected rem type " << type;
3826 }
3827}
3828
Calin Juravled0d48522014-11-04 16:40:20 +00003829void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003830 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003831 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003832 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003833 case Primitive::kPrimByte:
3834 case Primitive::kPrimChar:
3835 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003836 case Primitive::kPrimInt: {
3837 locations->SetInAt(0, Location::Any());
3838 break;
3839 }
3840 case Primitive::kPrimLong: {
3841 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
3842 if (!instruction->IsConstant()) {
3843 locations->AddTemp(Location::RequiresRegister());
3844 }
3845 break;
3846 }
3847 default:
3848 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3849 }
Calin Juravled0d48522014-11-04 16:40:20 +00003850}
3851
3852void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003853 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003854 codegen_->AddSlowPath(slow_path);
3855
3856 LocationSummary* locations = instruction->GetLocations();
3857 Location value = locations->InAt(0);
3858
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003859 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003860 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003861 case Primitive::kPrimByte:
3862 case Primitive::kPrimChar:
3863 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003864 case Primitive::kPrimInt: {
3865 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003866 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003867 __ j(kEqual, slow_path->GetEntryLabel());
3868 } else if (value.IsStackSlot()) {
3869 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
3870 __ j(kEqual, slow_path->GetEntryLabel());
3871 } else {
3872 DCHECK(value.IsConstant()) << value;
3873 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01003874 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003875 }
3876 }
3877 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003878 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003879 case Primitive::kPrimLong: {
3880 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003881 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003882 __ movl(temp, value.AsRegisterPairLow<Register>());
3883 __ orl(temp, value.AsRegisterPairHigh<Register>());
3884 __ j(kEqual, slow_path->GetEntryLabel());
3885 } else {
3886 DCHECK(value.IsConstant()) << value;
3887 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3888 __ jmp(slow_path->GetEntryLabel());
3889 }
3890 }
3891 break;
3892 }
3893 default:
3894 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003895 }
Calin Juravled0d48522014-11-04 16:40:20 +00003896}
3897
Calin Juravle9aec02f2014-11-18 23:06:35 +00003898void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3899 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3900
3901 LocationSummary* locations =
3902 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3903
3904 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00003905 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00003906 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003907 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003908 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003909 // The shift count needs to be in CL or a constant.
3910 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003911 locations->SetOut(Location::SameAsFirstInput());
3912 break;
3913 }
3914 default:
3915 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3916 }
3917}
3918
3919void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3920 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3921
3922 LocationSummary* locations = op->GetLocations();
3923 Location first = locations->InAt(0);
3924 Location second = locations->InAt(1);
3925 DCHECK(first.Equals(locations->Out()));
3926
3927 switch (op->GetResultType()) {
3928 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00003929 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003930 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003931 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003932 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003933 DCHECK_EQ(ECX, second_reg);
3934 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003935 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003936 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003937 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003938 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003939 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003940 }
3941 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003942 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003943 if (shift == 0) {
3944 return;
3945 }
3946 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003947 if (op->IsShl()) {
3948 __ shll(first_reg, imm);
3949 } else if (op->IsShr()) {
3950 __ sarl(first_reg, imm);
3951 } else {
3952 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003953 }
3954 }
3955 break;
3956 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003957 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003958 if (second.IsRegister()) {
3959 Register second_reg = second.AsRegister<Register>();
3960 DCHECK_EQ(ECX, second_reg);
3961 if (op->IsShl()) {
3962 GenerateShlLong(first, second_reg);
3963 } else if (op->IsShr()) {
3964 GenerateShrLong(first, second_reg);
3965 } else {
3966 GenerateUShrLong(first, second_reg);
3967 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003968 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003969 // Shift by a constant.
Roland Levillain5b5b9312016-03-22 14:57:31 +00003970 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003971 // Nothing to do if the shift is 0, as the input is already the output.
3972 if (shift != 0) {
3973 if (op->IsShl()) {
3974 GenerateShlLong(first, shift);
3975 } else if (op->IsShr()) {
3976 GenerateShrLong(first, shift);
3977 } else {
3978 GenerateUShrLong(first, shift);
3979 }
3980 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003981 }
3982 break;
3983 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003984 default:
3985 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3986 }
3987}
3988
Mark P Mendell73945692015-04-29 14:56:17 +00003989void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3990 Register low = loc.AsRegisterPairLow<Register>();
3991 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003992 if (shift == 1) {
3993 // This is just an addition.
3994 __ addl(low, low);
3995 __ adcl(high, high);
3996 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003997 // Shift by 32 is easy. High gets low, and low gets 0.
3998 codegen_->EmitParallelMoves(
3999 loc.ToLow(),
4000 loc.ToHigh(),
4001 Primitive::kPrimInt,
4002 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
4003 loc.ToLow(),
4004 Primitive::kPrimInt);
4005 } else if (shift > 32) {
4006 // Low part becomes 0. High part is low part << (shift-32).
4007 __ movl(high, low);
4008 __ shll(high, Immediate(shift - 32));
4009 __ xorl(low, low);
4010 } else {
4011 // Between 1 and 31.
4012 __ shld(high, low, Immediate(shift));
4013 __ shll(low, Immediate(shift));
4014 }
4015}
4016
Calin Juravle9aec02f2014-11-18 23:06:35 +00004017void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004018 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004019 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
4020 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
4021 __ testl(shifter, Immediate(32));
4022 __ j(kEqual, &done);
4023 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
4024 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
4025 __ Bind(&done);
4026}
4027
Mark P Mendell73945692015-04-29 14:56:17 +00004028void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
4029 Register low = loc.AsRegisterPairLow<Register>();
4030 Register high = loc.AsRegisterPairHigh<Register>();
4031 if (shift == 32) {
4032 // Need to copy the sign.
4033 DCHECK_NE(low, high);
4034 __ movl(low, high);
4035 __ sarl(high, Immediate(31));
4036 } else if (shift > 32) {
4037 DCHECK_NE(low, high);
4038 // High part becomes sign. Low part is shifted by shift - 32.
4039 __ movl(low, high);
4040 __ sarl(high, Immediate(31));
4041 __ sarl(low, Immediate(shift - 32));
4042 } else {
4043 // Between 1 and 31.
4044 __ shrd(low, high, Immediate(shift));
4045 __ sarl(high, Immediate(shift));
4046 }
4047}
4048
Calin Juravle9aec02f2014-11-18 23:06:35 +00004049void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004050 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004051 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4052 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
4053 __ testl(shifter, Immediate(32));
4054 __ j(kEqual, &done);
4055 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4056 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
4057 __ Bind(&done);
4058}
4059
Mark P Mendell73945692015-04-29 14:56:17 +00004060void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
4061 Register low = loc.AsRegisterPairLow<Register>();
4062 Register high = loc.AsRegisterPairHigh<Register>();
4063 if (shift == 32) {
4064 // Shift by 32 is easy. Low gets high, and high gets 0.
4065 codegen_->EmitParallelMoves(
4066 loc.ToHigh(),
4067 loc.ToLow(),
4068 Primitive::kPrimInt,
4069 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
4070 loc.ToHigh(),
4071 Primitive::kPrimInt);
4072 } else if (shift > 32) {
4073 // Low part is high >> (shift - 32). High part becomes 0.
4074 __ movl(low, high);
4075 __ shrl(low, Immediate(shift - 32));
4076 __ xorl(high, high);
4077 } else {
4078 // Between 1 and 31.
4079 __ shrd(low, high, Immediate(shift));
4080 __ shrl(high, Immediate(shift));
4081 }
4082}
4083
Calin Juravle9aec02f2014-11-18 23:06:35 +00004084void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004085 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004086 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4087 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
4088 __ testl(shifter, Immediate(32));
4089 __ j(kEqual, &done);
4090 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4091 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
4092 __ Bind(&done);
4093}
4094
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004095void LocationsBuilderX86::VisitRor(HRor* ror) {
4096 LocationSummary* locations =
4097 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
4098
4099 switch (ror->GetResultType()) {
4100 case Primitive::kPrimLong:
4101 // Add the temporary needed.
4102 locations->AddTemp(Location::RequiresRegister());
4103 FALLTHROUGH_INTENDED;
4104 case Primitive::kPrimInt:
4105 locations->SetInAt(0, Location::RequiresRegister());
4106 // The shift count needs to be in CL (unless it is a constant).
4107 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
4108 locations->SetOut(Location::SameAsFirstInput());
4109 break;
4110 default:
4111 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4112 UNREACHABLE();
4113 }
4114}
4115
4116void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
4117 LocationSummary* locations = ror->GetLocations();
4118 Location first = locations->InAt(0);
4119 Location second = locations->InAt(1);
4120
4121 if (ror->GetResultType() == Primitive::kPrimInt) {
4122 Register first_reg = first.AsRegister<Register>();
4123 if (second.IsRegister()) {
4124 Register second_reg = second.AsRegister<Register>();
4125 __ rorl(first_reg, second_reg);
4126 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004127 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004128 __ rorl(first_reg, imm);
4129 }
4130 return;
4131 }
4132
4133 DCHECK_EQ(ror->GetResultType(), Primitive::kPrimLong);
4134 Register first_reg_lo = first.AsRegisterPairLow<Register>();
4135 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
4136 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
4137 if (second.IsRegister()) {
4138 Register second_reg = second.AsRegister<Register>();
4139 DCHECK_EQ(second_reg, ECX);
4140 __ movl(temp_reg, first_reg_hi);
4141 __ shrd(first_reg_hi, first_reg_lo, second_reg);
4142 __ shrd(first_reg_lo, temp_reg, second_reg);
4143 __ movl(temp_reg, first_reg_hi);
4144 __ testl(second_reg, Immediate(32));
4145 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
4146 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
4147 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004148 int32_t shift_amt = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004149 if (shift_amt == 0) {
4150 // Already fine.
4151 return;
4152 }
4153 if (shift_amt == 32) {
4154 // Just swap.
4155 __ movl(temp_reg, first_reg_lo);
4156 __ movl(first_reg_lo, first_reg_hi);
4157 __ movl(first_reg_hi, temp_reg);
4158 return;
4159 }
4160
4161 Immediate imm(shift_amt);
4162 // Save the constents of the low value.
4163 __ movl(temp_reg, first_reg_lo);
4164
4165 // Shift right into low, feeding bits from high.
4166 __ shrd(first_reg_lo, first_reg_hi, imm);
4167
4168 // Shift right into high, feeding bits from the original low.
4169 __ shrd(first_reg_hi, temp_reg, imm);
4170
4171 // Swap if needed.
4172 if (shift_amt > 32) {
4173 __ movl(temp_reg, first_reg_lo);
4174 __ movl(first_reg_lo, first_reg_hi);
4175 __ movl(first_reg_hi, temp_reg);
4176 }
4177 }
4178}
4179
Calin Juravle9aec02f2014-11-18 23:06:35 +00004180void LocationsBuilderX86::VisitShl(HShl* shl) {
4181 HandleShift(shl);
4182}
4183
4184void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
4185 HandleShift(shl);
4186}
4187
4188void LocationsBuilderX86::VisitShr(HShr* shr) {
4189 HandleShift(shr);
4190}
4191
4192void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
4193 HandleShift(shr);
4194}
4195
4196void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
4197 HandleShift(ushr);
4198}
4199
4200void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
4201 HandleShift(ushr);
4202}
4203
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004204void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004205 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004206 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004207 locations->SetOut(Location::RegisterLocation(EAX));
David Brazdil6de19382016-01-08 17:37:10 +00004208 if (instruction->IsStringAlloc()) {
4209 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4210 } else {
4211 InvokeRuntimeCallingConvention calling_convention;
4212 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00004213 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004214}
4215
4216void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004217 // Note: if heap poisoning is enabled, the entry point takes cares
4218 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004219 if (instruction->IsStringAlloc()) {
4220 // String is allocated through StringFactory. Call NewEmptyString entry point.
4221 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07004222 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004223 __ fs()->movl(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString)));
4224 __ call(Address(temp, code_offset.Int32Value()));
4225 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4226 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01004227 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00004228 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00004229 DCHECK(!codegen_->IsLeafMethod());
4230 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004231}
4232
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004233void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
4234 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004235 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004236 locations->SetOut(Location::RegisterLocation(EAX));
4237 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004238 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4239 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004240}
4241
4242void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004243 // Note: if heap poisoning is enabled, the entry point takes cares
4244 // of poisoning the reference.
Nicolas Geoffrayd0958442017-01-30 14:57:16 +00004245 QuickEntrypointEnum entrypoint =
4246 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
4247 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004248 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004249 DCHECK(!codegen_->IsLeafMethod());
4250}
4251
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004252void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004253 LocationSummary* locations =
4254 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004255 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4256 if (location.IsStackSlot()) {
4257 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4258 } else if (location.IsDoubleStackSlot()) {
4259 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004260 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004261 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004262}
4263
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004264void InstructionCodeGeneratorX86::VisitParameterValue(
4265 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4266}
4267
4268void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
4269 LocationSummary* locations =
4270 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4271 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4272}
4273
4274void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004275}
4276
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004277void LocationsBuilderX86::VisitClassTableGet(HClassTableGet* instruction) {
4278 LocationSummary* locations =
4279 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4280 locations->SetInAt(0, Location::RequiresRegister());
4281 locations->SetOut(Location::RequiresRegister());
4282}
4283
4284void InstructionCodeGeneratorX86::VisitClassTableGet(HClassTableGet* instruction) {
4285 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004286 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004287 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004288 instruction->GetIndex(), kX86PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004289 __ movl(locations->Out().AsRegister<Register>(),
4290 Address(locations->InAt(0).AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004291 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004292 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004293 instruction->GetIndex(), kX86PointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004294 __ movl(locations->Out().AsRegister<Register>(),
4295 Address(locations->InAt(0).AsRegister<Register>(),
4296 mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
4297 // temp = temp->GetImtEntryAt(method_offset);
4298 __ movl(locations->Out().AsRegister<Register>(),
4299 Address(locations->Out().AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004300 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004301}
4302
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004303void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004304 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004305 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004306 locations->SetInAt(0, Location::RequiresRegister());
4307 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004308}
4309
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004310void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
4311 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01004312 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004313 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01004314 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004315 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004316 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004317 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004318 break;
4319
4320 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01004321 __ notl(out.AsRegisterPairLow<Register>());
4322 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004323 break;
4324
4325 default:
4326 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4327 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004328}
4329
David Brazdil66d126e2015-04-03 16:02:44 +01004330void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
4331 LocationSummary* locations =
4332 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4333 locations->SetInAt(0, Location::RequiresRegister());
4334 locations->SetOut(Location::SameAsFirstInput());
4335}
4336
4337void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004338 LocationSummary* locations = bool_not->GetLocations();
4339 Location in = locations->InAt(0);
4340 Location out = locations->Out();
4341 DCHECK(in.Equals(out));
4342 __ xorl(out.AsRegister<Register>(), Immediate(1));
4343}
4344
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004345void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004346 LocationSummary* locations =
4347 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004348 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004349 case Primitive::kPrimBoolean:
4350 case Primitive::kPrimByte:
4351 case Primitive::kPrimShort:
4352 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004353 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00004354 case Primitive::kPrimLong: {
4355 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00004356 locations->SetInAt(1, Location::Any());
4357 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4358 break;
4359 }
4360 case Primitive::kPrimFloat:
4361 case Primitive::kPrimDouble: {
4362 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004363 if (compare->InputAt(1)->IsX86LoadFromConstantTable()) {
4364 DCHECK(compare->InputAt(1)->IsEmittedAtUseSite());
4365 } else if (compare->InputAt(1)->IsConstant()) {
4366 locations->SetInAt(1, Location::RequiresFpuRegister());
4367 } else {
4368 locations->SetInAt(1, Location::Any());
4369 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004370 locations->SetOut(Location::RequiresRegister());
4371 break;
4372 }
4373 default:
4374 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4375 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004376}
4377
4378void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004379 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004380 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004381 Location left = locations->InAt(0);
4382 Location right = locations->InAt(1);
4383
Mark Mendell0c9497d2015-08-21 09:30:05 -04004384 NearLabel less, greater, done;
Aart Bika19616e2016-02-01 18:57:58 -08004385 Condition less_cond = kLess;
4386
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004387 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004388 case Primitive::kPrimBoolean:
4389 case Primitive::kPrimByte:
4390 case Primitive::kPrimShort:
4391 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004392 case Primitive::kPrimInt: {
Roland Levillain0b671c02016-08-19 12:02:34 +01004393 codegen_->GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08004394 break;
4395 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004396 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004397 Register left_low = left.AsRegisterPairLow<Register>();
4398 Register left_high = left.AsRegisterPairHigh<Register>();
4399 int32_t val_low = 0;
4400 int32_t val_high = 0;
4401 bool right_is_const = false;
4402
4403 if (right.IsConstant()) {
4404 DCHECK(right.GetConstant()->IsLongConstant());
4405 right_is_const = true;
4406 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
4407 val_low = Low32Bits(val);
4408 val_high = High32Bits(val);
4409 }
4410
Calin Juravleddb7df22014-11-25 20:56:51 +00004411 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004412 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004413 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004414 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004415 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004416 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004417 codegen_->Compare32BitValue(left_high, val_high);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004418 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004419 __ j(kLess, &less); // Signed compare.
4420 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004421 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004422 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004423 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004424 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004425 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004426 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004427 codegen_->Compare32BitValue(left_low, val_low);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004428 }
Aart Bika19616e2016-02-01 18:57:58 -08004429 less_cond = kBelow; // for CF (unsigned).
Calin Juravleddb7df22014-11-25 20:56:51 +00004430 break;
4431 }
4432 case Primitive::kPrimFloat: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004433 GenerateFPCompare(left, right, compare, false);
Calin Juravleddb7df22014-11-25 20:56:51 +00004434 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004435 less_cond = kBelow; // for CF (floats).
Calin Juravleddb7df22014-11-25 20:56:51 +00004436 break;
4437 }
4438 case Primitive::kPrimDouble: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004439 GenerateFPCompare(left, right, compare, true);
Calin Juravleddb7df22014-11-25 20:56:51 +00004440 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004441 less_cond = kBelow; // for CF (floats).
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004442 break;
4443 }
4444 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004445 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004446 }
Aart Bika19616e2016-02-01 18:57:58 -08004447
Calin Juravleddb7df22014-11-25 20:56:51 +00004448 __ movl(out, Immediate(0));
4449 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08004450 __ j(less_cond, &less);
Calin Juravleddb7df22014-11-25 20:56:51 +00004451
4452 __ Bind(&greater);
4453 __ movl(out, Immediate(1));
4454 __ jmp(&done);
4455
4456 __ Bind(&less);
4457 __ movl(out, Immediate(-1));
4458
4459 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004460}
4461
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004462void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004463 LocationSummary* locations =
4464 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004465 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004466 locations->SetInAt(i, Location::Any());
4467 }
4468 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004469}
4470
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004471void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004472 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004473}
4474
Roland Levillain7c1559a2015-12-15 10:55:36 +00004475void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004476 /*
4477 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
4478 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
4479 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4480 */
4481 switch (kind) {
4482 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004483 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004484 break;
4485 }
4486 case MemBarrierKind::kAnyStore:
4487 case MemBarrierKind::kLoadAny:
4488 case MemBarrierKind::kStoreStore: {
4489 // nop
4490 break;
4491 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004492 case MemBarrierKind::kNTStoreStore:
4493 // Non-Temporal Store/Store needs an explicit fence.
4494 MemoryFence(/* non-temporal */ true);
4495 break;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004496 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004497}
4498
Vladimir Markodc151b22015-10-15 18:02:30 +01004499HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
4500 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004501 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004502 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004503}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004504
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004505Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4506 Register temp) {
4507 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Markoc53c0792015-11-19 15:48:33 +00004508 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004509 if (!invoke->GetLocations()->Intrinsified()) {
4510 return location.AsRegister<Register>();
4511 }
4512 // For intrinsics we allow any location, so it may be on the stack.
4513 if (!location.IsRegister()) {
4514 __ movl(temp, Address(ESP, location.GetStackIndex()));
4515 return temp;
4516 }
4517 // For register locations, check if the register was saved. If so, get it from the stack.
4518 // Note: There is a chance that the register was saved but not overwritten, so we could
4519 // save one load. However, since this is just an intrinsic slow path we prefer this
4520 // simple and more robust approach rather that trying to determine if that's the case.
4521 SlowPathCode* slow_path = GetCurrentSlowPath();
Vladimir Marko4ee8e292017-06-02 15:39:30 +00004522 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
4523 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4524 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4525 __ movl(temp, Address(ESP, stack_offset));
4526 return temp;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004527 }
4528 return location.AsRegister<Register>();
4529}
4530
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004531void CodeGeneratorX86::GenerateStaticOrDirectCall(
4532 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Vladimir Marko58155012015-08-19 12:49:41 +00004533 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4534 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004535 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00004536 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004537 uint32_t offset =
4538 GetThreadOffset<kX86PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
4539 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004540 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004541 }
Vladimir Marko58155012015-08-19 12:49:41 +00004542 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004543 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004544 break;
Vladimir Marko65979462017-05-19 17:25:12 +01004545 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
4546 DCHECK(GetCompilerOptions().IsBootImage());
4547 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4548 temp.AsRegister<Register>());
4549 __ leal(temp.AsRegister<Register>(), Address(base_reg, CodeGeneratorX86::kDummy32BitOffset));
4550 RecordBootMethodPatch(invoke);
4551 break;
4552 }
Vladimir Marko58155012015-08-19 12:49:41 +00004553 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4554 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
4555 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004556 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004557 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4558 temp.AsRegister<Register>());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004559 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004560 // Bind a new fixup label at the end of the "movl" insn.
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004561 __ Bind(NewMethodBssEntryPatch(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004562 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress(),
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004563 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex())));
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004564 break;
4565 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004566 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
4567 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
4568 return; // No code pointer retrieval; the runtime performs the call directly.
Vladimir Marko9b688a02015-05-06 14:12:42 +01004569 }
Vladimir Marko58155012015-08-19 12:49:41 +00004570 }
4571
4572 switch (invoke->GetCodePtrLocation()) {
4573 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4574 __ call(GetFrameEntryLabel());
4575 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004576 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4577 // (callee_method + offset_of_quick_compiled_code)()
4578 __ call(Address(callee_method.AsRegister<Register>(),
4579 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07004580 kX86PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004581 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04004582 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004583 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Mark Mendell09ed1a32015-03-25 08:30:06 -04004584
4585 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04004586}
4587
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004588void CodeGeneratorX86::GenerateVirtualCall(
4589 HInvokeVirtual* invoke, Location temp_in, SlowPathCode* slow_path) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004590 Register temp = temp_in.AsRegister<Register>();
4591 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4592 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004593
4594 // Use the calling convention instead of the location of the receiver, as
4595 // intrinsics may have put the receiver in a different register. In the intrinsics
4596 // slow path, the arguments have been moved to the right place, so here we are
4597 // guaranteed that the receiver is the first register of the calling convention.
4598 InvokeDexCallingConvention calling_convention;
4599 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004600 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004601 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004602 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004603 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004604 // Instead of simply (possibly) unpoisoning `temp` here, we should
4605 // emit a read barrier for the previous class reference load.
4606 // However this is not required in practice, as this is an
4607 // intermediate/temporary reference and because the current
4608 // concurrent copying collector keeps the from-space memory
4609 // intact/accessible until the end of the marking phase (the
4610 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004611 __ MaybeUnpoisonHeapReference(temp);
4612 // temp = temp->GetMethodAt(method_offset);
4613 __ movl(temp, Address(temp, method_offset));
4614 // call temp->GetEntryPoint();
4615 __ call(Address(
Andreas Gampe542451c2016-07-26 09:02:02 -07004616 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004617 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004618}
4619
Vladimir Marko65979462017-05-19 17:25:12 +01004620void CodeGeneratorX86::RecordBootMethodPatch(HInvokeStaticOrDirect* invoke) {
4621 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
4622 HX86ComputeBaseMethodAddress* address =
4623 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
4624 boot_image_method_patches_.emplace_back(address,
4625 *invoke->GetTargetMethod().dex_file,
4626 invoke->GetTargetMethod().dex_method_index);
4627 __ Bind(&boot_image_method_patches_.back().label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004628}
4629
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004630Label* CodeGeneratorX86::NewMethodBssEntryPatch(
4631 HX86ComputeBaseMethodAddress* method_address,
4632 MethodReference target_method) {
4633 // Add the patch entry and bind its label at the end of the instruction.
4634 method_bss_entry_patches_.emplace_back(method_address,
4635 *target_method.dex_file,
4636 target_method.dex_method_index);
4637 return &method_bss_entry_patches_.back().label;
4638}
4639
Vladimir Marko1998cd02017-01-13 13:02:58 +00004640void CodeGeneratorX86::RecordBootTypePatch(HLoadClass* load_class) {
Vladimir Marko00916b92017-05-17 17:45:45 +01004641 HX86ComputeBaseMethodAddress* address = load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004642 boot_image_type_patches_.emplace_back(address,
4643 load_class->GetDexFile(),
Vladimir Marko1998cd02017-01-13 13:02:58 +00004644 load_class->GetTypeIndex().index_);
4645 __ Bind(&boot_image_type_patches_.back().label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004646}
4647
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004648Label* CodeGeneratorX86::NewTypeBssEntryPatch(HLoadClass* load_class) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004649 HX86ComputeBaseMethodAddress* address =
4650 load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
4651 type_bss_entry_patches_.emplace_back(
4652 address, load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00004653 return &type_bss_entry_patches_.back().label;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004654}
4655
Vladimir Marko65979462017-05-19 17:25:12 +01004656void CodeGeneratorX86::RecordBootStringPatch(HLoadString* load_string) {
Vladimir Marko65979462017-05-19 17:25:12 +01004657 HX86ComputeBaseMethodAddress* address = load_string->InputAt(0)->AsX86ComputeBaseMethodAddress();
4658 string_patches_.emplace_back(address,
4659 load_string->GetDexFile(),
4660 load_string->GetStringIndex().index_);
4661 __ Bind(&string_patches_.back().label);
4662}
4663
Vladimir Markoaad75c62016-10-03 08:46:48 +00004664Label* CodeGeneratorX86::NewStringBssEntryPatch(HLoadString* load_string) {
4665 DCHECK(!GetCompilerOptions().IsBootImage());
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004666 HX86ComputeBaseMethodAddress* address =
4667 load_string->InputAt(0)->AsX86ComputeBaseMethodAddress();
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004668 string_bss_entry_patches_.emplace_back(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004669 address, load_string->GetDexFile(), load_string->GetStringIndex().index_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004670 return &string_bss_entry_patches_.back().label;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004671}
4672
Vladimir Markoaad75c62016-10-03 08:46:48 +00004673// The label points to the end of the "movl" or another instruction but the literal offset
4674// for method patch needs to point to the embedded constant which occupies the last 4 bytes.
4675constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
4676
4677template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
4678inline void CodeGeneratorX86::EmitPcRelativeLinkerPatches(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004679 const ArenaDeque<X86PcRelativePatchInfo>& infos,
Vladimir Markoaad75c62016-10-03 08:46:48 +00004680 ArenaVector<LinkerPatch>* linker_patches) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004681 for (const X86PcRelativePatchInfo& info : infos) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00004682 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004683 linker_patches->push_back(Factory(
4684 literal_offset, &info.dex_file, GetMethodAddressOffset(info.method_address), info.index));
Vladimir Markoaad75c62016-10-03 08:46:48 +00004685 }
4686}
4687
Vladimir Marko58155012015-08-19 12:49:41 +00004688void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4689 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004690 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01004691 boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004692 method_bss_entry_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +00004693 boot_image_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01004694 type_bss_entry_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004695 string_patches_.size() +
4696 string_bss_entry_patches_.size();
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004697 linker_patches->reserve(size);
Vladimir Marko764d4542017-05-16 10:31:41 +01004698 if (GetCompilerOptions().IsBootImage()) {
Vladimir Marko65979462017-05-19 17:25:12 +01004699 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeMethodPatch>(boot_image_method_patches_,
4700 linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00004701 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(boot_image_type_patches_,
4702 linker_patches);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004703 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(string_patches_, linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004704 } else {
Vladimir Marko65979462017-05-19 17:25:12 +01004705 DCHECK(boot_image_method_patches_.empty());
Vladimir Marko94ec2db2017-09-06 17:21:03 +01004706 EmitPcRelativeLinkerPatches<LinkerPatch::TypeClassTablePatch>(boot_image_type_patches_,
4707 linker_patches);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004708 EmitPcRelativeLinkerPatches<LinkerPatch::StringInternTablePatch>(string_patches_,
4709 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004710 }
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004711 EmitPcRelativeLinkerPatches<LinkerPatch::MethodBssEntryPatch>(method_bss_entry_patches_,
4712 linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00004713 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
4714 linker_patches);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004715 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(string_bss_entry_patches_,
4716 linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00004717 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00004718}
4719
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004720void CodeGeneratorX86::MarkGCCard(Register temp,
4721 Register card,
4722 Register object,
4723 Register value,
4724 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004725 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004726 if (value_can_be_null) {
4727 __ testl(value, value);
4728 __ j(kEqual, &is_null);
4729 }
Andreas Gampe542451c2016-07-26 09:02:02 -07004730 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86PointerSize>().Int32Value()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004731 __ movl(temp, object);
4732 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004733 __ movb(Address(temp, card, TIMES_1, 0),
4734 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004735 if (value_can_be_null) {
4736 __ Bind(&is_null);
4737 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004738}
4739
Calin Juravle52c48962014-12-16 17:02:57 +00004740void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4741 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00004742
4743 bool object_field_get_with_read_barrier =
4744 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004745 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004746 new (GetGraph()->GetArena()) LocationSummary(instruction,
4747 kEmitCompilerReadBarrier ?
4748 LocationSummary::kCallOnSlowPath :
4749 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004750 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004751 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004752 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004753 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004754
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004755 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4756 locations->SetOut(Location::RequiresFpuRegister());
4757 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004758 // The output overlaps in case of long: we don't want the low move
4759 // to overwrite the object's location. Likewise, in the case of
4760 // an object field get with read barriers enabled, we do not want
4761 // the move to overwrite the object's location, as we need it to emit
4762 // the read barrier.
4763 locations->SetOut(
4764 Location::RequiresRegister(),
4765 (object_field_get_with_read_barrier || instruction->GetType() == Primitive::kPrimLong) ?
4766 Location::kOutputOverlap :
4767 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004768 }
Calin Juravle52c48962014-12-16 17:02:57 +00004769
4770 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
4771 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00004772 // So we use an XMM register as a temp to achieve atomicity (first
4773 // load the temp into the XMM and then copy the XMM into the
4774 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00004775 locations->AddTemp(Location::RequiresFpuRegister());
4776 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004777}
4778
Calin Juravle52c48962014-12-16 17:02:57 +00004779void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
4780 const FieldInfo& field_info) {
4781 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004782
Calin Juravle52c48962014-12-16 17:02:57 +00004783 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004784 Location base_loc = locations->InAt(0);
4785 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004786 Location out = locations->Out();
4787 bool is_volatile = field_info.IsVolatile();
4788 Primitive::Type field_type = field_info.GetFieldType();
4789 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4790
4791 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004792 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00004793 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004794 break;
4795 }
4796
4797 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00004798 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004799 break;
4800 }
4801
4802 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00004803 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004804 break;
4805 }
4806
4807 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00004808 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004809 break;
4810 }
4811
4812 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004813 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004814 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00004815
4816 case Primitive::kPrimNot: {
4817 // /* HeapReference<Object> */ out = *(base + offset)
4818 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004819 // Note that a potential implicit null check is handled in this
4820 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4821 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00004822 instruction, out, base, offset, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00004823 if (is_volatile) {
4824 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4825 }
4826 } else {
4827 __ movl(out.AsRegister<Register>(), Address(base, offset));
4828 codegen_->MaybeRecordImplicitNullCheck(instruction);
4829 if (is_volatile) {
4830 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4831 }
4832 // If read barriers are enabled, emit read barriers other than
4833 // Baker's using a slow path (and also unpoison the loaded
4834 // reference, if heap poisoning is enabled).
4835 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4836 }
4837 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004838 }
4839
4840 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00004841 if (is_volatile) {
4842 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4843 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004844 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004845 __ movd(out.AsRegisterPairLow<Register>(), temp);
4846 __ psrlq(temp, Immediate(32));
4847 __ movd(out.AsRegisterPairHigh<Register>(), temp);
4848 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004849 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00004850 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004851 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004852 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
4853 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004854 break;
4855 }
4856
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004857 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004858 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004859 break;
4860 }
4861
4862 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004863 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004864 break;
4865 }
4866
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004867 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004868 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004869 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004870 }
Calin Juravle52c48962014-12-16 17:02:57 +00004871
Roland Levillain7c1559a2015-12-15 10:55:36 +00004872 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimLong) {
4873 // Potential implicit null checks, in the case of reference or
4874 // long fields, are handled in the previous switch statement.
4875 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004876 codegen_->MaybeRecordImplicitNullCheck(instruction);
4877 }
4878
Calin Juravle52c48962014-12-16 17:02:57 +00004879 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004880 if (field_type == Primitive::kPrimNot) {
4881 // Memory barriers, in the case of references, are also handled
4882 // in the previous switch statement.
4883 } else {
4884 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4885 }
Roland Levillain4d027112015-07-01 15:41:14 +01004886 }
Calin Juravle52c48962014-12-16 17:02:57 +00004887}
4888
4889void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4890 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4891
4892 LocationSummary* locations =
4893 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4894 locations->SetInAt(0, Location::RequiresRegister());
4895 bool is_volatile = field_info.IsVolatile();
4896 Primitive::Type field_type = field_info.GetFieldType();
4897 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
4898 || (field_type == Primitive::kPrimByte);
4899
4900 // The register allocator does not support multiple
4901 // inputs that die at entry with one in a specific register.
4902 if (is_byte_type) {
4903 // Ensure the value is in a byte register.
4904 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004905 } else if (Primitive::IsFloatingPointType(field_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05004906 if (is_volatile && field_type == Primitive::kPrimDouble) {
4907 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4908 locations->SetInAt(1, Location::RequiresFpuRegister());
4909 } else {
4910 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4911 }
4912 } else if (is_volatile && field_type == Primitive::kPrimLong) {
4913 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00004914 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004915
Calin Juravle52c48962014-12-16 17:02:57 +00004916 // 64bits value can be atomically written to an address with movsd and an XMM register.
4917 // We need two XMM registers because there's no easier way to (bit) copy a register pair
4918 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
4919 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
4920 // isolated cases when we need this it isn't worth adding the extra complexity.
4921 locations->AddTemp(Location::RequiresFpuRegister());
4922 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004923 } else {
4924 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4925
4926 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4927 // Temporary registers for the write barrier.
4928 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
4929 // Ensure the card is in a byte register.
4930 locations->AddTemp(Location::RegisterLocation(ECX));
4931 }
Calin Juravle52c48962014-12-16 17:02:57 +00004932 }
4933}
4934
4935void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004936 const FieldInfo& field_info,
4937 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004938 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4939
4940 LocationSummary* locations = instruction->GetLocations();
4941 Register base = locations->InAt(0).AsRegister<Register>();
4942 Location value = locations->InAt(1);
4943 bool is_volatile = field_info.IsVolatile();
4944 Primitive::Type field_type = field_info.GetFieldType();
4945 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004946 bool needs_write_barrier =
4947 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004948
4949 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004950 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004951 }
4952
Mark Mendell81489372015-11-04 11:30:41 -05004953 bool maybe_record_implicit_null_check_done = false;
4954
Calin Juravle52c48962014-12-16 17:02:57 +00004955 switch (field_type) {
4956 case Primitive::kPrimBoolean:
4957 case Primitive::kPrimByte: {
4958 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
4959 break;
4960 }
4961
4962 case Primitive::kPrimShort:
4963 case Primitive::kPrimChar: {
Mark Mendell81489372015-11-04 11:30:41 -05004964 if (value.IsConstant()) {
Nicolas Geoffray78612082017-07-24 14:18:53 +01004965 __ movw(Address(base, offset),
4966 Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Mark Mendell81489372015-11-04 11:30:41 -05004967 } else {
4968 __ movw(Address(base, offset), value.AsRegister<Register>());
4969 }
Calin Juravle52c48962014-12-16 17:02:57 +00004970 break;
4971 }
4972
4973 case Primitive::kPrimInt:
4974 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01004975 if (kPoisonHeapReferences && needs_write_barrier) {
4976 // Note that in the case where `value` is a null reference,
4977 // we do not enter this block, as the reference does not
4978 // need poisoning.
4979 DCHECK_EQ(field_type, Primitive::kPrimNot);
4980 Register temp = locations->GetTemp(0).AsRegister<Register>();
4981 __ movl(temp, value.AsRegister<Register>());
4982 __ PoisonHeapReference(temp);
4983 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05004984 } else if (value.IsConstant()) {
4985 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4986 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01004987 } else {
Nicolas Geoffray03971632016-03-17 10:44:24 +00004988 DCHECK(value.IsRegister()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01004989 __ movl(Address(base, offset), value.AsRegister<Register>());
4990 }
Calin Juravle52c48962014-12-16 17:02:57 +00004991 break;
4992 }
4993
4994 case Primitive::kPrimLong: {
4995 if (is_volatile) {
4996 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4997 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
4998 __ movd(temp1, value.AsRegisterPairLow<Register>());
4999 __ movd(temp2, value.AsRegisterPairHigh<Register>());
5000 __ punpckldq(temp1, temp2);
5001 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00005002 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05005003 } else if (value.IsConstant()) {
5004 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
5005 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
5006 codegen_->MaybeRecordImplicitNullCheck(instruction);
5007 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00005008 } else {
5009 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005010 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005011 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
5012 }
Mark Mendell81489372015-11-04 11:30:41 -05005013 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00005014 break;
5015 }
5016
5017 case Primitive::kPrimFloat: {
Mark Mendell81489372015-11-04 11:30:41 -05005018 if (value.IsConstant()) {
5019 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5020 __ movl(Address(base, offset), Immediate(v));
5021 } else {
5022 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
5023 }
Calin Juravle52c48962014-12-16 17:02:57 +00005024 break;
5025 }
5026
5027 case Primitive::kPrimDouble: {
Mark Mendell81489372015-11-04 11:30:41 -05005028 if (value.IsConstant()) {
5029 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
5030 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
5031 codegen_->MaybeRecordImplicitNullCheck(instruction);
5032 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
5033 maybe_record_implicit_null_check_done = true;
5034 } else {
5035 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
5036 }
Calin Juravle52c48962014-12-16 17:02:57 +00005037 break;
5038 }
5039
5040 case Primitive::kPrimVoid:
5041 LOG(FATAL) << "Unreachable type " << field_type;
5042 UNREACHABLE();
5043 }
5044
Mark Mendell81489372015-11-04 11:30:41 -05005045 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005046 codegen_->MaybeRecordImplicitNullCheck(instruction);
5047 }
5048
Roland Levillain4d027112015-07-01 15:41:14 +01005049 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005050 Register temp = locations->GetTemp(0).AsRegister<Register>();
5051 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005052 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00005053 }
5054
Calin Juravle52c48962014-12-16 17:02:57 +00005055 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005056 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00005057 }
5058}
5059
5060void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5061 HandleFieldGet(instruction, instruction->GetFieldInfo());
5062}
5063
5064void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5065 HandleFieldGet(instruction, instruction->GetFieldInfo());
5066}
5067
5068void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5069 HandleFieldSet(instruction, instruction->GetFieldInfo());
5070}
5071
5072void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005073 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005074}
5075
5076void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5077 HandleFieldSet(instruction, instruction->GetFieldInfo());
5078}
5079
5080void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005081 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005082}
5083
5084void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5085 HandleFieldGet(instruction, instruction->GetFieldInfo());
5086}
5087
5088void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5089 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005090}
5091
Calin Juravlee460d1d2015-09-29 04:52:17 +01005092void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
5093 HUnresolvedInstanceFieldGet* instruction) {
5094 FieldAccessCallingConventionX86 calling_convention;
5095 codegen_->CreateUnresolvedFieldLocationSummary(
5096 instruction, instruction->GetFieldType(), calling_convention);
5097}
5098
5099void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
5100 HUnresolvedInstanceFieldGet* instruction) {
5101 FieldAccessCallingConventionX86 calling_convention;
5102 codegen_->GenerateUnresolvedFieldAccess(instruction,
5103 instruction->GetFieldType(),
5104 instruction->GetFieldIndex(),
5105 instruction->GetDexPc(),
5106 calling_convention);
5107}
5108
5109void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
5110 HUnresolvedInstanceFieldSet* instruction) {
5111 FieldAccessCallingConventionX86 calling_convention;
5112 codegen_->CreateUnresolvedFieldLocationSummary(
5113 instruction, instruction->GetFieldType(), calling_convention);
5114}
5115
5116void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
5117 HUnresolvedInstanceFieldSet* instruction) {
5118 FieldAccessCallingConventionX86 calling_convention;
5119 codegen_->GenerateUnresolvedFieldAccess(instruction,
5120 instruction->GetFieldType(),
5121 instruction->GetFieldIndex(),
5122 instruction->GetDexPc(),
5123 calling_convention);
5124}
5125
5126void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
5127 HUnresolvedStaticFieldGet* instruction) {
5128 FieldAccessCallingConventionX86 calling_convention;
5129 codegen_->CreateUnresolvedFieldLocationSummary(
5130 instruction, instruction->GetFieldType(), calling_convention);
5131}
5132
5133void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
5134 HUnresolvedStaticFieldGet* instruction) {
5135 FieldAccessCallingConventionX86 calling_convention;
5136 codegen_->GenerateUnresolvedFieldAccess(instruction,
5137 instruction->GetFieldType(),
5138 instruction->GetFieldIndex(),
5139 instruction->GetDexPc(),
5140 calling_convention);
5141}
5142
5143void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
5144 HUnresolvedStaticFieldSet* instruction) {
5145 FieldAccessCallingConventionX86 calling_convention;
5146 codegen_->CreateUnresolvedFieldLocationSummary(
5147 instruction, instruction->GetFieldType(), calling_convention);
5148}
5149
5150void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
5151 HUnresolvedStaticFieldSet* instruction) {
5152 FieldAccessCallingConventionX86 calling_convention;
5153 codegen_->GenerateUnresolvedFieldAccess(instruction,
5154 instruction->GetFieldType(),
5155 instruction->GetFieldIndex(),
5156 instruction->GetDexPc(),
5157 calling_convention);
5158}
5159
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005160void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005161 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5162 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
5163 ? Location::RequiresRegister()
5164 : Location::Any();
5165 locations->SetInAt(0, loc);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005166}
5167
Calin Juravle2ae48182016-03-16 14:05:09 +00005168void CodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
5169 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005170 return;
5171 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005172 LocationSummary* locations = instruction->GetLocations();
5173 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005174
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005175 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00005176 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005177}
5178
Calin Juravle2ae48182016-03-16 14:05:09 +00005179void CodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07005180 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005181 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005182
5183 LocationSummary* locations = instruction->GetLocations();
5184 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005185
5186 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04005187 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005188 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005189 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005190 } else {
5191 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00005192 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005193 __ jmp(slow_path->GetEntryLabel());
5194 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005195 }
5196 __ j(kEqual, slow_path->GetEntryLabel());
5197}
5198
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005199void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005200 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005201}
5202
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005203void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005204 bool object_array_get_with_read_barrier =
5205 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005206 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00005207 new (GetGraph()->GetArena()) LocationSummary(instruction,
5208 object_array_get_with_read_barrier ?
5209 LocationSummary::kCallOnSlowPath :
5210 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005211 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005212 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005213 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005214 locations->SetInAt(0, Location::RequiresRegister());
5215 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005216 if (Primitive::IsFloatingPointType(instruction->GetType())) {
5217 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5218 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005219 // The output overlaps in case of long: we don't want the low move
5220 // to overwrite the array's location. Likewise, in the case of an
5221 // object array get with read barriers enabled, we do not want the
5222 // move to overwrite the array's location, as we need it to emit
5223 // the read barrier.
5224 locations->SetOut(
5225 Location::RequiresRegister(),
5226 (instruction->GetType() == Primitive::kPrimLong || object_array_get_with_read_barrier) ?
5227 Location::kOutputOverlap :
5228 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005229 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005230}
5231
5232void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
5233 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005234 Location obj_loc = locations->InAt(0);
5235 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005236 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005237 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005238 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005239
Calin Juravle77520bc2015-01-12 18:45:46 +00005240 Primitive::Type type = instruction->GetType();
5241 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005242 case Primitive::kPrimBoolean: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005243 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005244 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005245 break;
5246 }
5247
5248 case Primitive::kPrimByte: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005249 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005250 __ movsxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005251 break;
5252 }
5253
5254 case Primitive::kPrimShort: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005255 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005256 __ movsxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005257 break;
5258 }
5259
5260 case Primitive::kPrimChar: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005261 Register out = out_loc.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07005262 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5263 // Branch cases into compressed and uncompressed for each index's type.
5264 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
5265 NearLabel done, not_compressed;
Vladimir Marko3c89d422017-02-17 11:30:23 +00005266 __ testb(Address(obj, count_offset), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005267 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005268 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
5269 "Expecting 0=compressed, 1=uncompressed");
5270 __ j(kNotZero, &not_compressed);
jessicahandojo4877b792016-09-08 19:49:13 -07005271 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
5272 __ jmp(&done);
5273 __ Bind(&not_compressed);
5274 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5275 __ Bind(&done);
5276 } else {
5277 // Common case for charAt of array of char or when string compression's
5278 // feature is turned off.
5279 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5280 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005281 break;
5282 }
5283
Roland Levillain7c1559a2015-12-15 10:55:36 +00005284 case Primitive::kPrimInt: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005285 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005286 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005287 break;
5288 }
5289
Roland Levillain7c1559a2015-12-15 10:55:36 +00005290 case Primitive::kPrimNot: {
5291 static_assert(
5292 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5293 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00005294 // /* HeapReference<Object> */ out =
5295 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5296 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005297 // Note that a potential implicit null check is handled in this
5298 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
5299 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00005300 instruction, out_loc, obj, data_offset, index, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005301 } else {
5302 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005303 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
5304 codegen_->MaybeRecordImplicitNullCheck(instruction);
5305 // If read barriers are enabled, emit read barriers other than
5306 // Baker's using a slow path (and also unpoison the loaded
5307 // reference, if heap poisoning is enabled).
Roland Levillain7c1559a2015-12-15 10:55:36 +00005308 if (index.IsConstant()) {
5309 uint32_t offset =
5310 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005311 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5312 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005313 codegen_->MaybeGenerateReadBarrierSlow(
5314 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5315 }
5316 }
5317 break;
5318 }
5319
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005320 case Primitive::kPrimLong: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005321 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005322 __ movl(out_loc.AsRegisterPairLow<Register>(),
5323 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
5324 codegen_->MaybeRecordImplicitNullCheck(instruction);
5325 __ movl(out_loc.AsRegisterPairHigh<Register>(),
5326 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005327 break;
5328 }
5329
Mark Mendell7c8d0092015-01-26 11:21:33 -05005330 case Primitive::kPrimFloat: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005331 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005332 __ movss(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005333 break;
5334 }
5335
5336 case Primitive::kPrimDouble: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005337 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005338 __ movsd(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005339 break;
5340 }
5341
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005342 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00005343 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005344 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005345 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005346
Roland Levillain7c1559a2015-12-15 10:55:36 +00005347 if (type == Primitive::kPrimNot || type == Primitive::kPrimLong) {
5348 // Potential implicit null checks, in the case of reference or
5349 // long arrays, are handled in the previous switch statement.
5350 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005351 codegen_->MaybeRecordImplicitNullCheck(instruction);
5352 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005353}
5354
5355void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005356 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005357
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005358 bool needs_write_barrier =
5359 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005360 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005361
Nicolas Geoffray39468442014-09-02 15:17:15 +01005362 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
5363 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01005364 may_need_runtime_call_for_type_check ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00005365 LocationSummary::kCallOnSlowPath :
5366 LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005367
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005368 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
5369 || (value_type == Primitive::kPrimByte);
5370 // We need the inputs to be different than the output in case of long operation.
5371 // In case of a byte operation, the register allocator does not support multiple
5372 // inputs that die at entry with one in a specific register.
5373 locations->SetInAt(0, Location::RequiresRegister());
5374 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5375 if (is_byte_type) {
5376 // Ensure the value is in a byte register.
5377 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
5378 } else if (Primitive::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05005379 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005380 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005381 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
5382 }
5383 if (needs_write_barrier) {
5384 // Temporary registers for the write barrier.
5385 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5386 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005387 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005388 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005389}
5390
5391void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
5392 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005393 Location array_loc = locations->InAt(0);
5394 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005395 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005396 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005397 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005398 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5399 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5400 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005401 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005402 bool needs_write_barrier =
5403 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005404
5405 switch (value_type) {
5406 case Primitive::kPrimBoolean:
5407 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005408 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005409 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_1, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005410 if (value.IsRegister()) {
5411 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005412 } else {
Nicolas Geoffray78612082017-07-24 14:18:53 +01005413 __ movb(address, Immediate(CodeGenerator::GetInt8ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005414 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005415 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005416 break;
5417 }
5418
5419 case Primitive::kPrimShort:
5420 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005421 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005422 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_2, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005423 if (value.IsRegister()) {
5424 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005425 } else {
Nicolas Geoffray78612082017-07-24 14:18:53 +01005426 __ movw(address, Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005427 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005428 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005429 break;
5430 }
5431
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005432 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005433 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005434 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005435
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005436 if (!value.IsRegister()) {
5437 // Just setting null.
5438 DCHECK(instruction->InputAt(2)->IsNullConstant());
5439 DCHECK(value.IsConstant()) << value;
5440 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00005441 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005442 DCHECK(!needs_write_barrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005443 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005444 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005445 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005446
5447 DCHECK(needs_write_barrier);
5448 Register register_value = value.AsRegister<Register>();
Roland Levillain16d9f942016-08-25 17:27:56 +01005449 // We cannot use a NearLabel for `done`, as its range may be too
5450 // short when Baker read barriers are enabled.
5451 Label done;
5452 NearLabel not_null, do_put;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005453 SlowPathCode* slow_path = nullptr;
Roland Levillain16d9f942016-08-25 17:27:56 +01005454 Location temp_loc = locations->GetTemp(0);
5455 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005456 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005457 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86(instruction);
5458 codegen_->AddSlowPath(slow_path);
5459 if (instruction->GetValueCanBeNull()) {
5460 __ testl(register_value, register_value);
5461 __ j(kNotEqual, &not_null);
5462 __ movl(address, Immediate(0));
5463 codegen_->MaybeRecordImplicitNullCheck(instruction);
5464 __ jmp(&done);
5465 __ Bind(&not_null);
5466 }
5467
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005468 // Note that when Baker read barriers are enabled, the type
5469 // checks are performed without read barriers. This is fine,
5470 // even in the case where a class object is in the from-space
5471 // after the flip, as a comparison involving such a type would
5472 // not produce a false positive; it may of course produce a
5473 // false negative, in which case we would take the ArraySet
5474 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01005475
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005476 // /* HeapReference<Class> */ temp = array->klass_
5477 __ movl(temp, Address(array, class_offset));
5478 codegen_->MaybeRecordImplicitNullCheck(instruction);
5479 __ MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01005480
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005481 // /* HeapReference<Class> */ temp = temp->component_type_
5482 __ movl(temp, Address(temp, component_offset));
5483 // If heap poisoning is enabled, no need to unpoison `temp`
5484 // nor the object reference in `register_value->klass`, as
5485 // we are comparing two poisoned references.
5486 __ cmpl(temp, Address(register_value, class_offset));
Roland Levillain16d9f942016-08-25 17:27:56 +01005487
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005488 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5489 __ j(kEqual, &do_put);
5490 // If heap poisoning is enabled, the `temp` reference has
5491 // not been unpoisoned yet; unpoison it now.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005492 __ MaybeUnpoisonHeapReference(temp);
5493
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005494 // If heap poisoning is enabled, no need to unpoison the
5495 // heap reference loaded below, as it is only used for a
5496 // comparison with null.
5497 __ cmpl(Address(temp, super_offset), Immediate(0));
5498 __ j(kNotEqual, slow_path->GetEntryLabel());
5499 __ Bind(&do_put);
5500 } else {
5501 __ j(kNotEqual, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005502 }
5503 }
5504
5505 if (kPoisonHeapReferences) {
5506 __ movl(temp, register_value);
5507 __ PoisonHeapReference(temp);
5508 __ movl(address, temp);
5509 } else {
5510 __ movl(address, register_value);
5511 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005512 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005513 codegen_->MaybeRecordImplicitNullCheck(instruction);
5514 }
5515
5516 Register card = locations->GetTemp(1).AsRegister<Register>();
5517 codegen_->MarkGCCard(
5518 temp, card, array, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
5519 __ Bind(&done);
5520
5521 if (slow_path != nullptr) {
5522 __ Bind(slow_path->GetExitLabel());
5523 }
5524
5525 break;
5526 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005527
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005528 case Primitive::kPrimInt: {
5529 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005530 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005531 if (value.IsRegister()) {
5532 __ movl(address, value.AsRegister<Register>());
5533 } else {
5534 DCHECK(value.IsConstant()) << value;
5535 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5536 __ movl(address, Immediate(v));
5537 }
5538 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005539 break;
5540 }
5541
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005542 case Primitive::kPrimLong: {
5543 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005544 if (value.IsRegisterPair()) {
5545 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
5546 value.AsRegisterPairLow<Register>());
5547 codegen_->MaybeRecordImplicitNullCheck(instruction);
5548 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
5549 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005550 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005551 DCHECK(value.IsConstant());
5552 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
5553 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
5554 Immediate(Low32Bits(val)));
5555 codegen_->MaybeRecordImplicitNullCheck(instruction);
5556 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
5557 Immediate(High32Bits(val)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005558 }
5559 break;
5560 }
5561
Mark Mendell7c8d0092015-01-26 11:21:33 -05005562 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005563 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005564 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005565 if (value.IsFpuRegister()) {
5566 __ movss(address, value.AsFpuRegister<XmmRegister>());
5567 } else {
5568 DCHECK(value.IsConstant());
5569 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
5570 __ movl(address, Immediate(v));
5571 }
5572 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005573 break;
5574 }
5575
5576 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005577 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005578 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005579 if (value.IsFpuRegister()) {
5580 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5581 } else {
5582 DCHECK(value.IsConstant());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005583 Address address_hi =
5584 CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset + kX86WordSize);
Mark Mendell81489372015-11-04 11:30:41 -05005585 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5586 __ movl(address, Immediate(Low32Bits(v)));
5587 codegen_->MaybeRecordImplicitNullCheck(instruction);
5588 __ movl(address_hi, Immediate(High32Bits(v)));
5589 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005590 break;
5591 }
5592
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005593 case Primitive::kPrimVoid:
5594 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005595 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005596 }
5597}
5598
5599void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
5600 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005601 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04005602 if (!instruction->IsEmittedAtUseSite()) {
5603 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5604 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005605}
5606
5607void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04005608 if (instruction->IsEmittedAtUseSite()) {
5609 return;
5610 }
5611
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005612 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005613 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005614 Register obj = locations->InAt(0).AsRegister<Register>();
5615 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005616 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005617 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo4877b792016-09-08 19:49:13 -07005618 // Mask out most significant bit in case the array is String's array of char.
5619 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005620 __ shrl(out, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005621 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005622}
5623
5624void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005625 RegisterSet caller_saves = RegisterSet::Empty();
5626 InvokeRuntimeCallingConvention calling_convention;
5627 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5628 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5629 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005630 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04005631 HInstruction* length = instruction->InputAt(1);
5632 if (!length->IsEmittedAtUseSite()) {
5633 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5634 }
jessicahandojo4877b792016-09-08 19:49:13 -07005635 // Need register to see array's length.
5636 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5637 locations->AddTemp(Location::RequiresRegister());
5638 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005639}
5640
5641void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
jessicahandojo4877b792016-09-08 19:49:13 -07005642 const bool is_string_compressed_char_at =
5643 mirror::kUseStringCompression && instruction->IsStringCharAt();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005644 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005645 Location index_loc = locations->InAt(0);
5646 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07005647 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005648 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005649
Mark Mendell99dbd682015-04-22 16:18:52 -04005650 if (length_loc.IsConstant()) {
5651 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5652 if (index_loc.IsConstant()) {
5653 // BCE will remove the bounds check if we are guarenteed to pass.
5654 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5655 if (index < 0 || index >= length) {
5656 codegen_->AddSlowPath(slow_path);
5657 __ jmp(slow_path->GetEntryLabel());
5658 } else {
5659 // Some optimization after BCE may have generated this, and we should not
5660 // generate a bounds check if it is a valid range.
5661 }
5662 return;
5663 }
5664
5665 // We have to reverse the jump condition because the length is the constant.
5666 Register index_reg = index_loc.AsRegister<Register>();
5667 __ cmpl(index_reg, Immediate(length));
5668 codegen_->AddSlowPath(slow_path);
5669 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005670 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04005671 HInstruction* array_length = instruction->InputAt(1);
5672 if (array_length->IsEmittedAtUseSite()) {
5673 // Address the length field in the array.
5674 DCHECK(array_length->IsArrayLength());
5675 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
5676 Location array_loc = array_length->GetLocations()->InAt(0);
5677 Address array_len(array_loc.AsRegister<Register>(), len_offset);
jessicahandojo4877b792016-09-08 19:49:13 -07005678 if (is_string_compressed_char_at) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005679 // TODO: if index_loc.IsConstant(), compare twice the index (to compensate for
5680 // the string compression flag) with the in-memory length and avoid the temporary.
jessicahandojo4877b792016-09-08 19:49:13 -07005681 Register length_reg = locations->GetTemp(0).AsRegister<Register>();
5682 __ movl(length_reg, array_len);
5683 codegen_->MaybeRecordImplicitNullCheck(array_length);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005684 __ shrl(length_reg, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005685 codegen_->GenerateIntCompare(length_reg, index_loc);
Mark Mendellee8d9712016-07-12 11:13:15 -04005686 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07005687 // Checking bounds for general case:
5688 // Array of char or string's array with feature compression off.
5689 if (index_loc.IsConstant()) {
5690 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5691 __ cmpl(array_len, Immediate(value));
5692 } else {
5693 __ cmpl(array_len, index_loc.AsRegister<Register>());
5694 }
5695 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendellee8d9712016-07-12 11:13:15 -04005696 }
Mark Mendell99dbd682015-04-22 16:18:52 -04005697 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005698 codegen_->GenerateIntCompare(length_loc, index_loc);
Mark Mendell99dbd682015-04-22 16:18:52 -04005699 }
5700 codegen_->AddSlowPath(slow_path);
5701 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005702 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005703}
5704
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005705void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005706 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005707}
5708
5709void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005710 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5711}
5712
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005713void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005714 LocationSummary* locations =
5715 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Aart Bikb13c65b2017-03-21 20:14:07 -07005716 // In suspend check slow path, usually there are no caller-save registers at all.
5717 // If SIMD instructions are present, however, we force spilling all live SIMD
5718 // registers in full width (since the runtime only saves/restores lower part).
Aart Bik5576f372017-03-23 16:17:37 -07005719 locations->SetCustomSlowPathCallerSaves(
5720 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005721}
5722
5723void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005724 HBasicBlock* block = instruction->GetBlock();
5725 if (block->GetLoopInformation() != nullptr) {
5726 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5727 // The back edge will generate the suspend check.
5728 return;
5729 }
5730 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5731 // The goto will generate the suspend check.
5732 return;
5733 }
5734 GenerateSuspendCheck(instruction, nullptr);
5735}
5736
5737void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
5738 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005739 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005740 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
5741 if (slow_path == nullptr) {
5742 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
5743 instruction->SetSlowPath(slow_path);
5744 codegen_->AddSlowPath(slow_path);
5745 if (successor != nullptr) {
5746 DCHECK(successor->IsLoopHeader());
5747 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5748 }
5749 } else {
5750 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5751 }
5752
Andreas Gampe542451c2016-07-26 09:02:02 -07005753 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86PointerSize>().Int32Value()),
Roland Levillain7c1559a2015-12-15 10:55:36 +00005754 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005755 if (successor == nullptr) {
5756 __ j(kNotEqual, slow_path->GetEntryLabel());
5757 __ Bind(slow_path->GetReturnLabel());
5758 } else {
5759 __ j(kEqual, codegen_->GetLabelOf(successor));
5760 __ jmp(slow_path->GetEntryLabel());
5761 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005762}
5763
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005764X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
5765 return codegen_->GetAssembler();
5766}
5767
Mark Mendell7c8d0092015-01-26 11:21:33 -05005768void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005769 ScratchRegisterScope ensure_scratch(
5770 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5771 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5772 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5773 __ movl(temp_reg, Address(ESP, src + stack_offset));
5774 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005775}
5776
5777void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005778 ScratchRegisterScope ensure_scratch(
5779 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5780 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5781 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5782 __ movl(temp_reg, Address(ESP, src + stack_offset));
5783 __ movl(Address(ESP, dst + stack_offset), temp_reg);
5784 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
5785 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005786}
5787
5788void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005789 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005790 Location source = move->GetSource();
5791 Location destination = move->GetDestination();
5792
5793 if (source.IsRegister()) {
5794 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005795 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005796 } else if (destination.IsFpuRegister()) {
5797 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005798 } else {
5799 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005800 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005801 }
David Brazdil74eb1b22015-12-14 11:44:01 +00005802 } else if (source.IsRegisterPair()) {
5803 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
5804 // Create stack space for 2 elements.
5805 __ subl(ESP, Immediate(2 * elem_size));
5806 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
5807 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
5808 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
5809 // And remove the temporary stack space we allocated.
5810 __ addl(ESP, Immediate(2 * elem_size));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005811 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005812 if (destination.IsRegister()) {
5813 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
5814 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005815 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005816 } else if (destination.IsRegisterPair()) {
5817 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
5818 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
5819 __ psrlq(src_reg, Immediate(32));
5820 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005821 } else if (destination.IsStackSlot()) {
5822 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07005823 } else if (destination.IsDoubleStackSlot()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005824 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07005825 } else {
5826 DCHECK(destination.IsSIMDStackSlot());
5827 __ movups(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005828 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005829 } else if (source.IsStackSlot()) {
5830 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005831 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005832 } else if (destination.IsFpuRegister()) {
5833 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005834 } else {
5835 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005836 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
5837 }
5838 } else if (source.IsDoubleStackSlot()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005839 if (destination.IsRegisterPair()) {
5840 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
5841 __ movl(destination.AsRegisterPairHigh<Register>(),
5842 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
5843 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005844 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
5845 } else {
5846 DCHECK(destination.IsDoubleStackSlot()) << destination;
5847 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005848 }
Aart Bik5576f372017-03-23 16:17:37 -07005849 } else if (source.IsSIMDStackSlot()) {
5850 DCHECK(destination.IsFpuRegister());
5851 __ movups(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005852 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005853 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005854 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005855 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005856 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005857 if (value == 0) {
5858 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
5859 } else {
5860 __ movl(destination.AsRegister<Register>(), Immediate(value));
5861 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005862 } else {
5863 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05005864 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005865 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005866 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005867 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005868 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005869 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005870 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005871 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5872 if (value == 0) {
5873 // Easy handling of 0.0.
5874 __ xorps(dest, dest);
5875 } else {
5876 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005877 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5878 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
5879 __ movl(temp, Immediate(value));
5880 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005881 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005882 } else {
5883 DCHECK(destination.IsStackSlot()) << destination;
5884 __ movl(Address(ESP, destination.GetStackIndex()), imm);
5885 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005886 } else if (constant->IsLongConstant()) {
5887 int64_t value = constant->AsLongConstant()->GetValue();
5888 int32_t low_value = Low32Bits(value);
5889 int32_t high_value = High32Bits(value);
5890 Immediate low(low_value);
5891 Immediate high(high_value);
5892 if (destination.IsDoubleStackSlot()) {
5893 __ movl(Address(ESP, destination.GetStackIndex()), low);
5894 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5895 } else {
5896 __ movl(destination.AsRegisterPairLow<Register>(), low);
5897 __ movl(destination.AsRegisterPairHigh<Register>(), high);
5898 }
5899 } else {
5900 DCHECK(constant->IsDoubleConstant());
5901 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005902 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005903 int32_t low_value = Low32Bits(value);
5904 int32_t high_value = High32Bits(value);
5905 Immediate low(low_value);
5906 Immediate high(high_value);
5907 if (destination.IsFpuRegister()) {
5908 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5909 if (value == 0) {
5910 // Easy handling of 0.0.
5911 __ xorpd(dest, dest);
5912 } else {
5913 __ pushl(high);
5914 __ pushl(low);
5915 __ movsd(dest, Address(ESP, 0));
5916 __ addl(ESP, Immediate(8));
5917 }
5918 } else {
5919 DCHECK(destination.IsDoubleStackSlot()) << destination;
5920 __ movl(Address(ESP, destination.GetStackIndex()), low);
5921 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5922 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005923 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005924 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005925 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005926 }
5927}
5928
Mark Mendella5c19ce2015-04-01 12:51:05 -04005929void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005930 Register suggested_scratch = reg == EAX ? EBX : EAX;
5931 ScratchRegisterScope ensure_scratch(
5932 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
5933
5934 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5935 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
5936 __ movl(Address(ESP, mem + stack_offset), reg);
5937 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005938}
5939
Mark Mendell7c8d0092015-01-26 11:21:33 -05005940void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005941 ScratchRegisterScope ensure_scratch(
5942 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5943
5944 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5945 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5946 __ movl(temp_reg, Address(ESP, mem + stack_offset));
5947 __ movss(Address(ESP, mem + stack_offset), reg);
5948 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005949}
5950
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005951void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005952 ScratchRegisterScope ensure_scratch1(
5953 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005954
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005955 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
5956 ScratchRegisterScope ensure_scratch2(
5957 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005958
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005959 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
5960 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
5961 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
5962 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
5963 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
5964 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005965}
5966
5967void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005968 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005969 Location source = move->GetSource();
5970 Location destination = move->GetDestination();
5971
5972 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04005973 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
5974 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
5975 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
5976 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
5977 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005978 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005979 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005980 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005981 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005982 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5983 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005984 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
5985 // Use XOR Swap algorithm to avoid a temporary.
5986 DCHECK_NE(source.reg(), destination.reg());
5987 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5988 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5989 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5990 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
5991 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
5992 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
5993 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005994 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
5995 // Take advantage of the 16 bytes in the XMM register.
5996 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
5997 Address stack(ESP, destination.GetStackIndex());
5998 // Load the double into the high doubleword.
5999 __ movhpd(reg, stack);
6000
6001 // Store the low double into the destination.
6002 __ movsd(stack, reg);
6003
6004 // Move the high double to the low double.
6005 __ psrldq(reg, Immediate(8));
6006 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
6007 // Take advantage of the 16 bytes in the XMM register.
6008 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
6009 Address stack(ESP, source.GetStackIndex());
6010 // Load the double into the high doubleword.
6011 __ movhpd(reg, stack);
6012
6013 // Store the low double into the destination.
6014 __ movsd(stack, reg);
6015
6016 // Move the high double to the low double.
6017 __ psrldq(reg, Immediate(8));
6018 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
6019 Exchange(destination.GetStackIndex(), source.GetStackIndex());
6020 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006021 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006022 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006023 }
6024}
6025
6026void ParallelMoveResolverX86::SpillScratch(int reg) {
6027 __ pushl(static_cast<Register>(reg));
6028}
6029
6030void ParallelMoveResolverX86::RestoreScratch(int reg) {
6031 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006032}
6033
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006034HLoadClass::LoadKind CodeGeneratorX86::GetSupportedLoadClassKind(
6035 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006036 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006037 case HLoadClass::LoadKind::kInvalid:
6038 LOG(FATAL) << "UNREACHABLE";
6039 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006040 case HLoadClass::LoadKind::kReferrersClass:
6041 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006042 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006043 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006044 case HLoadClass::LoadKind::kBssEntry:
Vladimir Marko764d4542017-05-16 10:31:41 +01006045 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006046 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006047 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006048 DCHECK(Runtime::Current()->UseJitCompilation());
6049 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01006050 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006051 case HLoadClass::LoadKind::kRuntimeCall:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006052 break;
6053 }
6054 return desired_class_load_kind;
6055}
6056
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006057void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00006058 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006059 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006060 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00006061 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006062 cls,
6063 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00006064 Location::RegisterLocation(EAX));
Vladimir Markoea4c1262017-02-06 19:59:33 +00006065 DCHECK_EQ(calling_convention.GetRegisterAt(0), EAX);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006066 return;
6067 }
Vladimir Marko41559982017-01-06 14:04:23 +00006068 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006069
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006070 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
6071 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006072 ? LocationSummary::kCallOnSlowPath
6073 : LocationSummary::kNoCall;
6074 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006075 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006076 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006077 }
6078
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006079 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006080 load_kind == HLoadClass::LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006081 load_kind == HLoadClass::LoadKind::kBootImageClassTable ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006082 load_kind == HLoadClass::LoadKind::kBssEntry) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006083 locations->SetInAt(0, Location::RequiresRegister());
6084 }
6085 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006086 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
6087 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6088 // Rely on the type resolution and/or initialization to save everything.
6089 RegisterSet caller_saves = RegisterSet::Empty();
6090 InvokeRuntimeCallingConvention calling_convention;
6091 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6092 locations->SetCustomSlowPathCallerSaves(caller_saves);
6093 } else {
6094 // For non-Baker read barrier we have a temp-clobbering call.
6095 }
6096 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006097}
6098
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006099Label* CodeGeneratorX86::NewJitRootClassPatch(const DexFile& dex_file,
6100 dex::TypeIndex dex_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006101 Handle<mirror::Class> handle) {
6102 jit_class_roots_.Overwrite(TypeReference(&dex_file, dex_index),
6103 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006104 // Add a patch entry and return the label.
6105 jit_class_patches_.emplace_back(dex_file, dex_index.index_);
6106 PatchInfo<Label>* info = &jit_class_patches_.back();
6107 return &info->label;
6108}
6109
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006110// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6111// move.
6112void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00006113 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006114 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00006115 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01006116 return;
6117 }
Vladimir Marko41559982017-01-06 14:04:23 +00006118 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01006119
Vladimir Marko41559982017-01-06 14:04:23 +00006120 LocationSummary* locations = cls->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006121 Location out_loc = locations->Out();
6122 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006123
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006124 bool generate_null_check = false;
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006125 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
6126 ? kWithoutReadBarrier
6127 : kCompilerReadBarrierOption;
Vladimir Marko41559982017-01-06 14:04:23 +00006128 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006129 case HLoadClass::LoadKind::kReferrersClass: {
6130 DCHECK(!cls->CanCallRuntime());
6131 DCHECK(!cls->MustGenerateClinitCheck());
6132 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6133 Register current_method = locations->InAt(0).AsRegister<Register>();
6134 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006135 cls,
6136 out_loc,
6137 Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()),
Roland Levillain00468f32016-10-27 18:02:48 +01006138 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006139 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006140 break;
6141 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006142 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006143 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006144 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006145 Register method_address = locations->InAt(0).AsRegister<Register>();
6146 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Marko1998cd02017-01-13 13:02:58 +00006147 codegen_->RecordBootTypePatch(cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006148 break;
6149 }
6150 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006151 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006152 uint32_t address = dchecked_integral_cast<uint32_t>(
6153 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
6154 DCHECK_NE(address, 0u);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006155 __ movl(out, Immediate(address));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006156 break;
6157 }
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006158 case HLoadClass::LoadKind::kBootImageClassTable: {
6159 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6160 Register method_address = locations->InAt(0).AsRegister<Register>();
6161 __ movl(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
6162 codegen_->RecordBootTypePatch(cls);
6163 // Extract the reference from the slot data, i.e. clear the hash bits.
6164 int32_t masked_hash = ClassTable::TableSlot::MaskHash(
6165 ComputeModifiedUtf8Hash(cls->GetDexFile().StringByTypeIdx(cls->GetTypeIndex())));
6166 if (masked_hash != 0) {
6167 __ subl(out, Immediate(masked_hash));
6168 }
6169 break;
6170 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006171 case HLoadClass::LoadKind::kBssEntry: {
6172 Register method_address = locations->InAt(0).AsRegister<Register>();
6173 Address address(method_address, CodeGeneratorX86::kDummy32BitOffset);
6174 Label* fixup_label = codegen_->NewTypeBssEntryPatch(cls);
6175 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
6176 generate_null_check = true;
6177 break;
6178 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006179 case HLoadClass::LoadKind::kJitTableAddress: {
6180 Address address = Address::Absolute(CodeGeneratorX86::kDummy32BitOffset);
6181 Label* fixup_label = codegen_->NewJitRootClassPatch(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006182 cls->GetDexFile(), cls->GetTypeIndex(), cls->GetClass());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006183 // /* GcRoot<mirror::Class> */ out = *address
Vladimir Markoea4c1262017-02-06 19:59:33 +00006184 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006185 break;
6186 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006187 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006188 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00006189 LOG(FATAL) << "UNREACHABLE";
6190 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006191 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006192
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006193 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6194 DCHECK(cls->CanCallRuntime());
6195 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
6196 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
6197 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006198
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006199 if (generate_null_check) {
6200 __ testl(out, out);
6201 __ j(kEqual, slow_path->GetEntryLabel());
6202 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006203
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006204 if (cls->MustGenerateClinitCheck()) {
6205 GenerateClassInitializationCheck(slow_path, out);
6206 } else {
6207 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006208 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006209 }
6210}
6211
6212void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
6213 LocationSummary* locations =
6214 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
6215 locations->SetInAt(0, Location::RequiresRegister());
6216 if (check->HasUses()) {
6217 locations->SetOut(Location::SameAsFirstInput());
6218 }
6219}
6220
6221void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006222 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07006223 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006224 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006225 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00006226 GenerateClassInitializationCheck(slow_path,
6227 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006228}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006229
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006230void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07006231 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006232 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
6233 Immediate(mirror::Class::kStatusInitialized));
6234 __ j(kLess, slow_path->GetEntryLabel());
6235 __ Bind(slow_path->GetExitLabel());
6236 // No need for memory fence, thanks to the X86 memory model.
6237}
6238
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006239HLoadString::LoadKind CodeGeneratorX86::GetSupportedLoadStringKind(
6240 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006241 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006242 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006243 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00006244 case HLoadString::LoadKind::kBssEntry:
Vladimir Marko764d4542017-05-16 10:31:41 +01006245 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006246 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006247 case HLoadString::LoadKind::kJitTableAddress:
6248 DCHECK(Runtime::Current()->UseJitCompilation());
6249 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01006250 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006251 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006252 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006253 }
6254 return desired_string_load_kind;
6255}
6256
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006257void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006258 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006259 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006260 HLoadString::LoadKind load_kind = load->GetLoadKind();
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006261 if (load_kind == HLoadString::LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006262 load_kind == HLoadString::LoadKind::kBootImageInternTable ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00006263 load_kind == HLoadString::LoadKind::kBssEntry) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006264 locations->SetInAt(0, Location::RequiresRegister());
6265 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006266 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006267 locations->SetOut(Location::RegisterLocation(EAX));
6268 } else {
6269 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006270 if (load_kind == HLoadString::LoadKind::kBssEntry) {
6271 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00006272 // Rely on the pResolveString to save everything.
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006273 RegisterSet caller_saves = RegisterSet::Empty();
6274 InvokeRuntimeCallingConvention calling_convention;
6275 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6276 locations->SetCustomSlowPathCallerSaves(caller_saves);
6277 } else {
6278 // For non-Baker read barrier we have a temp-clobbering call.
6279 }
6280 }
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006281 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006282}
6283
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006284Label* CodeGeneratorX86::NewJitRootStringPatch(const DexFile& dex_file,
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006285 dex::StringIndex dex_index,
6286 Handle<mirror::String> handle) {
6287 jit_string_roots_.Overwrite(
6288 StringReference(&dex_file, dex_index), reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006289 // Add a patch entry and return the label.
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006290 jit_string_patches_.emplace_back(dex_file, dex_index.index_);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006291 PatchInfo<Label>* info = &jit_string_patches_.back();
6292 return &info->label;
6293}
6294
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006295// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6296// move.
6297void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006298 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006299 Location out_loc = locations->Out();
6300 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006301
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006302 switch (load->GetLoadKind()) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006303 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006304 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006305 Register method_address = locations->InAt(0).AsRegister<Register>();
6306 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Markoaad75c62016-10-03 08:46:48 +00006307 codegen_->RecordBootStringPatch(load);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006308 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006309 }
6310 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006311 uint32_t address = dchecked_integral_cast<uint32_t>(
6312 reinterpret_cast<uintptr_t>(load->GetString().Get()));
6313 DCHECK_NE(address, 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006314 __ movl(out, Immediate(address));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006315 return;
6316 }
6317 case HLoadString::LoadKind::kBootImageInternTable: {
6318 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6319 Register method_address = locations->InAt(0).AsRegister<Register>();
6320 __ movl(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
6321 codegen_->RecordBootStringPatch(load);
6322 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006323 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00006324 case HLoadString::LoadKind::kBssEntry: {
6325 Register method_address = locations->InAt(0).AsRegister<Register>();
6326 Address address = Address(method_address, CodeGeneratorX86::kDummy32BitOffset);
6327 Label* fixup_label = codegen_->NewStringBssEntryPatch(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006328 // /* GcRoot<mirror::String> */ out = *address /* PC-relative */
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006329 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
Vladimir Markoaad75c62016-10-03 08:46:48 +00006330 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
6331 codegen_->AddSlowPath(slow_path);
6332 __ testl(out, out);
6333 __ j(kEqual, slow_path->GetEntryLabel());
6334 __ Bind(slow_path->GetExitLabel());
6335 return;
6336 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006337 case HLoadString::LoadKind::kJitTableAddress: {
6338 Address address = Address::Absolute(CodeGeneratorX86::kDummy32BitOffset);
6339 Label* fixup_label = codegen_->NewJitRootStringPatch(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006340 load->GetDexFile(), load->GetStringIndex(), load->GetString());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006341 // /* GcRoot<mirror::String> */ out = *address
6342 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
6343 return;
6344 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006345 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006346 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006347 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006348
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006349 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006350 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006351 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006352 __ movl(calling_convention.GetRegisterAt(0), Immediate(load->GetStringIndex().index_));
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006353 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6354 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006355}
6356
David Brazdilcb1c0552015-08-04 16:22:25 +01006357static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006358 return Address::Absolute(Thread::ExceptionOffset<kX86PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01006359}
6360
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006361void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
6362 LocationSummary* locations =
6363 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
6364 locations->SetOut(Location::RequiresRegister());
6365}
6366
6367void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01006368 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
6369}
6370
6371void LocationsBuilderX86::VisitClearException(HClearException* clear) {
6372 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
6373}
6374
6375void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6376 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006377}
6378
6379void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
6380 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006381 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006382 InvokeRuntimeCallingConvention calling_convention;
6383 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6384}
6385
6386void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006387 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006388 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006389}
6390
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006391// Temp is used for read barrier.
6392static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
6393 if (kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00006394 !kUseBakerReadBarrier &&
6395 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain7c1559a2015-12-15 10:55:36 +00006396 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006397 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
6398 return 1;
6399 }
6400 return 0;
6401}
6402
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006403// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006404// interface pointer, one for loading the current interface.
6405// The other checks have one temp for loading the object's class.
6406static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
6407 if (type_check_kind == TypeCheckKind::kInterfaceCheck && !kPoisonHeapReferences) {
6408 return 2;
6409 }
6410 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006411}
6412
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006413void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006414 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006415 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01006416 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006417 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006418 case TypeCheckKind::kExactCheck:
6419 case TypeCheckKind::kAbstractClassCheck:
6420 case TypeCheckKind::kClassHierarchyCheck:
6421 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006422 call_kind =
6423 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01006424 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006425 break;
6426 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006427 case TypeCheckKind::kUnresolvedCheck:
6428 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006429 call_kind = LocationSummary::kCallOnSlowPath;
6430 break;
6431 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006432
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006433 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01006434 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006435 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006436 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006437 locations->SetInAt(0, Location::RequiresRegister());
6438 locations->SetInAt(1, Location::Any());
6439 // Note that TypeCheckSlowPathX86 uses this "out" register too.
6440 locations->SetOut(Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006441 // When read barriers are enabled, we need a temporary register for some cases.
6442 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006443}
6444
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006445void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006446 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006447 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006448 Location obj_loc = locations->InAt(0);
6449 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006450 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006451 Location out_loc = locations->Out();
6452 Register out = out_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006453 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
6454 DCHECK_LE(num_temps, 1u);
6455 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006456 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006457 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6458 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6459 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07006460 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006461 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006462
6463 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006464 // Avoid null check if we know obj is not null.
6465 if (instruction->MustDoNullCheck()) {
6466 __ testl(obj, obj);
6467 __ j(kEqual, &zero);
6468 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006469
Roland Levillain7c1559a2015-12-15 10:55:36 +00006470 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006471 case TypeCheckKind::kExactCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006472 // /* HeapReference<Class> */ out = obj->klass_
6473 GenerateReferenceLoadTwoRegisters(instruction,
6474 out_loc,
6475 obj_loc,
6476 class_offset,
6477 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006478 if (cls.IsRegister()) {
6479 __ cmpl(out, cls.AsRegister<Register>());
6480 } else {
6481 DCHECK(cls.IsStackSlot()) << cls;
6482 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6483 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006484
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006485 // Classes must be equal for the instanceof to succeed.
6486 __ j(kNotEqual, &zero);
6487 __ movl(out, Immediate(1));
6488 __ jmp(&done);
6489 break;
6490 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006491
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006492 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006493 // /* HeapReference<Class> */ out = obj->klass_
6494 GenerateReferenceLoadTwoRegisters(instruction,
6495 out_loc,
6496 obj_loc,
6497 class_offset,
6498 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006499 // If the class is abstract, we eagerly fetch the super class of the
6500 // object to avoid doing a comparison we know will fail.
6501 NearLabel loop;
6502 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006503 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006504 GenerateReferenceLoadOneRegister(instruction,
6505 out_loc,
6506 super_offset,
6507 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006508 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006509 __ testl(out, out);
6510 // If `out` is null, we use it for the result, and jump to `done`.
6511 __ j(kEqual, &done);
6512 if (cls.IsRegister()) {
6513 __ cmpl(out, cls.AsRegister<Register>());
6514 } else {
6515 DCHECK(cls.IsStackSlot()) << cls;
6516 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6517 }
6518 __ j(kNotEqual, &loop);
6519 __ movl(out, Immediate(1));
6520 if (zero.IsLinked()) {
6521 __ jmp(&done);
6522 }
6523 break;
6524 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006525
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006526 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006527 // /* HeapReference<Class> */ out = obj->klass_
6528 GenerateReferenceLoadTwoRegisters(instruction,
6529 out_loc,
6530 obj_loc,
6531 class_offset,
6532 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006533 // Walk over the class hierarchy to find a match.
6534 NearLabel loop, success;
6535 __ Bind(&loop);
6536 if (cls.IsRegister()) {
6537 __ cmpl(out, cls.AsRegister<Register>());
6538 } else {
6539 DCHECK(cls.IsStackSlot()) << cls;
6540 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6541 }
6542 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006543 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006544 GenerateReferenceLoadOneRegister(instruction,
6545 out_loc,
6546 super_offset,
6547 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006548 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006549 __ testl(out, out);
6550 __ j(kNotEqual, &loop);
6551 // If `out` is null, we use it for the result, and jump to `done`.
6552 __ jmp(&done);
6553 __ Bind(&success);
6554 __ movl(out, Immediate(1));
6555 if (zero.IsLinked()) {
6556 __ jmp(&done);
6557 }
6558 break;
6559 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006560
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006561 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006562 // /* HeapReference<Class> */ out = obj->klass_
6563 GenerateReferenceLoadTwoRegisters(instruction,
6564 out_loc,
6565 obj_loc,
6566 class_offset,
6567 kCompilerReadBarrierOption);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006568 // Do an exact check.
6569 NearLabel exact_check;
6570 if (cls.IsRegister()) {
6571 __ cmpl(out, cls.AsRegister<Register>());
6572 } else {
6573 DCHECK(cls.IsStackSlot()) << cls;
6574 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6575 }
6576 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006577 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006578 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006579 GenerateReferenceLoadOneRegister(instruction,
6580 out_loc,
6581 component_offset,
6582 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006583 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006584 __ testl(out, out);
6585 // If `out` is null, we use it for the result, and jump to `done`.
6586 __ j(kEqual, &done);
6587 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
6588 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006589 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006590 __ movl(out, Immediate(1));
6591 __ jmp(&done);
6592 break;
6593 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006594
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006595 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006596 // No read barrier since the slow path will retry upon failure.
6597 // /* HeapReference<Class> */ out = obj->klass_
6598 GenerateReferenceLoadTwoRegisters(instruction,
6599 out_loc,
6600 obj_loc,
6601 class_offset,
6602 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006603 if (cls.IsRegister()) {
6604 __ cmpl(out, cls.AsRegister<Register>());
6605 } else {
6606 DCHECK(cls.IsStackSlot()) << cls;
6607 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6608 }
6609 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006610 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6611 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006612 codegen_->AddSlowPath(slow_path);
6613 __ j(kNotEqual, slow_path->GetEntryLabel());
6614 __ movl(out, Immediate(1));
6615 if (zero.IsLinked()) {
6616 __ jmp(&done);
6617 }
6618 break;
6619 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006620
Calin Juravle98893e12015-10-02 21:05:03 +01006621 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006622 case TypeCheckKind::kInterfaceCheck: {
6623 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006624 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00006625 // cases.
6626 //
6627 // We cannot directly call the InstanceofNonTrivial runtime
6628 // entry point without resorting to a type checking slow path
6629 // here (i.e. by calling InvokeRuntime directly), as it would
6630 // require to assign fixed registers for the inputs of this
6631 // HInstanceOf instruction (following the runtime calling
6632 // convention), which might be cluttered by the potential first
6633 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006634 //
6635 // TODO: Introduce a new runtime entry point taking the object
6636 // to test (instead of its class) as argument, and let it deal
6637 // with the read barrier issues. This will let us refactor this
6638 // case of the `switch` code as it was previously (with a direct
6639 // call to the runtime not using a type checking slow path).
6640 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006641 DCHECK(locations->OnlyCallsOnSlowPath());
6642 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6643 /* is_fatal */ false);
6644 codegen_->AddSlowPath(slow_path);
6645 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006646 if (zero.IsLinked()) {
6647 __ jmp(&done);
6648 }
6649 break;
6650 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006651 }
6652
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006653 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006654 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006655 __ xorl(out, out);
6656 }
6657
6658 if (done.IsLinked()) {
6659 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006660 }
6661
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006662 if (slow_path != nullptr) {
6663 __ Bind(slow_path->GetExitLabel());
6664 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006665}
6666
Mathieu Chartier5ac321b2016-11-09 16:33:54 -08006667static bool IsTypeCheckSlowPathFatal(TypeCheckKind type_check_kind, bool throws_into_catch) {
6668 switch (type_check_kind) {
6669 case TypeCheckKind::kExactCheck:
6670 case TypeCheckKind::kAbstractClassCheck:
6671 case TypeCheckKind::kClassHierarchyCheck:
6672 case TypeCheckKind::kArrayObjectCheck:
6673 return !throws_into_catch && !kEmitCompilerReadBarrier;
6674 case TypeCheckKind::kInterfaceCheck:
6675 return !throws_into_catch && !kEmitCompilerReadBarrier && !kPoisonHeapReferences;
6676 case TypeCheckKind::kArrayCheck:
6677 case TypeCheckKind::kUnresolvedCheck:
6678 return false;
6679 }
6680 LOG(FATAL) << "Unreachable";
6681 UNREACHABLE();
6682}
6683
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006684void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006685 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006686 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Mathieu Chartier5ac321b2016-11-09 16:33:54 -08006687 LocationSummary::CallKind call_kind =
6688 IsTypeCheckSlowPathFatal(type_check_kind, throws_into_catch)
6689 ? LocationSummary::kNoCall
6690 : LocationSummary::kCallOnSlowPath;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006691 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6692 locations->SetInAt(0, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006693 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
6694 // Require a register for the interface check since there is a loop that compares the class to
6695 // a memory address.
6696 locations->SetInAt(1, Location::RequiresRegister());
6697 } else {
6698 locations->SetInAt(1, Location::Any());
6699 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006700 // Note that TypeCheckSlowPathX86 uses this "temp" register too.
6701 locations->AddTemp(Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006702 // When read barriers are enabled, we need an additional temporary register for some cases.
6703 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
6704}
6705
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006706void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006707 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006708 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006709 Location obj_loc = locations->InAt(0);
6710 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006711 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006712 Location temp_loc = locations->GetTemp(0);
6713 Register temp = temp_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006714 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
6715 DCHECK_GE(num_temps, 1u);
6716 DCHECK_LE(num_temps, 2u);
6717 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
6718 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6719 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6720 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6721 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
6722 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
6723 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
6724 const uint32_t object_array_data_offset =
6725 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006726
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006727 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
6728 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
6729 // read barriers is done for performance and code size reasons.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006730 bool is_type_check_slow_path_fatal =
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006731 IsTypeCheckSlowPathFatal(type_check_kind, instruction->CanThrowIntoCatchBlock());
6732
Roland Levillain0d5a2812015-11-13 10:07:31 +00006733 SlowPathCode* type_check_slow_path =
6734 new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6735 is_type_check_slow_path_fatal);
6736 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006737
Roland Levillain0d5a2812015-11-13 10:07:31 +00006738 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006739 // Avoid null check if we know obj is not null.
6740 if (instruction->MustDoNullCheck()) {
6741 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006742 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006743 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006744
Roland Levillain0d5a2812015-11-13 10:07:31 +00006745 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006746 case TypeCheckKind::kExactCheck:
6747 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006748 // /* HeapReference<Class> */ temp = obj->klass_
6749 GenerateReferenceLoadTwoRegisters(instruction,
6750 temp_loc,
6751 obj_loc,
6752 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006753 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006754
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006755 if (cls.IsRegister()) {
6756 __ cmpl(temp, cls.AsRegister<Register>());
6757 } else {
6758 DCHECK(cls.IsStackSlot()) << cls;
6759 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6760 }
6761 // Jump to slow path for throwing the exception or doing a
6762 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006763 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006764 break;
6765 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006766
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006767 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006768 // /* HeapReference<Class> */ temp = obj->klass_
6769 GenerateReferenceLoadTwoRegisters(instruction,
6770 temp_loc,
6771 obj_loc,
6772 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006773 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006774
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006775 // If the class is abstract, we eagerly fetch the super class of the
6776 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006777 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006778 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006779 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006780 GenerateReferenceLoadOneRegister(instruction,
6781 temp_loc,
6782 super_offset,
6783 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006784 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006785
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006786 // If the class reference currently in `temp` is null, jump to the slow path to throw the
6787 // exception.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006788 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006789 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006790
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006791 // Otherwise, compare the classes
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006792 if (cls.IsRegister()) {
6793 __ cmpl(temp, cls.AsRegister<Register>());
6794 } else {
6795 DCHECK(cls.IsStackSlot()) << cls;
6796 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6797 }
6798 __ j(kNotEqual, &loop);
6799 break;
6800 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006801
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006802 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006803 // /* HeapReference<Class> */ temp = obj->klass_
6804 GenerateReferenceLoadTwoRegisters(instruction,
6805 temp_loc,
6806 obj_loc,
6807 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006808 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006809
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006810 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006811 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006812 __ Bind(&loop);
6813 if (cls.IsRegister()) {
6814 __ cmpl(temp, cls.AsRegister<Register>());
6815 } else {
6816 DCHECK(cls.IsStackSlot()) << cls;
6817 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6818 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006819 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006820
Roland Levillain0d5a2812015-11-13 10:07:31 +00006821 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006822 GenerateReferenceLoadOneRegister(instruction,
6823 temp_loc,
6824 super_offset,
6825 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006826 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006827
6828 // If the class reference currently in `temp` is not null, jump
6829 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006830 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006831 __ j(kNotZero, &loop);
6832 // Otherwise, jump to the slow path to throw the exception.;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006833 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006834 break;
6835 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006836
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006837 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006838 // /* HeapReference<Class> */ temp = obj->klass_
6839 GenerateReferenceLoadTwoRegisters(instruction,
6840 temp_loc,
6841 obj_loc,
6842 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006843 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006844
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006845 // Do an exact check.
6846 if (cls.IsRegister()) {
6847 __ cmpl(temp, cls.AsRegister<Register>());
6848 } else {
6849 DCHECK(cls.IsStackSlot()) << cls;
6850 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6851 }
6852 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006853
6854 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006855 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006856 GenerateReferenceLoadOneRegister(instruction,
6857 temp_loc,
6858 component_offset,
6859 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006860 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006861
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006862 // If the component type is null (i.e. the object not an array), jump to the slow path to
6863 // throw the exception. Otherwise proceed with the check.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006864 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006865 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006866
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006867 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006868 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006869 break;
6870 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006871
Calin Juravle98893e12015-10-02 21:05:03 +01006872 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006873 // We always go into the type check slow path for the unresolved check case.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006874 // We cannot directly call the CheckCast runtime entry point
6875 // without resorting to a type checking slow path here (i.e. by
6876 // calling InvokeRuntime directly), as it would require to
6877 // assign fixed registers for the inputs of this HInstanceOf
6878 // instruction (following the runtime calling convention), which
6879 // might be cluttered by the potential first read barrier
6880 // emission at the beginning of this method.
6881 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006882 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006883
6884 case TypeCheckKind::kInterfaceCheck: {
6885 // Fast path for the interface check. Since we compare with a memory location in the inner
6886 // loop we would need to have cls poisoned. However unpoisoning cls would reset the
6887 // conditional flags and cause the conditional jump to be incorrect. Therefore we just jump
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006888 // to the slow path if we are running under poisoning.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006889 if (!kPoisonHeapReferences) {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006890 // Try to avoid read barriers to improve the fast path. We can not get false positives by
6891 // doing this.
6892 // /* HeapReference<Class> */ temp = obj->klass_
6893 GenerateReferenceLoadTwoRegisters(instruction,
6894 temp_loc,
6895 obj_loc,
6896 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006897 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006898
6899 // /* HeapReference<Class> */ temp = temp->iftable_
6900 GenerateReferenceLoadTwoRegisters(instruction,
6901 temp_loc,
6902 temp_loc,
6903 iftable_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006904 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08006905 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006906 __ movl(maybe_temp2_loc.AsRegister<Register>(), Address(temp, array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08006907 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006908 NearLabel start_loop;
6909 __ Bind(&start_loop);
Mathieu Chartier6beced42016-11-15 15:51:31 -08006910 // Need to subtract first to handle the empty array case.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006911 __ subl(maybe_temp2_loc.AsRegister<Register>(), Immediate(2));
Mathieu Chartier6beced42016-11-15 15:51:31 -08006912 __ j(kNegative, type_check_slow_path->GetEntryLabel());
6913 // Go to next interface if the classes do not match.
6914 __ cmpl(cls.AsRegister<Register>(),
6915 CodeGeneratorX86::ArrayAddress(temp,
6916 maybe_temp2_loc,
6917 TIMES_4,
6918 object_array_data_offset));
6919 __ j(kNotEqual, &start_loop);
6920 } else {
6921 __ jmp(type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006922 }
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006923 break;
6924 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006925 }
6926 __ Bind(&done);
6927
Roland Levillain0d5a2812015-11-13 10:07:31 +00006928 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006929}
6930
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006931void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
6932 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006933 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006934 InvokeRuntimeCallingConvention calling_convention;
6935 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6936}
6937
6938void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006939 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject
6940 : kQuickUnlockObject,
Alexandre Rames8158f282015-08-07 10:26:17 +01006941 instruction,
Serban Constantinescuba45db02016-07-12 22:53:02 +01006942 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006943 if (instruction->IsEnter()) {
6944 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6945 } else {
6946 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6947 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006948}
6949
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006950void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6951void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6952void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6953
6954void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6955 LocationSummary* locations =
6956 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6957 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6958 || instruction->GetResultType() == Primitive::kPrimLong);
6959 locations->SetInAt(0, Location::RequiresRegister());
6960 locations->SetInAt(1, Location::Any());
6961 locations->SetOut(Location::SameAsFirstInput());
6962}
6963
6964void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
6965 HandleBitwiseOperation(instruction);
6966}
6967
6968void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
6969 HandleBitwiseOperation(instruction);
6970}
6971
6972void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
6973 HandleBitwiseOperation(instruction);
6974}
6975
6976void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6977 LocationSummary* locations = instruction->GetLocations();
6978 Location first = locations->InAt(0);
6979 Location second = locations->InAt(1);
6980 DCHECK(first.Equals(locations->Out()));
6981
6982 if (instruction->GetResultType() == Primitive::kPrimInt) {
6983 if (second.IsRegister()) {
6984 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006985 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006986 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006987 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006988 } else {
6989 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006990 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006991 }
6992 } else if (second.IsConstant()) {
6993 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006994 __ andl(first.AsRegister<Register>(),
6995 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006996 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006997 __ orl(first.AsRegister<Register>(),
6998 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006999 } else {
7000 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00007001 __ xorl(first.AsRegister<Register>(),
7002 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007003 }
7004 } else {
7005 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007006 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007007 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007008 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007009 } else {
7010 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00007011 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007012 }
7013 }
7014 } else {
7015 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
7016 if (second.IsRegisterPair()) {
7017 if (instruction->IsAnd()) {
7018 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7019 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7020 } else if (instruction->IsOr()) {
7021 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7022 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7023 } else {
7024 DCHECK(instruction->IsXor());
7025 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7026 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7027 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007028 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007029 if (instruction->IsAnd()) {
7030 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7031 __ andl(first.AsRegisterPairHigh<Register>(),
7032 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7033 } else if (instruction->IsOr()) {
7034 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7035 __ orl(first.AsRegisterPairHigh<Register>(),
7036 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7037 } else {
7038 DCHECK(instruction->IsXor());
7039 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7040 __ xorl(first.AsRegisterPairHigh<Register>(),
7041 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7042 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007043 } else {
7044 DCHECK(second.IsConstant()) << second;
7045 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007046 int32_t low_value = Low32Bits(value);
7047 int32_t high_value = High32Bits(value);
7048 Immediate low(low_value);
7049 Immediate high(high_value);
7050 Register first_low = first.AsRegisterPairLow<Register>();
7051 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007052 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007053 if (low_value == 0) {
7054 __ xorl(first_low, first_low);
7055 } else if (low_value != -1) {
7056 __ andl(first_low, low);
7057 }
7058 if (high_value == 0) {
7059 __ xorl(first_high, first_high);
7060 } else if (high_value != -1) {
7061 __ andl(first_high, high);
7062 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007063 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007064 if (low_value != 0) {
7065 __ orl(first_low, low);
7066 }
7067 if (high_value != 0) {
7068 __ orl(first_high, high);
7069 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007070 } else {
7071 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007072 if (low_value != 0) {
7073 __ xorl(first_low, low);
7074 }
7075 if (high_value != 0) {
7076 __ xorl(first_high, high);
7077 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007078 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007079 }
7080 }
7081}
7082
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007083void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(
7084 HInstruction* instruction,
7085 Location out,
7086 uint32_t offset,
7087 Location maybe_temp,
7088 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007089 Register out_reg = out.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007090 if (read_barrier_option == kWithReadBarrier) {
7091 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007092 if (kUseBakerReadBarrier) {
7093 // Load with fast path based Baker's read barrier.
7094 // /* HeapReference<Object> */ out = *(out + offset)
7095 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00007096 instruction, out, out_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007097 } else {
7098 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007099 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain7c1559a2015-12-15 10:55:36 +00007100 // in the following move operation, as we will need it for the
7101 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00007102 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007103 __ movl(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007104 // /* HeapReference<Object> */ out = *(out + offset)
7105 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007106 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007107 }
7108 } else {
7109 // Plain load with no read barrier.
7110 // /* HeapReference<Object> */ out = *(out + offset)
7111 __ movl(out_reg, Address(out_reg, offset));
7112 __ MaybeUnpoisonHeapReference(out_reg);
7113 }
7114}
7115
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007116void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(
7117 HInstruction* instruction,
7118 Location out,
7119 Location obj,
7120 uint32_t offset,
7121 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007122 Register out_reg = out.AsRegister<Register>();
7123 Register obj_reg = obj.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007124 if (read_barrier_option == kWithReadBarrier) {
7125 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007126 if (kUseBakerReadBarrier) {
7127 // Load with fast path based Baker's read barrier.
7128 // /* HeapReference<Object> */ out = *(obj + offset)
7129 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00007130 instruction, out, obj_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007131 } else {
7132 // Load with slow path based read barrier.
7133 // /* HeapReference<Object> */ out = *(obj + offset)
7134 __ movl(out_reg, Address(obj_reg, offset));
7135 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
7136 }
7137 } else {
7138 // Plain load with no read barrier.
7139 // /* HeapReference<Object> */ out = *(obj + offset)
7140 __ movl(out_reg, Address(obj_reg, offset));
7141 __ MaybeUnpoisonHeapReference(out_reg);
7142 }
7143}
7144
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007145void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(
7146 HInstruction* instruction,
7147 Location root,
7148 const Address& address,
7149 Label* fixup_label,
7150 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007151 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007152 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07007153 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007154 if (kUseBakerReadBarrier) {
7155 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
7156 // Baker's read barrier are used:
7157 //
Roland Levillaind966ce72017-02-09 16:20:14 +00007158 // root = obj.field;
7159 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
7160 // if (temp != null) {
7161 // root = temp(root)
Roland Levillain7c1559a2015-12-15 10:55:36 +00007162 // }
7163
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007164 // /* GcRoot<mirror::Object> */ root = *address
7165 __ movl(root_reg, address);
7166 if (fixup_label != nullptr) {
7167 __ Bind(fixup_label);
7168 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007169 static_assert(
7170 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
7171 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
7172 "have different sizes.");
7173 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
7174 "art::mirror::CompressedReference<mirror::Object> and int32_t "
7175 "have different sizes.");
7176
Vladimir Marko953437b2016-08-24 08:30:46 +00007177 // Slow path marking the GC root `root`.
7178 SlowPathCode* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007179 instruction, root, /* unpoison_ref_before_marking */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007180 codegen_->AddSlowPath(slow_path);
7181
Roland Levillaind966ce72017-02-09 16:20:14 +00007182 // Test the entrypoint (`Thread::Current()->pReadBarrierMarkReg ## root.reg()`).
7183 const int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +01007184 Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(root.reg());
Roland Levillaind966ce72017-02-09 16:20:14 +00007185 __ fs()->cmpl(Address::Absolute(entry_point_offset), Immediate(0));
7186 // The entrypoint is null when the GC is not marking.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007187 __ j(kNotEqual, slow_path->GetEntryLabel());
7188 __ Bind(slow_path->GetExitLabel());
7189 } else {
7190 // GC root loaded through a slow path for read barriers other
7191 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007192 // /* GcRoot<mirror::Object>* */ root = address
7193 __ leal(root_reg, address);
7194 if (fixup_label != nullptr) {
7195 __ Bind(fixup_label);
7196 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007197 // /* mirror::Object* */ root = root->Read()
7198 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
7199 }
7200 } else {
7201 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007202 // /* GcRoot<mirror::Object> */ root = *address
7203 __ movl(root_reg, address);
7204 if (fixup_label != nullptr) {
7205 __ Bind(fixup_label);
7206 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007207 // Note that GC roots are not affected by heap poisoning, thus we
7208 // do not have to unpoison `root_reg` here.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007209 }
7210}
7211
7212void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
7213 Location ref,
7214 Register obj,
7215 uint32_t offset,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007216 bool needs_null_check) {
7217 DCHECK(kEmitCompilerReadBarrier);
7218 DCHECK(kUseBakerReadBarrier);
7219
7220 // /* HeapReference<Object> */ ref = *(obj + offset)
7221 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007222 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007223}
7224
7225void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
7226 Location ref,
7227 Register obj,
7228 uint32_t data_offset,
7229 Location index,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007230 bool needs_null_check) {
7231 DCHECK(kEmitCompilerReadBarrier);
7232 DCHECK(kUseBakerReadBarrier);
7233
Roland Levillain3d312422016-06-23 13:53:42 +01007234 static_assert(
7235 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
7236 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00007237 // /* HeapReference<Object> */ ref =
7238 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007239 Address src = CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007240 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007241}
7242
7243void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
7244 Location ref,
7245 Register obj,
7246 const Address& src,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007247 bool needs_null_check,
7248 bool always_update_field,
7249 Register* temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007250 DCHECK(kEmitCompilerReadBarrier);
7251 DCHECK(kUseBakerReadBarrier);
7252
7253 // In slow path based read barriers, the read barrier call is
7254 // inserted after the original load. However, in fast path based
7255 // Baker's read barriers, we need to perform the load of
7256 // mirror::Object::monitor_ *before* the original reference load.
7257 // This load-load ordering is required by the read barrier.
7258 // The fast path/slow path (for Baker's algorithm) should look like:
7259 //
7260 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7261 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7262 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007263 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain7c1559a2015-12-15 10:55:36 +00007264 // if (is_gray) {
7265 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7266 // }
7267 //
7268 // Note: the original implementation in ReadBarrier::Barrier is
7269 // slightly more complex as:
7270 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007271 // the high-bits of rb_state, which are expected to be all zeroes
7272 // (we use CodeGeneratorX86::GenerateMemoryBarrier instead here,
7273 // which is a no-op thanks to the x86 memory model);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007274 // - it performs additional checks that we do not do here for
7275 // performance reasons.
7276
7277 Register ref_reg = ref.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +00007278 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7279
Vladimir Marko953437b2016-08-24 08:30:46 +00007280 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007281 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
7282 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko953437b2016-08-24 08:30:46 +00007283 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
7284 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
7285 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
7286
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007287 // if (rb_state == ReadBarrier::GrayState())
Vladimir Marko953437b2016-08-24 08:30:46 +00007288 // ref = ReadBarrier::Mark(ref);
7289 // At this point, just do the "if" and make sure that flags are preserved until the branch.
7290 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain7c1559a2015-12-15 10:55:36 +00007291 if (needs_null_check) {
7292 MaybeRecordImplicitNullCheck(instruction);
7293 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007294
7295 // Load fence to prevent load-load reordering.
7296 // Note that this is a no-op, thanks to the x86 memory model.
7297 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
7298
7299 // The actual reference load.
7300 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00007301 __ movl(ref_reg, src); // Flags are unaffected.
7302
7303 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
7304 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007305 SlowPathCode* slow_path;
7306 if (always_update_field) {
7307 DCHECK(temp != nullptr);
7308 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkAndUpdateFieldSlowPathX86(
7309 instruction, ref, obj, src, /* unpoison_ref_before_marking */ true, *temp);
7310 } else {
7311 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(
7312 instruction, ref, /* unpoison_ref_before_marking */ true);
7313 }
Vladimir Marko953437b2016-08-24 08:30:46 +00007314 AddSlowPath(slow_path);
7315
7316 // We have done the "if" of the gray bit check above, now branch based on the flags.
7317 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain7c1559a2015-12-15 10:55:36 +00007318
7319 // Object* ref = ref_addr->AsMirrorPtr()
7320 __ MaybeUnpoisonHeapReference(ref_reg);
7321
Roland Levillain7c1559a2015-12-15 10:55:36 +00007322 __ Bind(slow_path->GetExitLabel());
7323}
7324
7325void CodeGeneratorX86::GenerateReadBarrierSlow(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 DCHECK(kEmitCompilerReadBarrier);
7332
Roland Levillain7c1559a2015-12-15 10:55:36 +00007333 // Insert a slow path based read barrier *after* the reference load.
7334 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007335 // If heap poisoning is enabled, the unpoisoning of the loaded
7336 // reference will be carried out by the runtime within the slow
7337 // path.
7338 //
7339 // Note that `ref` currently does not get unpoisoned (when heap
7340 // poisoning is enabled), which is alright as the `ref` argument is
7341 // not used by the artReadBarrierSlow entry point.
7342 //
7343 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
7344 SlowPathCode* slow_path = new (GetGraph()->GetArena())
7345 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
7346 AddSlowPath(slow_path);
7347
Roland Levillain0d5a2812015-11-13 10:07:31 +00007348 __ jmp(slow_path->GetEntryLabel());
7349 __ Bind(slow_path->GetExitLabel());
7350}
7351
Roland Levillain7c1559a2015-12-15 10:55:36 +00007352void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7353 Location out,
7354 Location ref,
7355 Location obj,
7356 uint32_t offset,
7357 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007358 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007359 // Baker's read barriers shall be handled by the fast path
7360 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
7361 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007362 // If heap poisoning is enabled, unpoisoning will be taken care of
7363 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007364 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007365 } else if (kPoisonHeapReferences) {
7366 __ UnpoisonHeapReference(out.AsRegister<Register>());
7367 }
7368}
7369
Roland Levillain7c1559a2015-12-15 10:55:36 +00007370void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7371 Location out,
7372 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007373 DCHECK(kEmitCompilerReadBarrier);
7374
Roland Levillain7c1559a2015-12-15 10:55:36 +00007375 // Insert a slow path based read barrier *after* the GC root load.
7376 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007377 // Note that GC roots are not affected by heap poisoning, so we do
7378 // not need to do anything special for this here.
7379 SlowPathCode* slow_path =
7380 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86(instruction, out, root);
7381 AddSlowPath(slow_path);
7382
Roland Levillain0d5a2812015-11-13 10:07:31 +00007383 __ jmp(slow_path->GetEntryLabel());
7384 __ Bind(slow_path->GetExitLabel());
7385}
7386
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007387void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007388 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007389 LOG(FATAL) << "Unreachable";
7390}
7391
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007392void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007393 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007394 LOG(FATAL) << "Unreachable";
7395}
7396
Mark Mendellfe57faa2015-09-18 09:26:15 -04007397// Simple implementation of packed switch - generate cascaded compare/jumps.
7398void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7399 LocationSummary* locations =
7400 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7401 locations->SetInAt(0, Location::RequiresRegister());
7402}
7403
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007404void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
7405 int32_t lower_bound,
7406 uint32_t num_entries,
7407 HBasicBlock* switch_block,
7408 HBasicBlock* default_block) {
7409 // Figure out the correct compare values and jump conditions.
7410 // Handle the first compare/branch as a special case because it might
7411 // jump to the default case.
7412 DCHECK_GT(num_entries, 2u);
7413 Condition first_condition;
7414 uint32_t index;
7415 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
7416 if (lower_bound != 0) {
7417 first_condition = kLess;
7418 __ cmpl(value_reg, Immediate(lower_bound));
7419 __ j(first_condition, codegen_->GetLabelOf(default_block));
7420 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007421
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007422 index = 1;
7423 } else {
7424 // Handle all the compare/jumps below.
7425 first_condition = kBelow;
7426 index = 0;
7427 }
7428
7429 // Handle the rest of the compare/jumps.
7430 for (; index + 1 < num_entries; index += 2) {
7431 int32_t compare_to_value = lower_bound + index + 1;
7432 __ cmpl(value_reg, Immediate(compare_to_value));
7433 // Jump to successors[index] if value < case_value[index].
7434 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
7435 // Jump to successors[index + 1] if value == case_value[index + 1].
7436 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
7437 }
7438
7439 if (index != num_entries) {
7440 // There are an odd number of entries. Handle the last one.
7441 DCHECK_EQ(index + 1, num_entries);
7442 __ cmpl(value_reg, Immediate(lower_bound + index));
7443 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007444 }
7445
7446 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007447 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
7448 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007449 }
7450}
7451
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007452void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7453 int32_t lower_bound = switch_instr->GetStartValue();
7454 uint32_t num_entries = switch_instr->GetNumEntries();
7455 LocationSummary* locations = switch_instr->GetLocations();
7456 Register value_reg = locations->InAt(0).AsRegister<Register>();
7457
7458 GenPackedSwitchWithCompares(value_reg,
7459 lower_bound,
7460 num_entries,
7461 switch_instr->GetBlock(),
7462 switch_instr->GetDefaultBlock());
7463}
7464
Mark Mendell805b3b52015-09-18 14:10:29 -04007465void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7466 LocationSummary* locations =
7467 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7468 locations->SetInAt(0, Location::RequiresRegister());
7469
7470 // Constant area pointer.
7471 locations->SetInAt(1, Location::RequiresRegister());
7472
7473 // And the temporary we need.
7474 locations->AddTemp(Location::RequiresRegister());
7475}
7476
7477void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7478 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007479 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04007480 LocationSummary* locations = switch_instr->GetLocations();
7481 Register value_reg = locations->InAt(0).AsRegister<Register>();
7482 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7483
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007484 if (num_entries <= kPackedSwitchJumpTableThreshold) {
7485 GenPackedSwitchWithCompares(value_reg,
7486 lower_bound,
7487 num_entries,
7488 switch_instr->GetBlock(),
7489 default_block);
7490 return;
7491 }
7492
Mark Mendell805b3b52015-09-18 14:10:29 -04007493 // Optimizing has a jump area.
7494 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7495 Register constant_area = locations->InAt(1).AsRegister<Register>();
7496
7497 // Remove the bias, if needed.
7498 if (lower_bound != 0) {
7499 __ leal(temp_reg, Address(value_reg, -lower_bound));
7500 value_reg = temp_reg;
7501 }
7502
7503 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007504 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04007505 __ cmpl(value_reg, Immediate(num_entries - 1));
7506 __ j(kAbove, codegen_->GetLabelOf(default_block));
7507
7508 // We are in the range of the table.
7509 // Load (target-constant_area) from the jump table, indexing by the value.
7510 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
7511
7512 // Compute the actual target address by adding in constant_area.
7513 __ addl(temp_reg, constant_area);
7514
7515 // And jump.
7516 __ jmp(temp_reg);
7517}
7518
Mark Mendell0616ae02015-04-17 12:49:27 -04007519void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
7520 HX86ComputeBaseMethodAddress* insn) {
7521 LocationSummary* locations =
7522 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7523 locations->SetOut(Location::RequiresRegister());
7524}
7525
7526void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
7527 HX86ComputeBaseMethodAddress* insn) {
7528 LocationSummary* locations = insn->GetLocations();
7529 Register reg = locations->Out().AsRegister<Register>();
7530
7531 // Generate call to next instruction.
7532 Label next_instruction;
7533 __ call(&next_instruction);
7534 __ Bind(&next_instruction);
7535
7536 // Remember this offset for later use with constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007537 codegen_->AddMethodAddressOffset(insn, GetAssembler()->CodeSize());
Mark Mendell0616ae02015-04-17 12:49:27 -04007538
7539 // Grab the return address off the stack.
7540 __ popl(reg);
7541}
7542
7543void LocationsBuilderX86::VisitX86LoadFromConstantTable(
7544 HX86LoadFromConstantTable* insn) {
7545 LocationSummary* locations =
7546 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7547
7548 locations->SetInAt(0, Location::RequiresRegister());
7549 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
7550
7551 // If we don't need to be materialized, we only need the inputs to be set.
David Brazdilb3e773e2016-01-26 11:28:37 +00007552 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007553 return;
7554 }
7555
7556 switch (insn->GetType()) {
7557 case Primitive::kPrimFloat:
7558 case Primitive::kPrimDouble:
7559 locations->SetOut(Location::RequiresFpuRegister());
7560 break;
7561
7562 case Primitive::kPrimInt:
7563 locations->SetOut(Location::RequiresRegister());
7564 break;
7565
7566 default:
7567 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7568 }
7569}
7570
7571void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
David Brazdilb3e773e2016-01-26 11:28:37 +00007572 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007573 return;
7574 }
7575
7576 LocationSummary* locations = insn->GetLocations();
7577 Location out = locations->Out();
7578 Register const_area = locations->InAt(0).AsRegister<Register>();
7579 HConstant *value = insn->GetConstant();
7580
7581 switch (insn->GetType()) {
7582 case Primitive::kPrimFloat:
7583 __ movss(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007584 codegen_->LiteralFloatAddress(
7585 value->AsFloatConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04007586 break;
7587
7588 case Primitive::kPrimDouble:
7589 __ movsd(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007590 codegen_->LiteralDoubleAddress(
7591 value->AsDoubleConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04007592 break;
7593
7594 case Primitive::kPrimInt:
7595 __ movl(out.AsRegister<Register>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007596 codegen_->LiteralInt32Address(
7597 value->AsIntConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04007598 break;
7599
7600 default:
7601 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7602 }
7603}
7604
Mark Mendell0616ae02015-04-17 12:49:27 -04007605/**
7606 * Class to handle late fixup of offsets into constant area.
7607 */
Vladimir Marko5233f932015-09-29 19:01:15 +01007608class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04007609 public:
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007610 RIPFixup(CodeGeneratorX86& codegen,
7611 HX86ComputeBaseMethodAddress* base_method_address,
7612 size_t offset)
7613 : codegen_(&codegen),
7614 base_method_address_(base_method_address),
7615 offset_into_constant_area_(offset) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04007616
7617 protected:
7618 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
7619
7620 CodeGeneratorX86* codegen_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007621 HX86ComputeBaseMethodAddress* base_method_address_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007622
7623 private:
7624 void Process(const MemoryRegion& region, int pos) OVERRIDE {
7625 // Patch the correct offset for the instruction. The place to patch is the
7626 // last 4 bytes of the instruction.
7627 // The value to patch is the distance from the offset in the constant area
7628 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04007629 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007630 int32_t relative_position =
7631 constant_offset - codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell0616ae02015-04-17 12:49:27 -04007632
7633 // Patch in the right value.
7634 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
7635 }
7636
Mark Mendell0616ae02015-04-17 12:49:27 -04007637 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04007638 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007639};
7640
Mark Mendell805b3b52015-09-18 14:10:29 -04007641/**
7642 * Class to handle late fixup of offsets to a jump table that will be created in the
7643 * constant area.
7644 */
7645class JumpTableRIPFixup : public RIPFixup {
7646 public:
7647 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007648 : RIPFixup(codegen, switch_instr->GetBaseMethodAddress(), static_cast<size_t>(-1)),
7649 switch_instr_(switch_instr) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04007650
7651 void CreateJumpTable() {
7652 X86Assembler* assembler = codegen_->GetAssembler();
7653
7654 // Ensure that the reference to the jump table has the correct offset.
7655 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
7656 SetOffset(offset_in_constant_table);
7657
7658 // The label values in the jump table are computed relative to the
7659 // instruction addressing the constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007660 const int32_t relative_offset = codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell805b3b52015-09-18 14:10:29 -04007661
7662 // Populate the jump table with the correct values for the jump table.
7663 int32_t num_entries = switch_instr_->GetNumEntries();
7664 HBasicBlock* block = switch_instr_->GetBlock();
7665 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
7666 // The value that we want is the target offset - the position of the table.
7667 for (int32_t i = 0; i < num_entries; i++) {
7668 HBasicBlock* b = successors[i];
7669 Label* l = codegen_->GetLabelOf(b);
7670 DCHECK(l->IsBound());
7671 int32_t offset_to_block = l->Position() - relative_offset;
7672 assembler->AppendInt32(offset_to_block);
7673 }
7674 }
7675
7676 private:
7677 const HX86PackedSwitch* switch_instr_;
7678};
7679
7680void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
7681 // Generate the constant area if needed.
7682 X86Assembler* assembler = GetAssembler();
7683 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
7684 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
7685 // byte values.
7686 assembler->Align(4, 0);
7687 constant_area_start_ = assembler->CodeSize();
7688
7689 // Populate any jump tables.
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007690 for (JumpTableRIPFixup* jump_table : fixups_to_jump_tables_) {
Mark Mendell805b3b52015-09-18 14:10:29 -04007691 jump_table->CreateJumpTable();
7692 }
7693
7694 // And now add the constant area to the generated code.
7695 assembler->AddConstantArea();
7696 }
7697
7698 // And finish up.
7699 CodeGenerator::Finalize(allocator);
7700}
7701
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007702Address CodeGeneratorX86::LiteralDoubleAddress(double v,
7703 HX86ComputeBaseMethodAddress* method_base,
7704 Register reg) {
7705 AssemblerFixup* fixup =
7706 new (GetGraph()->GetArena()) RIPFixup(*this, method_base, __ AddDouble(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04007707 return Address(reg, kDummy32BitOffset, fixup);
7708}
7709
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007710Address CodeGeneratorX86::LiteralFloatAddress(float v,
7711 HX86ComputeBaseMethodAddress* method_base,
7712 Register reg) {
7713 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, method_base, __ AddFloat(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04007714 return Address(reg, kDummy32BitOffset, fixup);
7715}
7716
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007717Address CodeGeneratorX86::LiteralInt32Address(int32_t v,
7718 HX86ComputeBaseMethodAddress* method_base,
7719 Register reg) {
7720 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, method_base, __ AddInt32(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04007721 return Address(reg, kDummy32BitOffset, fixup);
7722}
7723
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007724Address CodeGeneratorX86::LiteralInt64Address(int64_t v,
7725 HX86ComputeBaseMethodAddress* method_base,
7726 Register reg) {
7727 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, method_base, __ AddInt64(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04007728 return Address(reg, kDummy32BitOffset, fixup);
7729}
7730
Aart Bika19616e2016-02-01 18:57:58 -08007731void CodeGeneratorX86::Load32BitValue(Register dest, int32_t value) {
7732 if (value == 0) {
7733 __ xorl(dest, dest);
7734 } else {
7735 __ movl(dest, Immediate(value));
7736 }
7737}
7738
7739void CodeGeneratorX86::Compare32BitValue(Register dest, int32_t value) {
7740 if (value == 0) {
7741 __ testl(dest, dest);
7742 } else {
7743 __ cmpl(dest, Immediate(value));
7744 }
7745}
7746
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007747void CodeGeneratorX86::GenerateIntCompare(Location lhs, Location rhs) {
7748 Register lhs_reg = lhs.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07007749 GenerateIntCompare(lhs_reg, rhs);
7750}
7751
7752void CodeGeneratorX86::GenerateIntCompare(Register lhs, Location rhs) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007753 if (rhs.IsConstant()) {
7754 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
jessicahandojo4877b792016-09-08 19:49:13 -07007755 Compare32BitValue(lhs, value);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007756 } else if (rhs.IsStackSlot()) {
jessicahandojo4877b792016-09-08 19:49:13 -07007757 __ cmpl(lhs, Address(ESP, rhs.GetStackIndex()));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007758 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07007759 __ cmpl(lhs, rhs.AsRegister<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007760 }
7761}
7762
7763Address CodeGeneratorX86::ArrayAddress(Register obj,
7764 Location index,
7765 ScaleFactor scale,
7766 uint32_t data_offset) {
7767 return index.IsConstant() ?
7768 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << scale) + data_offset) :
7769 Address(obj, index.AsRegister<Register>(), scale, data_offset);
7770}
7771
Mark Mendell805b3b52015-09-18 14:10:29 -04007772Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
7773 Register reg,
7774 Register value) {
7775 // Create a fixup to be used to create and address the jump table.
7776 JumpTableRIPFixup* table_fixup =
7777 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
7778
7779 // We have to populate the jump tables.
7780 fixups_to_jump_tables_.push_back(table_fixup);
7781
7782 // We want a scaled address, as we are extracting the correct offset from the table.
7783 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
7784}
7785
Andreas Gampe85b62f22015-09-09 13:15:38 -07007786// TODO: target as memory.
7787void CodeGeneratorX86::MoveFromReturnRegister(Location target, Primitive::Type type) {
7788 if (!target.IsValid()) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007789 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007790 return;
7791 }
7792
7793 DCHECK_NE(type, Primitive::kPrimVoid);
7794
7795 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
7796 if (target.Equals(return_loc)) {
7797 return;
7798 }
7799
7800 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7801 // with the else branch.
7802 if (type == Primitive::kPrimLong) {
7803 HParallelMove parallel_move(GetGraph()->GetArena());
7804 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), Primitive::kPrimInt, nullptr);
7805 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), Primitive::kPrimInt, nullptr);
7806 GetMoveResolver()->EmitNativeCode(&parallel_move);
7807 } else {
7808 // Let the parallel move resolver take care of all of this.
7809 HParallelMove parallel_move(GetGraph()->GetArena());
7810 parallel_move.AddMove(return_loc, target, type, nullptr);
7811 GetMoveResolver()->EmitNativeCode(&parallel_move);
7812 }
7813}
7814
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007815void CodeGeneratorX86::PatchJitRootUse(uint8_t* code,
7816 const uint8_t* roots_data,
7817 const PatchInfo<Label>& info,
7818 uint64_t index_in_table) const {
7819 uint32_t code_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
7820 uintptr_t address =
7821 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
7822 typedef __attribute__((__aligned__(1))) uint32_t unaligned_uint32_t;
7823 reinterpret_cast<unaligned_uint32_t*>(code + code_offset)[0] =
7824 dchecked_integral_cast<uint32_t>(address);
7825}
7826
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007827void CodeGeneratorX86::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
7828 for (const PatchInfo<Label>& info : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007829 const auto it = jit_string_roots_.find(
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007830 StringReference(&info.dex_file, dex::StringIndex(info.index)));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007831 DCHECK(it != jit_string_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007832 uint64_t index_in_table = it->second;
7833 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007834 }
7835
7836 for (const PatchInfo<Label>& info : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007837 const auto it = jit_class_roots_.find(
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007838 TypeReference(&info.dex_file, dex::TypeIndex(info.index)));
7839 DCHECK(it != jit_class_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007840 uint64_t index_in_table = it->second;
7841 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007842 }
7843}
7844
Roland Levillain4d027112015-07-01 15:41:14 +01007845#undef __
7846
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007847} // namespace x86
7848} // namespace art