blob: 0b9130fa5a54c36ed1d946fd7f5b79fa979e2f28 [file] [log] [blame]
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_x86.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010018
Mathieu Chartiere401d142015-04-22 13:56:20 -070019#include "art_method.h"
Vladimir Marko94ec2db2017-09-06 17:21:03 +010020#include "class_table.h"
Guillaume Sanchez0f88e872015-03-30 17:55:45 +010021#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000022#include "compiled_method.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010023#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000024#include "entrypoints/quick/quick_entrypoints_enum.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010025#include "gc/accounting/card_table.h"
Andreas Gampe09659c22017-09-18 18:23:32 -070026#include "heap_poisoning.h"
Mark Mendell09ed1a32015-03-25 08:30:06 -040027#include "intrinsics.h"
28#include "intrinsics_x86.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070029#include "lock_word.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070030#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070031#include "mirror/class-inl.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010032#include "thread.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000033#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010034#include "utils/stack_checks.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000035#include "utils/x86/assembler_x86.h"
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010036#include "utils/x86/managed_register_x86.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000037
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000038namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010039
Roland Levillain0d5a2812015-11-13 10:07:31 +000040template<class MirrorType>
41class GcRoot;
42
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000043namespace x86 {
44
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010045static constexpr int kCurrentMethodStackOffset = 0;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010046static constexpr Register kMethodRegisterArgument = EAX;
Mark Mendell5f874182015-03-04 15:42:45 -050047static constexpr Register kCoreCalleeSaves[] = { EBP, ESI, EDI };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010048
Mark Mendell24f2dfa2015-01-14 19:51:45 -050049static constexpr int kC2ConditionMask = 0x400;
50
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000051static constexpr int kFakeReturnRegister = Register(8);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +000052
Roland Levillain7cbd27f2016-08-11 23:53:33 +010053// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
54#define __ down_cast<X86Assembler*>(codegen->GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -070055#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kX86PointerSize, x).Int32Value()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010056
Andreas Gampe85b62f22015-09-09 13:15:38 -070057class NullCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010058 public:
David Srbecky9cd6d372016-02-09 15:24:47 +000059 explicit NullCheckSlowPathX86(HNullCheck* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010060
Alexandre Rames2ed20af2015-03-06 13:55:35 +000061 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +010062 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010063 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000064 if (instruction_->CanThrowIntoCatchBlock()) {
65 // Live registers will be restored in the catch block if caught.
66 SaveLiveRegisters(codegen, instruction_->GetLocations());
67 }
Serban Constantinescuba45db02016-07-12 22:53:02 +010068 x86_codegen->InvokeRuntime(kQuickThrowNullPointer,
Alexandre Rames8158f282015-08-07 10:26:17 +010069 instruction_,
70 instruction_->GetDexPc(),
71 this);
Roland Levillain888d0672015-11-23 18:53:50 +000072 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +010073 }
74
Alexandre Rames8158f282015-08-07 10:26:17 +010075 bool IsFatal() const OVERRIDE { return true; }
76
Alexandre Rames9931f312015-06-19 14:47:01 +010077 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathX86"; }
78
Nicolas Geoffraye5038322014-07-04 09:41:32 +010079 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +010080 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86);
81};
82
Andreas Gampe85b62f22015-09-09 13:15:38 -070083class DivZeroCheckSlowPathX86 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +000084 public:
David Srbecky9cd6d372016-02-09 15:24:47 +000085 explicit DivZeroCheckSlowPathX86(HDivZeroCheck* instruction) : SlowPathCode(instruction) {}
Calin Juravled0d48522014-11-04 16:40:20 +000086
Alexandre Rames2ed20af2015-03-06 13:55:35 +000087 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +010088 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Calin Juravled0d48522014-11-04 16:40:20 +000089 __ Bind(GetEntryLabel());
Serban Constantinescuba45db02016-07-12 22:53:02 +010090 x86_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +000091 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Calin Juravled0d48522014-11-04 16:40:20 +000092 }
93
Alexandre Rames8158f282015-08-07 10:26:17 +010094 bool IsFatal() const OVERRIDE { return true; }
95
Alexandre Rames9931f312015-06-19 14:47:01 +010096 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathX86"; }
97
Calin Juravled0d48522014-11-04 16:40:20 +000098 private:
Calin Juravled0d48522014-11-04 16:40:20 +000099 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86);
100};
101
Andreas Gampe85b62f22015-09-09 13:15:38 -0700102class DivRemMinusOneSlowPathX86 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +0000103 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000104 DivRemMinusOneSlowPathX86(HInstruction* instruction, Register reg, bool is_div)
105 : SlowPathCode(instruction), reg_(reg), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000106
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000107 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +0000108 __ Bind(GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +0000109 if (is_div_) {
110 __ negl(reg_);
111 } else {
112 __ movl(reg_, Immediate(0));
113 }
Calin Juravled0d48522014-11-04 16:40:20 +0000114 __ jmp(GetExitLabel());
115 }
116
Alexandre Rames9931f312015-06-19 14:47:01 +0100117 const char* GetDescription() const OVERRIDE { return "DivRemMinusOneSlowPathX86"; }
118
Calin Juravled0d48522014-11-04 16:40:20 +0000119 private:
120 Register reg_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000121 bool is_div_;
122 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86);
Calin Juravled0d48522014-11-04 16:40:20 +0000123};
124
Andreas Gampe85b62f22015-09-09 13:15:38 -0700125class BoundsCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100126 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000127 explicit BoundsCheckSlowPathX86(HBoundsCheck* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100128
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000129 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100130 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100131 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100132 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000133 // We're moving two locations to locations that could overlap, so we need a parallel
134 // move resolver.
David Brazdil77a48ae2015-09-15 12:34:04 +0000135 if (instruction_->CanThrowIntoCatchBlock()) {
136 // Live registers will be restored in the catch block if caught.
137 SaveLiveRegisters(codegen, instruction_->GetLocations());
138 }
Mark Mendellee8d9712016-07-12 11:13:15 -0400139
140 // Are we using an array length from memory?
141 HInstruction* array_length = instruction_->InputAt(1);
142 Location length_loc = locations->InAt(1);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100143 InvokeRuntimeCallingConvention calling_convention;
Mark Mendellee8d9712016-07-12 11:13:15 -0400144 if (array_length->IsArrayLength() && array_length->IsEmittedAtUseSite()) {
145 // Load the array length into our temporary.
146 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
147 Location array_loc = array_length->GetLocations()->InAt(0);
148 Address array_len(array_loc.AsRegister<Register>(), len_offset);
149 length_loc = Location::RegisterLocation(calling_convention.GetRegisterAt(1));
150 // Check for conflicts with index.
151 if (length_loc.Equals(locations->InAt(0))) {
152 // We know we aren't using parameter 2.
153 length_loc = Location::RegisterLocation(calling_convention.GetRegisterAt(2));
154 }
155 __ movl(length_loc.AsRegister<Register>(), array_len);
jessicahandojo4877b792016-09-08 19:49:13 -0700156 if (mirror::kUseStringCompression) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +0100157 __ shrl(length_loc.AsRegister<Register>(), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -0700158 }
Mark Mendellee8d9712016-07-12 11:13:15 -0400159 }
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000160 x86_codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100161 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000162 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100163 Primitive::kPrimInt,
Mark Mendellee8d9712016-07-12 11:13:15 -0400164 length_loc,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100165 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
166 Primitive::kPrimInt);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100167 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
168 ? kQuickThrowStringBounds
169 : kQuickThrowArrayBounds;
170 x86_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100171 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Roland Levillain888d0672015-11-23 18:53:50 +0000172 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100173 }
174
Alexandre Rames8158f282015-08-07 10:26:17 +0100175 bool IsFatal() const OVERRIDE { return true; }
176
Alexandre Rames9931f312015-06-19 14:47:01 +0100177 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathX86"; }
178
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100179 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100180 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
181};
182
Andreas Gampe85b62f22015-09-09 13:15:38 -0700183class SuspendCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000184 public:
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000185 SuspendCheckSlowPathX86(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000186 : SlowPathCode(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000187
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000188 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bikb13c65b2017-03-21 20:14:07 -0700189 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100190 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000191 __ Bind(GetEntryLabel());
Aart Bik24b905f2017-04-06 09:59:06 -0700192 SaveLiveRegisters(codegen, locations); // Only saves full width XMM for SIMD.
Serban Constantinescuba45db02016-07-12 22:53:02 +0100193 x86_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000194 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Aart Bik24b905f2017-04-06 09:59:06 -0700195 RestoreLiveRegisters(codegen, locations); // Only restores full width XMM for SIMD.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100196 if (successor_ == nullptr) {
197 __ jmp(GetReturnLabel());
198 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100199 __ jmp(x86_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100200 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000201 }
202
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100203 Label* GetReturnLabel() {
204 DCHECK(successor_ == nullptr);
205 return &return_label_;
206 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000207
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100208 HBasicBlock* GetSuccessor() const {
209 return successor_;
210 }
211
Alexandre Rames9931f312015-06-19 14:47:01 +0100212 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathX86"; }
213
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000214 private:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100215 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000216 Label return_label_;
217
218 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
219};
220
Vladimir Markoaad75c62016-10-03 08:46:48 +0000221class LoadStringSlowPathX86 : public SlowPathCode {
222 public:
223 explicit LoadStringSlowPathX86(HLoadString* instruction): SlowPathCode(instruction) {}
224
225 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
226 LocationSummary* locations = instruction_->GetLocations();
227 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
228
229 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
230 __ Bind(GetEntryLabel());
231 SaveLiveRegisters(codegen, locations);
232
233 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000234 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
235 __ movl(calling_convention.GetRegisterAt(0), Immediate(string_index.index_));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000236 x86_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
237 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
238 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
239 RestoreLiveRegisters(codegen, locations);
240
241 // Store the resolved String to the BSS entry.
242 Register method_address = locations->InAt(0).AsRegister<Register>();
243 __ movl(Address(method_address, CodeGeneratorX86::kDummy32BitOffset),
244 locations->Out().AsRegister<Register>());
245 Label* fixup_label = x86_codegen->NewStringBssEntryPatch(instruction_->AsLoadString());
246 __ Bind(fixup_label);
247
248 __ jmp(GetExitLabel());
249 }
250
251 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathX86"; }
252
253 private:
254 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86);
255};
256
Andreas Gampe85b62f22015-09-09 13:15:38 -0700257class LoadClassSlowPathX86 : public SlowPathCode {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000258 public:
259 LoadClassSlowPathX86(HLoadClass* cls,
260 HInstruction* at,
261 uint32_t dex_pc,
262 bool do_clinit)
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000263 : SlowPathCode(at), cls_(cls), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000264 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
265 }
266
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000267 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000268 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000269 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
270 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000271 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000272
273 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000274 dex::TypeIndex type_index = cls_->GetTypeIndex();
275 __ movl(calling_convention.GetRegisterAt(0), Immediate(type_index.index_));
Serban Constantinescuba45db02016-07-12 22:53:02 +0100276 x86_codegen->InvokeRuntime(do_clinit_ ? kQuickInitializeStaticStorage
277 : kQuickInitializeType,
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000278 instruction_,
279 dex_pc_,
280 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000281 if (do_clinit_) {
282 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
283 } else {
284 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
285 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000286
287 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000288 Location out = locations->Out();
289 if (out.IsValid()) {
290 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
291 x86_codegen->Move32(out, Location::RegisterLocation(EAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000292 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000293 RestoreLiveRegisters(codegen, locations);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000294 // For HLoadClass/kBssEntry, store the resolved Class to the BSS entry.
295 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
296 if (cls_ == instruction_ && cls_->GetLoadKind() == HLoadClass::LoadKind::kBssEntry) {
297 DCHECK(out.IsValid());
298 Register method_address = locations->InAt(0).AsRegister<Register>();
299 __ movl(Address(method_address, CodeGeneratorX86::kDummy32BitOffset),
300 locations->Out().AsRegister<Register>());
301 Label* fixup_label = x86_codegen->NewTypeBssEntryPatch(cls_);
302 __ Bind(fixup_label);
303 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000304 __ jmp(GetExitLabel());
305 }
306
Alexandre Rames9931f312015-06-19 14:47:01 +0100307 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathX86"; }
308
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000309 private:
310 // The class this slow path will load.
311 HLoadClass* const cls_;
312
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000313 // The dex PC of `at_`.
314 const uint32_t dex_pc_;
315
316 // Whether to initialize the class.
317 const bool do_clinit_;
318
319 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
320};
321
Andreas Gampe85b62f22015-09-09 13:15:38 -0700322class TypeCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000323 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000324 TypeCheckSlowPathX86(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000325 : SlowPathCode(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000326
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000327 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000328 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000329 DCHECK(instruction_->IsCheckCast()
330 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000331
332 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
333 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000334
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000335 if (!is_fatal_) {
336 SaveLiveRegisters(codegen, locations);
337 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000338
339 // We're moving two locations to locations that could overlap, so we need a parallel
340 // move resolver.
341 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800342 x86_codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800343 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
344 Primitive::kPrimNot,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800345 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800346 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
347 Primitive::kPrimNot);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000348 if (instruction_->IsInstanceOf()) {
Serban Constantinescuba45db02016-07-12 22:53:02 +0100349 x86_codegen->InvokeRuntime(kQuickInstanceofNonTrivial,
Alexandre Rames8158f282015-08-07 10:26:17 +0100350 instruction_,
351 instruction_->GetDexPc(),
352 this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800353 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000354 } else {
355 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800356 x86_codegen->InvokeRuntime(kQuickCheckInstanceOf,
357 instruction_,
358 instruction_->GetDexPc(),
359 this);
360 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000361 }
362
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000363 if (!is_fatal_) {
364 if (instruction_->IsInstanceOf()) {
365 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
366 }
367 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray75374372015-09-17 17:12:19 +0000368
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000369 __ jmp(GetExitLabel());
370 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000371 }
372
Alexandre Rames9931f312015-06-19 14:47:01 +0100373 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathX86"; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000374 bool IsFatal() const OVERRIDE { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100375
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000376 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000377 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000378
379 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
380};
381
Andreas Gampe85b62f22015-09-09 13:15:38 -0700382class DeoptimizationSlowPathX86 : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700383 public:
Aart Bik42249c32016-01-07 15:33:50 -0800384 explicit DeoptimizationSlowPathX86(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000385 : SlowPathCode(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700386
387 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +0100388 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700389 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100390 LocationSummary* locations = instruction_->GetLocations();
391 SaveLiveRegisters(codegen, locations);
392 InvokeRuntimeCallingConvention calling_convention;
393 x86_codegen->Load32BitValue(
394 calling_convention.GetRegisterAt(0),
395 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescuba45db02016-07-12 22:53:02 +0100396 x86_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100397 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700398 }
399
Alexandre Rames9931f312015-06-19 14:47:01 +0100400 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86"; }
401
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700402 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700403 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86);
404};
405
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100406class ArraySetSlowPathX86 : public SlowPathCode {
407 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000408 explicit ArraySetSlowPathX86(HInstruction* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100409
410 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
411 LocationSummary* locations = instruction_->GetLocations();
412 __ Bind(GetEntryLabel());
413 SaveLiveRegisters(codegen, locations);
414
415 InvokeRuntimeCallingConvention calling_convention;
416 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
417 parallel_move.AddMove(
418 locations->InAt(0),
419 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
420 Primitive::kPrimNot,
421 nullptr);
422 parallel_move.AddMove(
423 locations->InAt(1),
424 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
425 Primitive::kPrimInt,
426 nullptr);
427 parallel_move.AddMove(
428 locations->InAt(2),
429 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
430 Primitive::kPrimNot,
431 nullptr);
432 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
433
434 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100435 x86_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000436 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100437 RestoreLiveRegisters(codegen, locations);
438 __ jmp(GetExitLabel());
439 }
440
441 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathX86"; }
442
443 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100444 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathX86);
445};
446
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100447// Slow path marking an object reference `ref` during a read
448// barrier. The field `obj.field` in the object `obj` holding this
449// reference does not get updated by this slow path after marking (see
450// ReadBarrierMarkAndUpdateFieldSlowPathX86 below for that).
451//
452// This means that after the execution of this slow path, `ref` will
453// always be up-to-date, but `obj.field` may not; i.e., after the
454// flip, `ref` will be a to-space reference, but `obj.field` will
455// probably still be a from-space reference (unless it gets updated by
456// another thread, or if another thread installed another object
457// reference (different from `ref`) in `obj.field`).
Roland Levillain7c1559a2015-12-15 10:55:36 +0000458class ReadBarrierMarkSlowPathX86 : public SlowPathCode {
459 public:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100460 ReadBarrierMarkSlowPathX86(HInstruction* instruction,
461 Location ref,
462 bool unpoison_ref_before_marking)
463 : SlowPathCode(instruction),
464 ref_(ref),
465 unpoison_ref_before_marking_(unpoison_ref_before_marking) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000466 DCHECK(kEmitCompilerReadBarrier);
467 }
468
469 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathX86"; }
470
471 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
472 LocationSummary* locations = instruction_->GetLocations();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100473 Register ref_reg = ref_.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +0000474 DCHECK(locations->CanCall());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100475 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
Roland Levillain7c1559a2015-12-15 10:55:36 +0000476 DCHECK(instruction_->IsInstanceFieldGet() ||
477 instruction_->IsStaticFieldGet() ||
478 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100479 instruction_->IsArraySet() ||
Roland Levillain7c1559a2015-12-15 10:55:36 +0000480 instruction_->IsLoadClass() ||
481 instruction_->IsLoadString() ||
482 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100483 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100484 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
485 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain7c1559a2015-12-15 10:55:36 +0000486 << "Unexpected instruction in read barrier marking slow path: "
487 << instruction_->DebugName();
488
489 __ Bind(GetEntryLabel());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100490 if (unpoison_ref_before_marking_) {
Vladimir Marko953437b2016-08-24 08:30:46 +0000491 // Object* ref = ref_addr->AsMirrorPtr()
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100492 __ MaybeUnpoisonHeapReference(ref_reg);
Vladimir Marko953437b2016-08-24 08:30:46 +0000493 }
Roland Levillain4359e612016-07-20 11:32:19 +0100494 // No need to save live registers; it's taken care of by the
495 // entrypoint. Also, there is no need to update the stack mask,
496 // as this runtime call will not trigger a garbage collection.
Roland Levillain7c1559a2015-12-15 10:55:36 +0000497 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100498 DCHECK_NE(ref_reg, ESP);
499 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCpuRegisters) << ref_reg;
Roland Levillain02b75802016-07-13 11:54:35 +0100500 // "Compact" slow path, saving two moves.
501 //
502 // Instead of using the standard runtime calling convention (input
503 // and output in EAX):
504 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100505 // EAX <- ref
Roland Levillain02b75802016-07-13 11:54:35 +0100506 // EAX <- ReadBarrierMark(EAX)
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100507 // ref <- EAX
Roland Levillain02b75802016-07-13 11:54:35 +0100508 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100509 // we just use rX (the register containing `ref`) as input and output
Roland Levillain02b75802016-07-13 11:54:35 +0100510 // of a dedicated entrypoint:
511 //
512 // rX <- ReadBarrierMarkRegX(rX)
513 //
Roland Levillain97c46462017-05-11 14:04:03 +0100514 int32_t entry_point_offset = Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(ref_reg);
Roland Levillaindec8f632016-07-22 17:10:06 +0100515 // This runtime call does not require a stack map.
516 x86_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillain7c1559a2015-12-15 10:55:36 +0000517 __ jmp(GetExitLabel());
518 }
519
520 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100521 // The location (register) of the marked object reference.
522 const Location ref_;
523 // Should the reference in `ref_` be unpoisoned prior to marking it?
524 const bool unpoison_ref_before_marking_;
Roland Levillain7c1559a2015-12-15 10:55:36 +0000525
526 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86);
527};
528
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100529// Slow path marking an object reference `ref` during a read barrier,
530// and if needed, atomically updating the field `obj.field` in the
531// object `obj` holding this reference after marking (contrary to
532// ReadBarrierMarkSlowPathX86 above, which never tries to update
533// `obj.field`).
534//
535// This means that after the execution of this slow path, both `ref`
536// and `obj.field` will be up-to-date; i.e., after the flip, both will
537// hold the same to-space reference (unless another thread installed
538// another object reference (different from `ref`) in `obj.field`).
539class ReadBarrierMarkAndUpdateFieldSlowPathX86 : public SlowPathCode {
540 public:
541 ReadBarrierMarkAndUpdateFieldSlowPathX86(HInstruction* instruction,
542 Location ref,
543 Register obj,
544 const Address& field_addr,
545 bool unpoison_ref_before_marking,
546 Register temp)
547 : SlowPathCode(instruction),
548 ref_(ref),
549 obj_(obj),
550 field_addr_(field_addr),
551 unpoison_ref_before_marking_(unpoison_ref_before_marking),
552 temp_(temp) {
553 DCHECK(kEmitCompilerReadBarrier);
554 }
555
556 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkAndUpdateFieldSlowPathX86"; }
557
558 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
559 LocationSummary* locations = instruction_->GetLocations();
560 Register ref_reg = ref_.AsRegister<Register>();
561 DCHECK(locations->CanCall());
562 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
563 // This slow path is only used by the UnsafeCASObject intrinsic.
564 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
565 << "Unexpected instruction in read barrier marking and field updating slow path: "
566 << instruction_->DebugName();
567 DCHECK(instruction_->GetLocations()->Intrinsified());
568 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
569
570 __ Bind(GetEntryLabel());
571 if (unpoison_ref_before_marking_) {
572 // Object* ref = ref_addr->AsMirrorPtr()
573 __ MaybeUnpoisonHeapReference(ref_reg);
574 }
575
576 // Save the old (unpoisoned) reference.
577 __ movl(temp_, ref_reg);
578
579 // No need to save live registers; it's taken care of by the
580 // entrypoint. Also, there is no need to update the stack mask,
581 // as this runtime call will not trigger a garbage collection.
582 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
583 DCHECK_NE(ref_reg, ESP);
584 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCpuRegisters) << ref_reg;
585 // "Compact" slow path, saving two moves.
586 //
587 // Instead of using the standard runtime calling convention (input
588 // and output in EAX):
589 //
590 // EAX <- ref
591 // EAX <- ReadBarrierMark(EAX)
592 // ref <- EAX
593 //
594 // we just use rX (the register containing `ref`) as input and output
595 // of a dedicated entrypoint:
596 //
597 // rX <- ReadBarrierMarkRegX(rX)
598 //
Roland Levillain97c46462017-05-11 14:04:03 +0100599 int32_t entry_point_offset = Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(ref_reg);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100600 // This runtime call does not require a stack map.
601 x86_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
602
603 // If the new reference is different from the old reference,
604 // update the field in the holder (`*field_addr`).
605 //
606 // Note that this field could also hold a different object, if
607 // another thread had concurrently changed it. In that case, the
608 // LOCK CMPXCHGL instruction in the compare-and-set (CAS)
609 // operation below would abort the CAS, leaving the field as-is.
610 NearLabel done;
611 __ cmpl(temp_, ref_reg);
612 __ j(kEqual, &done);
613
614 // Update the the holder's field atomically. This may fail if
615 // mutator updates before us, but it's OK. This is achieved
616 // using a strong compare-and-set (CAS) operation with relaxed
617 // memory synchronization ordering, where the expected value is
618 // the old reference and the desired value is the new reference.
619 // This operation is implemented with a 32-bit LOCK CMPXLCHG
620 // instruction, which requires the expected value (the old
621 // reference) to be in EAX. Save EAX beforehand, and move the
622 // expected value (stored in `temp_`) into EAX.
623 __ pushl(EAX);
624 __ movl(EAX, temp_);
625
626 // Convenience aliases.
627 Register base = obj_;
628 Register expected = EAX;
629 Register value = ref_reg;
630
631 bool base_equals_value = (base == value);
632 if (kPoisonHeapReferences) {
633 if (base_equals_value) {
634 // If `base` and `value` are the same register location, move
635 // `value` to a temporary register. This way, poisoning
636 // `value` won't invalidate `base`.
637 value = temp_;
638 __ movl(value, base);
639 }
640
641 // Check that the register allocator did not assign the location
642 // of `expected` (EAX) to `value` nor to `base`, so that heap
643 // poisoning (when enabled) works as intended below.
644 // - If `value` were equal to `expected`, both references would
645 // be poisoned twice, meaning they would not be poisoned at
646 // all, as heap poisoning uses address negation.
647 // - If `base` were equal to `expected`, poisoning `expected`
648 // would invalidate `base`.
649 DCHECK_NE(value, expected);
650 DCHECK_NE(base, expected);
651
652 __ PoisonHeapReference(expected);
653 __ PoisonHeapReference(value);
654 }
655
656 __ LockCmpxchgl(field_addr_, value);
657
658 // If heap poisoning is enabled, we need to unpoison the values
659 // that were poisoned earlier.
660 if (kPoisonHeapReferences) {
661 if (base_equals_value) {
662 // `value` has been moved to a temporary register, no need
663 // to unpoison it.
664 } else {
665 __ UnpoisonHeapReference(value);
666 }
667 // No need to unpoison `expected` (EAX), as it is be overwritten below.
668 }
669
670 // Restore EAX.
671 __ popl(EAX);
672
673 __ Bind(&done);
674 __ jmp(GetExitLabel());
675 }
676
677 private:
678 // The location (register) of the marked object reference.
679 const Location ref_;
680 // The register containing the object holding the marked object reference field.
681 const Register obj_;
682 // The address of the marked reference field. The base of this address must be `obj_`.
683 const Address field_addr_;
684
685 // Should the reference in `ref_` be unpoisoned prior to marking it?
686 const bool unpoison_ref_before_marking_;
687
688 const Register temp_;
689
690 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathX86);
691};
692
Roland Levillain0d5a2812015-11-13 10:07:31 +0000693// Slow path generating a read barrier for a heap reference.
694class ReadBarrierForHeapReferenceSlowPathX86 : public SlowPathCode {
695 public:
696 ReadBarrierForHeapReferenceSlowPathX86(HInstruction* instruction,
697 Location out,
698 Location ref,
699 Location obj,
700 uint32_t offset,
701 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000702 : SlowPathCode(instruction),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000703 out_(out),
704 ref_(ref),
705 obj_(obj),
706 offset_(offset),
707 index_(index) {
708 DCHECK(kEmitCompilerReadBarrier);
709 // If `obj` is equal to `out` or `ref`, it means the initial object
710 // has been overwritten by (or after) the heap object reference load
711 // to be instrumented, e.g.:
712 //
713 // __ movl(out, Address(out, offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000714 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000715 //
716 // In that case, we have lost the information about the original
717 // object, and the emitted read barrier cannot work properly.
718 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
719 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
720 }
721
722 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
723 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
724 LocationSummary* locations = instruction_->GetLocations();
725 Register reg_out = out_.AsRegister<Register>();
726 DCHECK(locations->CanCall());
727 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain3d312422016-06-23 13:53:42 +0100728 DCHECK(instruction_->IsInstanceFieldGet() ||
729 instruction_->IsStaticFieldGet() ||
730 instruction_->IsArrayGet() ||
731 instruction_->IsInstanceOf() ||
732 instruction_->IsCheckCast() ||
Andreas Gamped9911ee2017-03-27 13:27:24 -0700733 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain7c1559a2015-12-15 10:55:36 +0000734 << "Unexpected instruction in read barrier for heap reference slow path: "
735 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000736
737 __ Bind(GetEntryLabel());
738 SaveLiveRegisters(codegen, locations);
739
740 // We may have to change the index's value, but as `index_` is a
741 // constant member (like other "inputs" of this slow path),
742 // introduce a copy of it, `index`.
743 Location index = index_;
744 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100745 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain0d5a2812015-11-13 10:07:31 +0000746 if (instruction_->IsArrayGet()) {
747 // Compute the actual memory offset and store it in `index`.
748 Register index_reg = index_.AsRegister<Register>();
749 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
750 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
751 // We are about to change the value of `index_reg` (see the
752 // calls to art::x86::X86Assembler::shll and
753 // art::x86::X86Assembler::AddImmediate below), but it has
754 // not been saved by the previous call to
755 // art::SlowPathCode::SaveLiveRegisters, as it is a
756 // callee-save register --
757 // art::SlowPathCode::SaveLiveRegisters does not consider
758 // callee-save registers, as it has been designed with the
759 // assumption that callee-save registers are supposed to be
760 // handled by the called function. So, as a callee-save
761 // register, `index_reg` _would_ eventually be saved onto
762 // the stack, but it would be too late: we would have
763 // changed its value earlier. Therefore, we manually save
764 // it here into another freely available register,
765 // `free_reg`, chosen of course among the caller-save
766 // registers (as a callee-save `free_reg` register would
767 // exhibit the same problem).
768 //
769 // Note we could have requested a temporary register from
770 // the register allocator instead; but we prefer not to, as
771 // this is a slow path, and we know we can find a
772 // caller-save register that is available.
773 Register free_reg = FindAvailableCallerSaveRegister(codegen);
774 __ movl(free_reg, index_reg);
775 index_reg = free_reg;
776 index = Location::RegisterLocation(index_reg);
777 } else {
778 // The initial register stored in `index_` has already been
779 // saved in the call to art::SlowPathCode::SaveLiveRegisters
780 // (as it is not a callee-save register), so we can freely
781 // use it.
782 }
783 // Shifting the index value contained in `index_reg` by the scale
784 // factor (2) cannot overflow in practice, as the runtime is
785 // unable to allocate object arrays with a size larger than
786 // 2^26 - 1 (that is, 2^28 - 4 bytes).
787 __ shll(index_reg, Immediate(TIMES_4));
788 static_assert(
789 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
790 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
791 __ AddImmediate(index_reg, Immediate(offset_));
792 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100793 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
794 // intrinsics, `index_` is not shifted by a scale factor of 2
795 // (as in the case of ArrayGet), as it is actually an offset
796 // to an object field within an object.
797 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000798 DCHECK(instruction_->GetLocations()->Intrinsified());
799 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
800 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
801 << instruction_->AsInvoke()->GetIntrinsic();
802 DCHECK_EQ(offset_, 0U);
803 DCHECK(index_.IsRegisterPair());
804 // UnsafeGet's offset location is a register pair, the low
805 // part contains the correct offset.
806 index = index_.ToLow();
807 }
808 }
809
810 // We're moving two or three locations to locations that could
811 // overlap, so we need a parallel move resolver.
812 InvokeRuntimeCallingConvention calling_convention;
813 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
814 parallel_move.AddMove(ref_,
815 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
816 Primitive::kPrimNot,
817 nullptr);
818 parallel_move.AddMove(obj_,
819 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
820 Primitive::kPrimNot,
821 nullptr);
822 if (index.IsValid()) {
823 parallel_move.AddMove(index,
824 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
825 Primitive::kPrimInt,
826 nullptr);
827 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
828 } else {
829 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
830 __ movl(calling_convention.GetRegisterAt(2), Immediate(offset_));
831 }
Serban Constantinescuba45db02016-07-12 22:53:02 +0100832 x86_codegen->InvokeRuntime(kQuickReadBarrierSlow, instruction_, instruction_->GetDexPc(), this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000833 CheckEntrypointTypes<
834 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
835 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
836
837 RestoreLiveRegisters(codegen, locations);
838 __ jmp(GetExitLabel());
839 }
840
841 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathX86"; }
842
843 private:
844 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
845 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
846 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
847 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
848 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
849 return static_cast<Register>(i);
850 }
851 }
852 // We shall never fail to find a free caller-save register, as
853 // there are more than two core caller-save registers on x86
854 // (meaning it is possible to find one which is different from
855 // `ref` and `obj`).
856 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
857 LOG(FATAL) << "Could not find a free caller-save register";
858 UNREACHABLE();
859 }
860
Roland Levillain0d5a2812015-11-13 10:07:31 +0000861 const Location out_;
862 const Location ref_;
863 const Location obj_;
864 const uint32_t offset_;
865 // An additional location containing an index to an array.
866 // Only used for HArrayGet and the UnsafeGetObject &
867 // UnsafeGetObjectVolatile intrinsics.
868 const Location index_;
869
870 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86);
871};
872
873// Slow path generating a read barrier for a GC root.
874class ReadBarrierForRootSlowPathX86 : public SlowPathCode {
875 public:
876 ReadBarrierForRootSlowPathX86(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000877 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000878 DCHECK(kEmitCompilerReadBarrier);
879 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000880
881 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
882 LocationSummary* locations = instruction_->GetLocations();
883 Register reg_out = out_.AsRegister<Register>();
884 DCHECK(locations->CanCall());
885 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000886 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
887 << "Unexpected instruction in read barrier for GC root slow path: "
888 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000889
890 __ Bind(GetEntryLabel());
891 SaveLiveRegisters(codegen, locations);
892
893 InvokeRuntimeCallingConvention calling_convention;
894 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
895 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100896 x86_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000897 instruction_,
898 instruction_->GetDexPc(),
899 this);
900 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
901 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
902
903 RestoreLiveRegisters(codegen, locations);
904 __ jmp(GetExitLabel());
905 }
906
907 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathX86"; }
908
909 private:
Roland Levillain0d5a2812015-11-13 10:07:31 +0000910 const Location out_;
911 const Location root_;
912
913 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86);
914};
915
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100916#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100917// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
918#define __ down_cast<X86Assembler*>(GetAssembler())-> // NOLINT
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100919
Aart Bike9f37602015-10-09 11:15:55 -0700920inline Condition X86Condition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700921 switch (cond) {
922 case kCondEQ: return kEqual;
923 case kCondNE: return kNotEqual;
924 case kCondLT: return kLess;
925 case kCondLE: return kLessEqual;
926 case kCondGT: return kGreater;
927 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700928 case kCondB: return kBelow;
929 case kCondBE: return kBelowEqual;
930 case kCondA: return kAbove;
931 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700932 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100933 LOG(FATAL) << "Unreachable";
934 UNREACHABLE();
935}
936
Aart Bike9f37602015-10-09 11:15:55 -0700937// Maps signed condition to unsigned condition and FP condition to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100938inline Condition X86UnsignedOrFPCondition(IfCondition cond) {
939 switch (cond) {
940 case kCondEQ: return kEqual;
941 case kCondNE: return kNotEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700942 // Signed to unsigned, and FP to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100943 case kCondLT: return kBelow;
944 case kCondLE: return kBelowEqual;
945 case kCondGT: return kAbove;
946 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700947 // Unsigned remain unchanged.
948 case kCondB: return kBelow;
949 case kCondBE: return kBelowEqual;
950 case kCondA: return kAbove;
951 case kCondAE: return kAboveEqual;
Roland Levillain4fa13f62015-07-06 18:11:54 +0100952 }
953 LOG(FATAL) << "Unreachable";
954 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700955}
956
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100957void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100958 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100959}
960
961void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100962 stream << XmmRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100963}
964
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100965size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
966 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
967 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100968}
969
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100970size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
971 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
972 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100973}
974
Mark Mendell7c8d0092015-01-26 11:21:33 -0500975size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Aart Bikb13c65b2017-03-21 20:14:07 -0700976 if (GetGraph()->HasSIMD()) {
Aart Bik5576f372017-03-23 16:17:37 -0700977 __ movups(Address(ESP, stack_index), XmmRegister(reg_id));
Aart Bikb13c65b2017-03-21 20:14:07 -0700978 } else {
979 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
980 }
Mark Mendell7c8d0092015-01-26 11:21:33 -0500981 return GetFloatingPointSpillSlotSize();
982}
983
984size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Aart Bikb13c65b2017-03-21 20:14:07 -0700985 if (GetGraph()->HasSIMD()) {
Aart Bik5576f372017-03-23 16:17:37 -0700986 __ movups(XmmRegister(reg_id), Address(ESP, stack_index));
Aart Bikb13c65b2017-03-21 20:14:07 -0700987 } else {
988 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
989 }
Mark Mendell7c8d0092015-01-26 11:21:33 -0500990 return GetFloatingPointSpillSlotSize();
991}
992
Calin Juravle175dc732015-08-25 15:42:32 +0100993void CodeGeneratorX86::InvokeRuntime(QuickEntrypointEnum entrypoint,
994 HInstruction* instruction,
995 uint32_t dex_pc,
996 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +0100997 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100998 GenerateInvokeRuntime(GetThreadOffset<kX86PointerSize>(entrypoint).Int32Value());
999 if (EntrypointRequiresStackMap(entrypoint)) {
1000 RecordPcInfo(instruction, dex_pc, slow_path);
1001 }
Alexandre Rames8158f282015-08-07 10:26:17 +01001002}
1003
Roland Levillaindec8f632016-07-22 17:10:06 +01001004void CodeGeneratorX86::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1005 HInstruction* instruction,
1006 SlowPathCode* slow_path) {
1007 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +01001008 GenerateInvokeRuntime(entry_point_offset);
1009}
1010
1011void CodeGeneratorX86::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001012 __ fs()->call(Address::Absolute(entry_point_offset));
1013}
1014
Mark Mendellfb8d2792015-03-31 22:16:59 -04001015CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +00001016 const X86InstructionSetFeatures& isa_features,
1017 const CompilerOptions& compiler_options,
1018 OptimizingCompilerStats* stats)
Mark Mendell5f874182015-03-04 15:42:45 -05001019 : CodeGenerator(graph,
1020 kNumberOfCpuRegisters,
1021 kNumberOfXmmRegisters,
1022 kNumberOfRegisterPairs,
1023 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1024 arraysize(kCoreCalleeSaves))
1025 | (1 << kFakeReturnRegister),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001026 0,
1027 compiler_options,
1028 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +01001029 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001030 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001031 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -04001032 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001033 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +00001034 isa_features_(isa_features),
Vladimir Marko65979462017-05-19 17:25:12 +01001035 boot_image_method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001036 method_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +00001037 boot_image_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1038 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko65979462017-05-19 17:25:12 +01001039 string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001040 string_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001041 jit_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001042 jit_class_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko93205e32016-04-13 11:59:46 +01001043 constant_area_start_(-1),
1044 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001045 method_address_offset_(std::less<uint32_t>(),
1046 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001047 // Use a fake return address register to mimic Quick.
1048 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001049}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001050
David Brazdil58282f42016-01-14 12:45:10 +00001051void CodeGeneratorX86::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001052 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001053 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001054}
1055
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001056InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001057 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001058 assembler_(codegen->GetAssembler()),
1059 codegen_(codegen) {}
1060
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001061static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001062 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001063}
1064
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001065void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001066 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001067 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +00001068 bool skip_overflow_check =
1069 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001070 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +00001071
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001072 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +01001073 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001074 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +01001075 }
1076
Mark Mendell5f874182015-03-04 15:42:45 -05001077 if (HasEmptyFrame()) {
1078 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001079 }
Mark Mendell5f874182015-03-04 15:42:45 -05001080
1081 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
1082 Register reg = kCoreCalleeSaves[i];
1083 if (allocated_registers_.ContainsCoreRegister(reg)) {
1084 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001085 __ cfi().AdjustCFAOffset(kX86WordSize);
1086 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -05001087 }
1088 }
1089
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001090 int adjust = GetFrameSize() - FrameEntrySpillSize();
1091 __ subl(ESP, Immediate(adjust));
1092 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001093 // Save the current method if we need it. Note that we do not
1094 // do this in HCurrentMethod, as the instruction might have been removed
1095 // in the SSA graph.
1096 if (RequiresCurrentMethod()) {
1097 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
1098 }
Nicolas Geoffrayf7893532017-06-15 12:34:36 +01001099
1100 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1101 // Initialize should_deoptimize flag to 0.
1102 __ movl(Address(ESP, GetStackOffsetOfShouldDeoptimizeFlag()), Immediate(0));
1103 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001104}
1105
1106void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001107 __ cfi().RememberState();
1108 if (!HasEmptyFrame()) {
1109 int adjust = GetFrameSize() - FrameEntrySpillSize();
1110 __ addl(ESP, Immediate(adjust));
1111 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -05001112
David Srbeckyc34dc932015-04-12 09:27:43 +01001113 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1114 Register reg = kCoreCalleeSaves[i];
1115 if (allocated_registers_.ContainsCoreRegister(reg)) {
1116 __ popl(reg);
1117 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
1118 __ cfi().Restore(DWARFReg(reg));
1119 }
Mark Mendell5f874182015-03-04 15:42:45 -05001120 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001121 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001122 __ ret();
1123 __ cfi().RestoreState();
1124 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001125}
1126
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001127void CodeGeneratorX86::Bind(HBasicBlock* block) {
1128 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001129}
1130
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001131Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(Primitive::Type type) const {
1132 switch (type) {
1133 case Primitive::kPrimBoolean:
1134 case Primitive::kPrimByte:
1135 case Primitive::kPrimChar:
1136 case Primitive::kPrimShort:
1137 case Primitive::kPrimInt:
1138 case Primitive::kPrimNot:
1139 return Location::RegisterLocation(EAX);
1140
1141 case Primitive::kPrimLong:
1142 return Location::RegisterPairLocation(EAX, EDX);
1143
1144 case Primitive::kPrimVoid:
1145 return Location::NoLocation();
1146
1147 case Primitive::kPrimDouble:
1148 case Primitive::kPrimFloat:
1149 return Location::FpuRegisterLocation(XMM0);
1150 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001151
1152 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001153}
1154
1155Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
1156 return Location::RegisterLocation(kMethodRegisterArgument);
1157}
1158
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001159Location InvokeDexCallingConventionVisitorX86::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001160 switch (type) {
1161 case Primitive::kPrimBoolean:
1162 case Primitive::kPrimByte:
1163 case Primitive::kPrimChar:
1164 case Primitive::kPrimShort:
1165 case Primitive::kPrimInt:
1166 case Primitive::kPrimNot: {
1167 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001168 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001169 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001170 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001171 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001172 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001173 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001174 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001175
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001176 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001177 uint32_t index = gp_index_;
1178 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001179 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001180 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001181 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
1182 calling_convention.GetRegisterPairAt(index));
1183 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001184 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001185 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1186 }
1187 }
1188
1189 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001190 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001191 stack_index_++;
1192 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1193 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1194 } else {
1195 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1196 }
1197 }
1198
1199 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001200 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001201 stack_index_ += 2;
1202 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1203 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1204 } else {
1205 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001206 }
1207 }
1208
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001209 case Primitive::kPrimVoid:
1210 LOG(FATAL) << "Unexpected parameter type " << type;
1211 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001212 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00001213 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001214}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001215
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001216void CodeGeneratorX86::Move32(Location destination, Location source) {
1217 if (source.Equals(destination)) {
1218 return;
1219 }
1220 if (destination.IsRegister()) {
1221 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001222 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001223 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001224 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001225 } else {
1226 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001227 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001228 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001229 } else if (destination.IsFpuRegister()) {
1230 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001231 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001232 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001233 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001234 } else {
1235 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001236 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001237 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001238 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001239 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001240 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001241 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001242 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001243 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05001244 } else if (source.IsConstant()) {
1245 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001246 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05001247 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001248 } else {
1249 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001250 __ pushl(Address(ESP, source.GetStackIndex()));
1251 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001252 }
1253 }
1254}
1255
1256void CodeGeneratorX86::Move64(Location destination, Location source) {
1257 if (source.Equals(destination)) {
1258 return;
1259 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001260 if (destination.IsRegisterPair()) {
1261 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001262 EmitParallelMoves(
1263 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1264 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001265 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001266 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001267 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1268 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001269 } else if (source.IsFpuRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001270 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
1271 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
1272 __ psrlq(src_reg, Immediate(32));
1273 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001274 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001275 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001276 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001277 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
1278 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001279 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
1280 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001281 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05001282 if (source.IsFpuRegister()) {
1283 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
1284 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001285 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001286 } else if (source.IsRegisterPair()) {
1287 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
1288 // Create stack space for 2 elements.
1289 __ subl(ESP, Immediate(2 * elem_size));
1290 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
1291 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
1292 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
1293 // And remove the temporary stack space we allocated.
1294 __ addl(ESP, Immediate(2 * elem_size));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001295 } else {
1296 LOG(FATAL) << "Unimplemented";
1297 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001298 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001299 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001300 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001301 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001302 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001303 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001304 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001305 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001306 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001307 } else if (source.IsConstant()) {
1308 HConstant* constant = source.GetConstant();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001309 DCHECK(constant->IsLongConstant() || constant->IsDoubleConstant());
1310 int64_t value = GetInt64ValueOf(constant);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001311 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001312 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
1313 Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001314 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001315 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001316 EmitParallelMoves(
1317 Location::StackSlot(source.GetStackIndex()),
1318 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001319 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001320 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001321 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
1322 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001323 }
1324 }
1325}
1326
Calin Juravle175dc732015-08-25 15:42:32 +01001327void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
1328 DCHECK(location.IsRegister());
1329 __ movl(location.AsRegister<Register>(), Immediate(value));
1330}
1331
Calin Juravlee460d1d2015-09-29 04:52:17 +01001332void CodeGeneratorX86::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001333 HParallelMove move(GetGraph()->GetArena());
1334 if (dst_type == Primitive::kPrimLong && !src.IsConstant() && !src.IsFpuRegister()) {
1335 move.AddMove(src.ToLow(), dst.ToLow(), Primitive::kPrimInt, nullptr);
1336 move.AddMove(src.ToHigh(), dst.ToHigh(), Primitive::kPrimInt, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001337 } else {
David Brazdil74eb1b22015-12-14 11:44:01 +00001338 move.AddMove(src, dst, dst_type, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001339 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001340 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001341}
1342
1343void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1344 if (location.IsRegister()) {
1345 locations->AddTemp(location);
1346 } else if (location.IsRegisterPair()) {
1347 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1348 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1349 } else {
1350 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1351 }
1352}
1353
David Brazdilfc6a86a2015-06-26 10:33:45 +00001354void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001355 DCHECK(!successor->IsExitBlock());
1356
1357 HBasicBlock* block = got->GetBlock();
1358 HInstruction* previous = got->GetPrevious();
1359
1360 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001361 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001362 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1363 return;
1364 }
1365
1366 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1367 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1368 }
1369 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001370 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001371 }
1372}
1373
David Brazdilfc6a86a2015-06-26 10:33:45 +00001374void LocationsBuilderX86::VisitGoto(HGoto* got) {
1375 got->SetLocations(nullptr);
1376}
1377
1378void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1379 HandleGoto(got, got->GetSuccessor());
1380}
1381
1382void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1383 try_boundary->SetLocations(nullptr);
1384}
1385
1386void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1387 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1388 if (!successor->IsExitBlock()) {
1389 HandleGoto(try_boundary, successor);
1390 }
1391}
1392
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001393void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001394 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001395}
1396
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001397void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001398}
1399
Mark Mendell152408f2015-12-31 12:28:50 -05001400template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001401void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001402 LabelType* true_label,
1403 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001404 if (cond->IsFPConditionTrueIfNaN()) {
1405 __ j(kUnordered, true_label);
1406 } else if (cond->IsFPConditionFalseIfNaN()) {
1407 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001408 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001409 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001410}
1411
Mark Mendell152408f2015-12-31 12:28:50 -05001412template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001413void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001414 LabelType* true_label,
1415 LabelType* false_label) {
Mark Mendellc4701932015-04-10 13:18:51 -04001416 LocationSummary* locations = cond->GetLocations();
1417 Location left = locations->InAt(0);
1418 Location right = locations->InAt(1);
1419 IfCondition if_cond = cond->GetCondition();
1420
Mark Mendellc4701932015-04-10 13:18:51 -04001421 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001422 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001423 IfCondition true_high_cond = if_cond;
1424 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001425 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001426
1427 // Set the conditions for the test, remembering that == needs to be
1428 // decided using the low words.
1429 switch (if_cond) {
1430 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001431 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001432 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001433 break;
1434 case kCondLT:
1435 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001436 break;
1437 case kCondLE:
1438 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001439 break;
1440 case kCondGT:
1441 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001442 break;
1443 case kCondGE:
1444 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001445 break;
Aart Bike9f37602015-10-09 11:15:55 -07001446 case kCondB:
1447 false_high_cond = kCondA;
1448 break;
1449 case kCondBE:
1450 true_high_cond = kCondB;
1451 break;
1452 case kCondA:
1453 false_high_cond = kCondB;
1454 break;
1455 case kCondAE:
1456 true_high_cond = kCondA;
1457 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001458 }
1459
1460 if (right.IsConstant()) {
1461 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001462 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001463 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001464
Aart Bika19616e2016-02-01 18:57:58 -08001465 codegen_->Compare32BitValue(left_high, val_high);
Mark Mendellc4701932015-04-10 13:18:51 -04001466 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001467 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001468 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001469 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001470 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001471 __ j(X86Condition(true_high_cond), true_label);
1472 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001473 }
1474 // Must be equal high, so compare the lows.
Aart Bika19616e2016-02-01 18:57:58 -08001475 codegen_->Compare32BitValue(left_low, val_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001476 } else if (right.IsRegisterPair()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001477 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001478 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001479
1480 __ cmpl(left_high, right_high);
1481 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001482 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001483 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001484 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001485 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001486 __ j(X86Condition(true_high_cond), true_label);
1487 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001488 }
1489 // Must be equal high, so compare the lows.
1490 __ cmpl(left_low, right_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001491 } else {
1492 DCHECK(right.IsDoubleStackSlot());
1493 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
1494 if (if_cond == kCondNE) {
1495 __ j(X86Condition(true_high_cond), true_label);
1496 } else if (if_cond == kCondEQ) {
1497 __ j(X86Condition(false_high_cond), false_label);
1498 } else {
1499 __ j(X86Condition(true_high_cond), true_label);
1500 __ j(X86Condition(false_high_cond), false_label);
1501 }
1502 // Must be equal high, so compare the lows.
1503 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Mark Mendellc4701932015-04-10 13:18:51 -04001504 }
1505 // The last comparison might be unsigned.
1506 __ j(final_condition, true_label);
1507}
1508
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001509void InstructionCodeGeneratorX86::GenerateFPCompare(Location lhs,
1510 Location rhs,
1511 HInstruction* insn,
1512 bool is_double) {
1513 HX86LoadFromConstantTable* const_area = insn->InputAt(1)->AsX86LoadFromConstantTable();
1514 if (is_double) {
1515 if (rhs.IsFpuRegister()) {
1516 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1517 } else if (const_area != nullptr) {
1518 DCHECK(const_area->IsEmittedAtUseSite());
1519 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(),
1520 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001521 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
1522 const_area->GetBaseMethodAddress(),
1523 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001524 } else {
1525 DCHECK(rhs.IsDoubleStackSlot());
1526 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1527 }
1528 } else {
1529 if (rhs.IsFpuRegister()) {
1530 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1531 } else if (const_area != nullptr) {
1532 DCHECK(const_area->IsEmittedAtUseSite());
1533 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(),
1534 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001535 const_area->GetConstant()->AsFloatConstant()->GetValue(),
1536 const_area->GetBaseMethodAddress(),
1537 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001538 } else {
1539 DCHECK(rhs.IsStackSlot());
1540 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1541 }
1542 }
1543}
1544
Mark Mendell152408f2015-12-31 12:28:50 -05001545template<class LabelType>
David Brazdil0debae72015-11-12 18:37:00 +00001546void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendell152408f2015-12-31 12:28:50 -05001547 LabelType* true_target_in,
1548 LabelType* false_target_in) {
David Brazdil0debae72015-11-12 18:37:00 +00001549 // Generated branching requires both targets to be explicit. If either of the
1550 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Mark Mendell152408f2015-12-31 12:28:50 -05001551 LabelType fallthrough_target;
1552 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1553 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
David Brazdil0debae72015-11-12 18:37:00 +00001554
Mark Mendellc4701932015-04-10 13:18:51 -04001555 LocationSummary* locations = condition->GetLocations();
1556 Location left = locations->InAt(0);
1557 Location right = locations->InAt(1);
1558
Mark Mendellc4701932015-04-10 13:18:51 -04001559 Primitive::Type type = condition->InputAt(0)->GetType();
1560 switch (type) {
1561 case Primitive::kPrimLong:
1562 GenerateLongComparesAndJumps(condition, true_target, false_target);
1563 break;
1564 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001565 GenerateFPCompare(left, right, condition, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001566 GenerateFPJumps(condition, true_target, false_target);
1567 break;
1568 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001569 GenerateFPCompare(left, right, condition, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001570 GenerateFPJumps(condition, true_target, false_target);
1571 break;
1572 default:
1573 LOG(FATAL) << "Unexpected compare type " << type;
1574 }
1575
David Brazdil0debae72015-11-12 18:37:00 +00001576 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001577 __ jmp(false_target);
1578 }
David Brazdil0debae72015-11-12 18:37:00 +00001579
1580 if (fallthrough_target.IsLinked()) {
1581 __ Bind(&fallthrough_target);
1582 }
Mark Mendellc4701932015-04-10 13:18:51 -04001583}
1584
David Brazdil0debae72015-11-12 18:37:00 +00001585static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1586 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1587 // are set only strictly before `branch`. We can't use the eflags on long/FP
1588 // conditions if they are materialized due to the complex branching.
1589 return cond->IsCondition() &&
1590 cond->GetNext() == branch &&
1591 cond->InputAt(0)->GetType() != Primitive::kPrimLong &&
1592 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1593}
1594
Mark Mendell152408f2015-12-31 12:28:50 -05001595template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001596void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001597 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001598 LabelType* true_target,
1599 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001600 HInstruction* cond = instruction->InputAt(condition_input_index);
1601
1602 if (true_target == nullptr && false_target == nullptr) {
1603 // Nothing to do. The code always falls through.
1604 return;
1605 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001606 // Constant condition, statically compared against "true" (integer value 1).
1607 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001608 if (true_target != nullptr) {
1609 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001610 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001611 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001612 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001613 if (false_target != nullptr) {
1614 __ jmp(false_target);
1615 }
1616 }
1617 return;
1618 }
1619
1620 // The following code generates these patterns:
1621 // (1) true_target == nullptr && false_target != nullptr
1622 // - opposite condition true => branch to false_target
1623 // (2) true_target != nullptr && false_target == nullptr
1624 // - condition true => branch to true_target
1625 // (3) true_target != nullptr && false_target != nullptr
1626 // - condition true => branch to true_target
1627 // - branch to false_target
1628 if (IsBooleanValueOrMaterializedCondition(cond)) {
1629 if (AreEflagsSetFrom(cond, instruction)) {
1630 if (true_target == nullptr) {
1631 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
1632 } else {
1633 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
1634 }
1635 } else {
1636 // Materialized condition, compare against 0.
1637 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1638 if (lhs.IsRegister()) {
1639 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1640 } else {
1641 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1642 }
1643 if (true_target == nullptr) {
1644 __ j(kEqual, false_target);
1645 } else {
1646 __ j(kNotEqual, true_target);
1647 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001648 }
1649 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001650 // Condition has not been materialized, use its inputs as the comparison and
1651 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001652 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001653
1654 // If this is a long or FP comparison that has been folded into
1655 // the HCondition, generate the comparison directly.
1656 Primitive::Type type = condition->InputAt(0)->GetType();
1657 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1658 GenerateCompareTestAndBranch(condition, true_target, false_target);
1659 return;
1660 }
1661
1662 Location lhs = condition->GetLocations()->InAt(0);
1663 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001664 // LHS is guaranteed to be in a register (see LocationsBuilderX86::HandleCondition).
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001665 codegen_->GenerateIntCompare(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00001666 if (true_target == nullptr) {
1667 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1668 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001669 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001670 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001671 }
David Brazdil0debae72015-11-12 18:37:00 +00001672
1673 // If neither branch falls through (case 3), the conditional branch to `true_target`
1674 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1675 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001676 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001677 }
1678}
1679
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001680void LocationsBuilderX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001681 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1682 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001683 locations->SetInAt(0, Location::Any());
1684 }
1685}
1686
1687void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001688 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1689 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1690 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1691 nullptr : codegen_->GetLabelOf(true_successor);
1692 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1693 nullptr : codegen_->GetLabelOf(false_successor);
1694 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001695}
1696
1697void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1698 LocationSummary* locations = new (GetGraph()->GetArena())
1699 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01001700 InvokeRuntimeCallingConvention calling_convention;
1701 RegisterSet caller_saves = RegisterSet::Empty();
1702 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1703 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00001704 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001705 locations->SetInAt(0, Location::Any());
1706 }
1707}
1708
1709void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001710 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001711 GenerateTestAndBranch<Label>(deoptimize,
1712 /* condition_input_index */ 0,
1713 slow_path->GetEntryLabel(),
1714 /* false_target */ nullptr);
1715}
1716
Mingyao Yang063fc772016-08-02 11:02:54 -07001717void LocationsBuilderX86::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
1718 LocationSummary* locations = new (GetGraph()->GetArena())
1719 LocationSummary(flag, LocationSummary::kNoCall);
1720 locations->SetOut(Location::RequiresRegister());
1721}
1722
1723void InstructionCodeGeneratorX86::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
1724 __ movl(flag->GetLocations()->Out().AsRegister<Register>(),
1725 Address(ESP, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
1726}
1727
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001728static bool SelectCanUseCMOV(HSelect* select) {
1729 // There are no conditional move instructions for XMMs.
1730 if (Primitive::IsFloatingPointType(select->GetType())) {
1731 return false;
1732 }
1733
1734 // A FP condition doesn't generate the single CC that we need.
1735 // In 32 bit mode, a long condition doesn't generate a single CC either.
1736 HInstruction* condition = select->GetCondition();
1737 if (condition->IsCondition()) {
1738 Primitive::Type compare_type = condition->InputAt(0)->GetType();
1739 if (compare_type == Primitive::kPrimLong ||
1740 Primitive::IsFloatingPointType(compare_type)) {
1741 return false;
1742 }
1743 }
1744
1745 // We can generate a CMOV for this Select.
1746 return true;
1747}
1748
David Brazdil74eb1b22015-12-14 11:44:01 +00001749void LocationsBuilderX86::VisitSelect(HSelect* select) {
1750 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001751 if (Primitive::IsFloatingPointType(select->GetType())) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001752 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001753 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001754 } else {
1755 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001756 if (SelectCanUseCMOV(select)) {
1757 if (select->InputAt(1)->IsConstant()) {
1758 // Cmov can't handle a constant value.
1759 locations->SetInAt(1, Location::RequiresRegister());
1760 } else {
1761 locations->SetInAt(1, Location::Any());
1762 }
1763 } else {
1764 locations->SetInAt(1, Location::Any());
1765 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001766 }
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001767 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1768 locations->SetInAt(2, Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00001769 }
1770 locations->SetOut(Location::SameAsFirstInput());
1771}
1772
1773void InstructionCodeGeneratorX86::VisitSelect(HSelect* select) {
1774 LocationSummary* locations = select->GetLocations();
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001775 DCHECK(locations->InAt(0).Equals(locations->Out()));
1776 if (SelectCanUseCMOV(select)) {
1777 // If both the condition and the source types are integer, we can generate
1778 // a CMOV to implement Select.
1779
1780 HInstruction* select_condition = select->GetCondition();
1781 Condition cond = kNotEqual;
1782
1783 // Figure out how to test the 'condition'.
1784 if (select_condition->IsCondition()) {
1785 HCondition* condition = select_condition->AsCondition();
1786 if (!condition->IsEmittedAtUseSite()) {
1787 // This was a previously materialized condition.
1788 // Can we use the existing condition code?
1789 if (AreEflagsSetFrom(condition, select)) {
1790 // Materialization was the previous instruction. Condition codes are right.
1791 cond = X86Condition(condition->GetCondition());
1792 } else {
1793 // No, we have to recreate the condition code.
1794 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1795 __ testl(cond_reg, cond_reg);
1796 }
1797 } else {
1798 // We can't handle FP or long here.
1799 DCHECK_NE(condition->InputAt(0)->GetType(), Primitive::kPrimLong);
1800 DCHECK(!Primitive::IsFloatingPointType(condition->InputAt(0)->GetType()));
1801 LocationSummary* cond_locations = condition->GetLocations();
Roland Levillain0b671c02016-08-19 12:02:34 +01001802 codegen_->GenerateIntCompare(cond_locations->InAt(0), cond_locations->InAt(1));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001803 cond = X86Condition(condition->GetCondition());
1804 }
1805 } else {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01001806 // Must be a Boolean condition, which needs to be compared to 0.
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001807 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1808 __ testl(cond_reg, cond_reg);
1809 }
1810
1811 // If the condition is true, overwrite the output, which already contains false.
1812 Location false_loc = locations->InAt(0);
1813 Location true_loc = locations->InAt(1);
1814 if (select->GetType() == Primitive::kPrimLong) {
1815 // 64 bit conditional move.
1816 Register false_high = false_loc.AsRegisterPairHigh<Register>();
1817 Register false_low = false_loc.AsRegisterPairLow<Register>();
1818 if (true_loc.IsRegisterPair()) {
1819 __ cmovl(cond, false_high, true_loc.AsRegisterPairHigh<Register>());
1820 __ cmovl(cond, false_low, true_loc.AsRegisterPairLow<Register>());
1821 } else {
1822 __ cmovl(cond, false_high, Address(ESP, true_loc.GetHighStackIndex(kX86WordSize)));
1823 __ cmovl(cond, false_low, Address(ESP, true_loc.GetStackIndex()));
1824 }
1825 } else {
1826 // 32 bit conditional move.
1827 Register false_reg = false_loc.AsRegister<Register>();
1828 if (true_loc.IsRegister()) {
1829 __ cmovl(cond, false_reg, true_loc.AsRegister<Register>());
1830 } else {
1831 __ cmovl(cond, false_reg, Address(ESP, true_loc.GetStackIndex()));
1832 }
1833 }
1834 } else {
1835 NearLabel false_target;
1836 GenerateTestAndBranch<NearLabel>(
1837 select, /* condition_input_index */ 2, /* true_target */ nullptr, &false_target);
1838 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1839 __ Bind(&false_target);
1840 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001841}
1842
David Srbecky0cf44932015-12-09 14:09:59 +00001843void LocationsBuilderX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1844 new (GetGraph()->GetArena()) LocationSummary(info);
1845}
1846
David Srbeckyd28f4a02016-03-14 17:14:24 +00001847void InstructionCodeGeneratorX86::VisitNativeDebugInfo(HNativeDebugInfo*) {
1848 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001849}
1850
1851void CodeGeneratorX86::GenerateNop() {
1852 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001853}
1854
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001855void LocationsBuilderX86::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001856 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001857 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001858 // Handle the long/FP comparisons made in instruction simplification.
1859 switch (cond->InputAt(0)->GetType()) {
1860 case Primitive::kPrimLong: {
1861 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell8659e842016-02-16 10:41:46 -05001862 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001863 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001864 locations->SetOut(Location::RequiresRegister());
1865 }
1866 break;
1867 }
1868 case Primitive::kPrimFloat:
1869 case Primitive::kPrimDouble: {
1870 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001871 if (cond->InputAt(1)->IsX86LoadFromConstantTable()) {
1872 DCHECK(cond->InputAt(1)->IsEmittedAtUseSite());
1873 } else if (cond->InputAt(1)->IsConstant()) {
1874 locations->SetInAt(1, Location::RequiresFpuRegister());
1875 } else {
1876 locations->SetInAt(1, Location::Any());
1877 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001878 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001879 locations->SetOut(Location::RequiresRegister());
1880 }
1881 break;
1882 }
1883 default:
1884 locations->SetInAt(0, Location::RequiresRegister());
1885 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001886 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001887 // We need a byte register.
1888 locations->SetOut(Location::RegisterLocation(ECX));
1889 }
1890 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001891 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001892}
1893
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001894void InstructionCodeGeneratorX86::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001895 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001896 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001897 }
Mark Mendellc4701932015-04-10 13:18:51 -04001898
1899 LocationSummary* locations = cond->GetLocations();
1900 Location lhs = locations->InAt(0);
1901 Location rhs = locations->InAt(1);
1902 Register reg = locations->Out().AsRegister<Register>();
Mark Mendell152408f2015-12-31 12:28:50 -05001903 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001904
1905 switch (cond->InputAt(0)->GetType()) {
1906 default: {
1907 // Integer case.
1908
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01001909 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04001910 __ xorl(reg, reg);
Roland Levillain0b671c02016-08-19 12:02:34 +01001911 codegen_->GenerateIntCompare(lhs, rhs);
Aart Bike9f37602015-10-09 11:15:55 -07001912 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001913 return;
1914 }
1915 case Primitive::kPrimLong:
1916 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1917 break;
1918 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001919 GenerateFPCompare(lhs, rhs, cond, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001920 GenerateFPJumps(cond, &true_label, &false_label);
1921 break;
1922 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001923 GenerateFPCompare(lhs, rhs, cond, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001924 GenerateFPJumps(cond, &true_label, &false_label);
1925 break;
1926 }
1927
1928 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001929 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001930
Roland Levillain4fa13f62015-07-06 18:11:54 +01001931 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001932 __ Bind(&false_label);
1933 __ xorl(reg, reg);
1934 __ jmp(&done_label);
1935
Roland Levillain4fa13f62015-07-06 18:11:54 +01001936 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001937 __ Bind(&true_label);
1938 __ movl(reg, Immediate(1));
1939 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001940}
1941
1942void LocationsBuilderX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001943 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001944}
1945
1946void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001947 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001948}
1949
1950void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001951 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001952}
1953
1954void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001955 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001956}
1957
1958void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001959 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001960}
1961
1962void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001963 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001964}
1965
1966void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001967 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001968}
1969
1970void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001971 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001972}
1973
1974void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001975 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001976}
1977
1978void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001979 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001980}
1981
1982void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001983 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001984}
1985
1986void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001987 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001988}
1989
Aart Bike9f37602015-10-09 11:15:55 -07001990void LocationsBuilderX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001991 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001992}
1993
1994void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001995 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001996}
1997
1998void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001999 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002000}
2001
2002void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002003 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002004}
2005
2006void LocationsBuilderX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002007 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002008}
2009
2010void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002011 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002012}
2013
2014void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002015 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002016}
2017
2018void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002019 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002020}
2021
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002022void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002023 LocationSummary* locations =
2024 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002025 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002026}
2027
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002028void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002029 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002030}
2031
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002032void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
2033 LocationSummary* locations =
2034 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2035 locations->SetOut(Location::ConstantLocation(constant));
2036}
2037
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002038void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002039 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002040}
2041
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002042void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002043 LocationSummary* locations =
2044 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002045 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002046}
2047
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002048void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002049 // Will be generated at use site.
2050}
2051
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002052void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
2053 LocationSummary* locations =
2054 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2055 locations->SetOut(Location::ConstantLocation(constant));
2056}
2057
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002058void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002059 // Will be generated at use site.
2060}
2061
2062void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
2063 LocationSummary* locations =
2064 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2065 locations->SetOut(Location::ConstantLocation(constant));
2066}
2067
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002068void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002069 // Will be generated at use site.
2070}
2071
Igor Murashkind01745e2017-04-05 16:40:31 -07002072void LocationsBuilderX86::VisitConstructorFence(HConstructorFence* constructor_fence) {
2073 constructor_fence->SetLocations(nullptr);
2074}
2075
2076void InstructionCodeGeneratorX86::VisitConstructorFence(
2077 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
2078 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
2079}
2080
Calin Juravle27df7582015-04-17 19:12:31 +01002081void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2082 memory_barrier->SetLocations(nullptr);
2083}
2084
2085void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00002086 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002087}
2088
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002089void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002090 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002091}
2092
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002093void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002094 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002095}
2096
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002097void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002098 LocationSummary* locations =
2099 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002100 switch (ret->InputAt(0)->GetType()) {
2101 case Primitive::kPrimBoolean:
2102 case Primitive::kPrimByte:
2103 case Primitive::kPrimChar:
2104 case Primitive::kPrimShort:
2105 case Primitive::kPrimInt:
2106 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002107 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002108 break;
2109
2110 case Primitive::kPrimLong:
2111 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002112 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002113 break;
2114
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002115 case Primitive::kPrimFloat:
2116 case Primitive::kPrimDouble:
2117 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002118 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002119 break;
2120
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002121 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002122 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002123 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002124}
2125
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002126void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002127 if (kIsDebugBuild) {
2128 switch (ret->InputAt(0)->GetType()) {
2129 case Primitive::kPrimBoolean:
2130 case Primitive::kPrimByte:
2131 case Primitive::kPrimChar:
2132 case Primitive::kPrimShort:
2133 case Primitive::kPrimInt:
2134 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002135 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002136 break;
2137
2138 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002139 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
2140 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002141 break;
2142
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002143 case Primitive::kPrimFloat:
2144 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002145 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002146 break;
2147
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002148 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002149 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002150 }
2151 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002152 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002153}
2154
Calin Juravle175dc732015-08-25 15:42:32 +01002155void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2156 // The trampoline uses the same calling convention as dex calling conventions,
2157 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2158 // the method_idx.
2159 HandleInvoke(invoke);
2160}
2161
2162void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2163 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2164}
2165
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002166void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002167 // Explicit clinit checks triggered by static invokes must have been pruned by
2168 // art::PrepareForRegisterAllocation.
2169 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002170
Mark Mendellfb8d2792015-03-31 22:16:59 -04002171 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002172 if (intrinsic.TryDispatch(invoke)) {
Vladimir Marko65979462017-05-19 17:25:12 +01002173 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeMethodLoadKind()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00002174 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002175 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04002176 return;
2177 }
2178
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002179 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002180
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002181 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
Vladimir Marko65979462017-05-19 17:25:12 +01002182 if (invoke->HasPcRelativeMethodLoadKind()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00002183 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002184 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002185}
2186
Mark Mendell09ed1a32015-03-25 08:30:06 -04002187static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
2188 if (invoke->GetLocations()->Intrinsified()) {
2189 IntrinsicCodeGeneratorX86 intrinsic(codegen);
2190 intrinsic.Dispatch(invoke);
2191 return true;
2192 }
2193 return false;
2194}
2195
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002196void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002197 // Explicit clinit checks triggered by static invokes must have been pruned by
2198 // art::PrepareForRegisterAllocation.
2199 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002200
Mark Mendell09ed1a32015-03-25 08:30:06 -04002201 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2202 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002203 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002204
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002205 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04002206 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002207 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002208}
2209
2210void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00002211 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
2212 if (intrinsic.TryDispatch(invoke)) {
2213 return;
2214 }
2215
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002216 HandleInvoke(invoke);
2217}
2218
2219void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002220 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002221 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002222}
2223
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002224void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002225 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2226 return;
2227 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002228
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002229 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002230 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002231}
2232
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002233void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002234 // This call to HandleInvoke allocates a temporary (core) register
2235 // which is also used to transfer the hidden argument from FP to
2236 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002237 HandleInvoke(invoke);
2238 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002239 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002240}
2241
2242void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
2243 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002244 LocationSummary* locations = invoke->GetLocations();
2245 Register temp = locations->GetTemp(0).AsRegister<Register>();
2246 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002247 Location receiver = locations->InAt(0);
2248 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2249
Roland Levillain0d5a2812015-11-13 10:07:31 +00002250 // Set the hidden argument. This is safe to do this here, as XMM7
2251 // won't be modified thereafter, before the `call` instruction.
2252 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002253 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002254 __ movd(hidden_reg, temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002255
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002256 if (receiver.IsStackSlot()) {
2257 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002258 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002259 __ movl(temp, Address(temp, class_offset));
2260 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002261 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002262 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002263 }
Roland Levillain4d027112015-07-01 15:41:14 +01002264 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002265 // Instead of simply (possibly) unpoisoning `temp` here, we should
2266 // emit a read barrier for the previous class reference load.
2267 // However this is not required in practice, as this is an
2268 // intermediate/temporary reference and because the current
2269 // concurrent copying collector keeps the from-space memory
2270 // intact/accessible until the end of the marking phase (the
2271 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002272 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002273 // temp = temp->GetAddressOfIMT()
2274 __ movl(temp,
2275 Address(temp, mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002276 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002277 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002278 invoke->GetImtIndex(), kX86PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002279 __ movl(temp, Address(temp, method_offset));
2280 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002281 __ call(Address(temp,
Andreas Gampe542451c2016-07-26 09:02:02 -07002282 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002283
2284 DCHECK(!codegen_->IsLeafMethod());
2285 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2286}
2287
Orion Hodsonac141392017-01-13 11:53:47 +00002288void LocationsBuilderX86::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2289 HandleInvoke(invoke);
2290}
2291
2292void InstructionCodeGeneratorX86::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2293 codegen_->GenerateInvokePolymorphicCall(invoke);
2294}
2295
Roland Levillain88cb1752014-10-20 16:36:47 +01002296void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2297 LocationSummary* locations =
2298 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2299 switch (neg->GetResultType()) {
2300 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002301 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002302 locations->SetInAt(0, Location::RequiresRegister());
2303 locations->SetOut(Location::SameAsFirstInput());
2304 break;
2305
Roland Levillain88cb1752014-10-20 16:36:47 +01002306 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00002307 locations->SetInAt(0, Location::RequiresFpuRegister());
2308 locations->SetOut(Location::SameAsFirstInput());
2309 locations->AddTemp(Location::RequiresRegister());
2310 locations->AddTemp(Location::RequiresFpuRegister());
2311 break;
2312
Roland Levillain88cb1752014-10-20 16:36:47 +01002313 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002314 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002315 locations->SetOut(Location::SameAsFirstInput());
2316 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002317 break;
2318
2319 default:
2320 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2321 }
2322}
2323
2324void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2325 LocationSummary* locations = neg->GetLocations();
2326 Location out = locations->Out();
2327 Location in = locations->InAt(0);
2328 switch (neg->GetResultType()) {
2329 case Primitive::kPrimInt:
2330 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002331 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002332 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002333 break;
2334
2335 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002336 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002337 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002338 __ negl(out.AsRegisterPairLow<Register>());
2339 // Negation is similar to subtraction from zero. The least
2340 // significant byte triggers a borrow when it is different from
2341 // zero; to take it into account, add 1 to the most significant
2342 // byte if the carry flag (CF) is set to 1 after the first NEGL
2343 // operation.
2344 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2345 __ negl(out.AsRegisterPairHigh<Register>());
2346 break;
2347
Roland Levillain5368c212014-11-27 15:03:41 +00002348 case Primitive::kPrimFloat: {
2349 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002350 Register constant = locations->GetTemp(0).AsRegister<Register>();
2351 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002352 // Implement float negation with an exclusive or with value
2353 // 0x80000000 (mask for bit 31, representing the sign of a
2354 // single-precision floating-point number).
2355 __ movl(constant, Immediate(INT32_C(0x80000000)));
2356 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002357 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002358 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002359 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002360
Roland Levillain5368c212014-11-27 15:03:41 +00002361 case Primitive::kPrimDouble: {
2362 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002363 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002364 // Implement double negation with an exclusive or with value
2365 // 0x8000000000000000 (mask for bit 63, representing the sign of
2366 // a double-precision floating-point number).
2367 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002368 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002369 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002370 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002371
2372 default:
2373 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2374 }
2375}
2376
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002377void LocationsBuilderX86::VisitX86FPNeg(HX86FPNeg* neg) {
2378 LocationSummary* locations =
2379 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2380 DCHECK(Primitive::IsFloatingPointType(neg->GetType()));
2381 locations->SetInAt(0, Location::RequiresFpuRegister());
2382 locations->SetInAt(1, Location::RequiresRegister());
2383 locations->SetOut(Location::SameAsFirstInput());
2384 locations->AddTemp(Location::RequiresFpuRegister());
2385}
2386
2387void InstructionCodeGeneratorX86::VisitX86FPNeg(HX86FPNeg* neg) {
2388 LocationSummary* locations = neg->GetLocations();
2389 Location out = locations->Out();
2390 DCHECK(locations->InAt(0).Equals(out));
2391
2392 Register constant_area = locations->InAt(1).AsRegister<Register>();
2393 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2394 if (neg->GetType() == Primitive::kPrimFloat) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00002395 __ movss(mask, codegen_->LiteralInt32Address(INT32_C(0x80000000),
2396 neg->GetBaseMethodAddress(),
2397 constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002398 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
2399 } else {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00002400 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000),
2401 neg->GetBaseMethodAddress(),
2402 constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002403 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
2404 }
2405}
2406
Roland Levillaindff1f282014-11-05 14:15:05 +00002407void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002408 Primitive::Type result_type = conversion->GetResultType();
2409 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002410 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002411
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002412 // The float-to-long and double-to-long type conversions rely on a
2413 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002414 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002415 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2416 && result_type == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002417 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002418 : LocationSummary::kNoCall;
2419 LocationSummary* locations =
2420 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2421
David Brazdilb2bd1c52015-03-25 11:17:37 +00002422 // The Java language does not allow treating boolean as an integral type but
2423 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002424
Roland Levillaindff1f282014-11-05 14:15:05 +00002425 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002426 case Primitive::kPrimByte:
2427 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002428 case Primitive::kPrimLong: {
2429 // Type conversion from long to byte is a result of code transformations.
2430 HInstruction* input = conversion->InputAt(0);
2431 Location input_location = input->IsConstant()
2432 ? Location::ConstantLocation(input->AsConstant())
2433 : Location::RegisterPairLocation(EAX, EDX);
2434 locations->SetInAt(0, input_location);
2435 // Make the output overlap to please the register allocator. This greatly simplifies
2436 // the validation of the linear scan implementation
2437 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2438 break;
2439 }
David Brazdil46e2a392015-03-16 17:31:52 +00002440 case Primitive::kPrimBoolean:
2441 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002442 case Primitive::kPrimShort:
2443 case Primitive::kPrimInt:
2444 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002445 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05002446 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2447 // Make the output overlap to please the register allocator. This greatly simplifies
2448 // the validation of the linear scan implementation
2449 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002450 break;
2451
2452 default:
2453 LOG(FATAL) << "Unexpected type conversion from " << input_type
2454 << " to " << result_type;
2455 }
2456 break;
2457
Roland Levillain01a8d712014-11-14 16:27:39 +00002458 case Primitive::kPrimShort:
2459 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002460 case Primitive::kPrimLong:
2461 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002462 case Primitive::kPrimBoolean:
2463 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002464 case Primitive::kPrimByte:
2465 case Primitive::kPrimInt:
2466 case Primitive::kPrimChar:
2467 // Processing a Dex `int-to-short' instruction.
2468 locations->SetInAt(0, Location::Any());
2469 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2470 break;
2471
2472 default:
2473 LOG(FATAL) << "Unexpected type conversion from " << input_type
2474 << " to " << result_type;
2475 }
2476 break;
2477
Roland Levillain946e1432014-11-11 17:35:19 +00002478 case Primitive::kPrimInt:
2479 switch (input_type) {
2480 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002481 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002482 locations->SetInAt(0, Location::Any());
2483 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2484 break;
2485
2486 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002487 // Processing a Dex `float-to-int' instruction.
2488 locations->SetInAt(0, Location::RequiresFpuRegister());
2489 locations->SetOut(Location::RequiresRegister());
2490 locations->AddTemp(Location::RequiresFpuRegister());
2491 break;
2492
Roland Levillain946e1432014-11-11 17:35:19 +00002493 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002494 // Processing a Dex `double-to-int' instruction.
2495 locations->SetInAt(0, Location::RequiresFpuRegister());
2496 locations->SetOut(Location::RequiresRegister());
2497 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002498 break;
2499
2500 default:
2501 LOG(FATAL) << "Unexpected type conversion from " << input_type
2502 << " to " << result_type;
2503 }
2504 break;
2505
Roland Levillaindff1f282014-11-05 14:15:05 +00002506 case Primitive::kPrimLong:
2507 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002508 case Primitive::kPrimBoolean:
2509 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002510 case Primitive::kPrimByte:
2511 case Primitive::kPrimShort:
2512 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002513 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002514 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002515 locations->SetInAt(0, Location::RegisterLocation(EAX));
2516 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2517 break;
2518
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002519 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00002520 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002521 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00002522 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002523 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2524 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2525
Vladimir Marko949c91f2015-01-27 10:48:44 +00002526 // The runtime helper puts the result in EAX, EDX.
2527 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002528 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002529 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002530
2531 default:
2532 LOG(FATAL) << "Unexpected type conversion from " << input_type
2533 << " to " << result_type;
2534 }
2535 break;
2536
Roland Levillain981e4542014-11-14 11:47:14 +00002537 case Primitive::kPrimChar:
2538 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002539 case Primitive::kPrimLong:
2540 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002541 case Primitive::kPrimBoolean:
2542 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002543 case Primitive::kPrimByte:
2544 case Primitive::kPrimShort:
2545 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002546 // Processing a Dex `int-to-char' instruction.
2547 locations->SetInAt(0, Location::Any());
2548 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2549 break;
2550
2551 default:
2552 LOG(FATAL) << "Unexpected type conversion from " << input_type
2553 << " to " << result_type;
2554 }
2555 break;
2556
Roland Levillaindff1f282014-11-05 14:15:05 +00002557 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002558 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002559 case Primitive::kPrimBoolean:
2560 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002561 case Primitive::kPrimByte:
2562 case Primitive::kPrimShort:
2563 case Primitive::kPrimInt:
2564 case Primitive::kPrimChar:
2565 // Processing a Dex `int-to-float' instruction.
2566 locations->SetInAt(0, Location::RequiresRegister());
2567 locations->SetOut(Location::RequiresFpuRegister());
2568 break;
2569
2570 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002571 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002572 locations->SetInAt(0, Location::Any());
2573 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002574 break;
2575
Roland Levillaincff13742014-11-17 14:32:17 +00002576 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002577 // Processing a Dex `double-to-float' instruction.
2578 locations->SetInAt(0, Location::RequiresFpuRegister());
2579 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002580 break;
2581
2582 default:
2583 LOG(FATAL) << "Unexpected type conversion from " << input_type
2584 << " to " << result_type;
2585 };
2586 break;
2587
Roland Levillaindff1f282014-11-05 14:15:05 +00002588 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002589 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002590 case Primitive::kPrimBoolean:
2591 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002592 case Primitive::kPrimByte:
2593 case Primitive::kPrimShort:
2594 case Primitive::kPrimInt:
2595 case Primitive::kPrimChar:
2596 // Processing a Dex `int-to-double' instruction.
2597 locations->SetInAt(0, Location::RequiresRegister());
2598 locations->SetOut(Location::RequiresFpuRegister());
2599 break;
2600
2601 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002602 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002603 locations->SetInAt(0, Location::Any());
2604 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002605 break;
2606
Roland Levillaincff13742014-11-17 14:32:17 +00002607 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002608 // Processing a Dex `float-to-double' instruction.
2609 locations->SetInAt(0, Location::RequiresFpuRegister());
2610 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002611 break;
2612
2613 default:
2614 LOG(FATAL) << "Unexpected type conversion from " << input_type
2615 << " to " << result_type;
2616 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002617 break;
2618
2619 default:
2620 LOG(FATAL) << "Unexpected type conversion from " << input_type
2621 << " to " << result_type;
2622 }
2623}
2624
2625void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2626 LocationSummary* locations = conversion->GetLocations();
2627 Location out = locations->Out();
2628 Location in = locations->InAt(0);
2629 Primitive::Type result_type = conversion->GetResultType();
2630 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002631 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002632 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002633 case Primitive::kPrimByte:
2634 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002635 case Primitive::kPrimLong:
2636 // Type conversion from long to byte is a result of code transformations.
2637 if (in.IsRegisterPair()) {
2638 __ movsxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2639 } else {
2640 DCHECK(in.GetConstant()->IsLongConstant());
2641 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2642 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2643 }
2644 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002645 case Primitive::kPrimBoolean:
2646 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002647 case Primitive::kPrimShort:
2648 case Primitive::kPrimInt:
2649 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002650 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002651 if (in.IsRegister()) {
2652 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002653 } else {
2654 DCHECK(in.GetConstant()->IsIntConstant());
2655 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2656 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2657 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002658 break;
2659
2660 default:
2661 LOG(FATAL) << "Unexpected type conversion from " << input_type
2662 << " to " << result_type;
2663 }
2664 break;
2665
Roland Levillain01a8d712014-11-14 16:27:39 +00002666 case Primitive::kPrimShort:
2667 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002668 case Primitive::kPrimLong:
2669 // Type conversion from long to short is a result of code transformations.
2670 if (in.IsRegisterPair()) {
2671 __ movsxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2672 } else if (in.IsDoubleStackSlot()) {
2673 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2674 } else {
2675 DCHECK(in.GetConstant()->IsLongConstant());
2676 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2677 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
2678 }
2679 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002680 case Primitive::kPrimBoolean:
2681 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002682 case Primitive::kPrimByte:
2683 case Primitive::kPrimInt:
2684 case Primitive::kPrimChar:
2685 // Processing a Dex `int-to-short' instruction.
2686 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002687 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002688 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002689 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002690 } else {
2691 DCHECK(in.GetConstant()->IsIntConstant());
2692 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002693 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002694 }
2695 break;
2696
2697 default:
2698 LOG(FATAL) << "Unexpected type conversion from " << input_type
2699 << " to " << result_type;
2700 }
2701 break;
2702
Roland Levillain946e1432014-11-11 17:35:19 +00002703 case Primitive::kPrimInt:
2704 switch (input_type) {
2705 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002706 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002707 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002708 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002709 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002710 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002711 } else {
2712 DCHECK(in.IsConstant());
2713 DCHECK(in.GetConstant()->IsLongConstant());
2714 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002715 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002716 }
2717 break;
2718
Roland Levillain3f8f9362014-12-02 17:45:01 +00002719 case Primitive::kPrimFloat: {
2720 // Processing a Dex `float-to-int' instruction.
2721 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2722 Register output = out.AsRegister<Register>();
2723 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002724 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002725
2726 __ movl(output, Immediate(kPrimIntMax));
2727 // temp = int-to-float(output)
2728 __ cvtsi2ss(temp, output);
2729 // if input >= temp goto done
2730 __ comiss(input, temp);
2731 __ j(kAboveEqual, &done);
2732 // if input == NaN goto nan
2733 __ j(kUnordered, &nan);
2734 // output = float-to-int-truncate(input)
2735 __ cvttss2si(output, input);
2736 __ jmp(&done);
2737 __ Bind(&nan);
2738 // output = 0
2739 __ xorl(output, output);
2740 __ Bind(&done);
2741 break;
2742 }
2743
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002744 case Primitive::kPrimDouble: {
2745 // Processing a Dex `double-to-int' instruction.
2746 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2747 Register output = out.AsRegister<Register>();
2748 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002749 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002750
2751 __ movl(output, Immediate(kPrimIntMax));
2752 // temp = int-to-double(output)
2753 __ cvtsi2sd(temp, output);
2754 // if input >= temp goto done
2755 __ comisd(input, temp);
2756 __ j(kAboveEqual, &done);
2757 // if input == NaN goto nan
2758 __ j(kUnordered, &nan);
2759 // output = double-to-int-truncate(input)
2760 __ cvttsd2si(output, input);
2761 __ jmp(&done);
2762 __ Bind(&nan);
2763 // output = 0
2764 __ xorl(output, output);
2765 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002766 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002767 }
Roland Levillain946e1432014-11-11 17:35:19 +00002768
2769 default:
2770 LOG(FATAL) << "Unexpected type conversion from " << input_type
2771 << " to " << result_type;
2772 }
2773 break;
2774
Roland Levillaindff1f282014-11-05 14:15:05 +00002775 case Primitive::kPrimLong:
2776 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002777 case Primitive::kPrimBoolean:
2778 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002779 case Primitive::kPrimByte:
2780 case Primitive::kPrimShort:
2781 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002782 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002783 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002784 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2785 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002786 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002787 __ cdq();
2788 break;
2789
2790 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002791 // Processing a Dex `float-to-long' instruction.
Serban Constantinescuba45db02016-07-12 22:53:02 +01002792 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002793 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002794 break;
2795
Roland Levillaindff1f282014-11-05 14:15:05 +00002796 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002797 // Processing a Dex `double-to-long' instruction.
Serban Constantinescuba45db02016-07-12 22:53:02 +01002798 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002799 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002800 break;
2801
2802 default:
2803 LOG(FATAL) << "Unexpected type conversion from " << input_type
2804 << " to " << result_type;
2805 }
2806 break;
2807
Roland Levillain981e4542014-11-14 11:47:14 +00002808 case Primitive::kPrimChar:
2809 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002810 case Primitive::kPrimLong:
2811 // Type conversion from long to short is a result of code transformations.
2812 if (in.IsRegisterPair()) {
2813 __ movzxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2814 } else if (in.IsDoubleStackSlot()) {
2815 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2816 } else {
2817 DCHECK(in.GetConstant()->IsLongConstant());
2818 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2819 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
2820 }
2821 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002822 case Primitive::kPrimBoolean:
2823 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002824 case Primitive::kPrimByte:
2825 case Primitive::kPrimShort:
2826 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002827 // Processing a Dex `Process a Dex `int-to-char'' instruction.
2828 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002829 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00002830 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002831 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00002832 } else {
2833 DCHECK(in.GetConstant()->IsIntConstant());
2834 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002835 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00002836 }
2837 break;
2838
2839 default:
2840 LOG(FATAL) << "Unexpected type conversion from " << input_type
2841 << " to " << result_type;
2842 }
2843 break;
2844
Roland Levillaindff1f282014-11-05 14:15:05 +00002845 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002846 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002847 case Primitive::kPrimBoolean:
2848 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002849 case Primitive::kPrimByte:
2850 case Primitive::kPrimShort:
2851 case Primitive::kPrimInt:
2852 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002853 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002854 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002855 break;
2856
Roland Levillain6d0e4832014-11-27 18:31:21 +00002857 case Primitive::kPrimLong: {
2858 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002859 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002860
Roland Levillain232ade02015-04-20 15:14:36 +01002861 // Create stack space for the call to
2862 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2863 // TODO: enhance register allocator to ask for stack temporaries.
2864 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
2865 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2866 __ subl(ESP, Immediate(adjustment));
2867 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002868
Roland Levillain232ade02015-04-20 15:14:36 +01002869 // Load the value to the FP stack, using temporaries if needed.
2870 PushOntoFPStack(in, 0, adjustment, false, true);
2871
2872 if (out.IsStackSlot()) {
2873 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2874 } else {
2875 __ fstps(Address(ESP, 0));
2876 Location stack_temp = Location::StackSlot(0);
2877 codegen_->Move32(out, stack_temp);
2878 }
2879
2880 // Remove the temporary stack space we allocated.
2881 if (adjustment != 0) {
2882 __ addl(ESP, Immediate(adjustment));
2883 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002884 break;
2885 }
2886
Roland Levillaincff13742014-11-17 14:32:17 +00002887 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002888 // Processing a Dex `double-to-float' instruction.
2889 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002890 break;
2891
2892 default:
2893 LOG(FATAL) << "Unexpected type conversion from " << input_type
2894 << " to " << result_type;
2895 };
2896 break;
2897
Roland Levillaindff1f282014-11-05 14:15:05 +00002898 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002899 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002900 case Primitive::kPrimBoolean:
2901 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002902 case Primitive::kPrimByte:
2903 case Primitive::kPrimShort:
2904 case Primitive::kPrimInt:
2905 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002906 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002907 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002908 break;
2909
Roland Levillain647b9ed2014-11-27 12:06:00 +00002910 case Primitive::kPrimLong: {
2911 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002912 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002913
Roland Levillain232ade02015-04-20 15:14:36 +01002914 // Create stack space for the call to
2915 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2916 // TODO: enhance register allocator to ask for stack temporaries.
2917 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
2918 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2919 __ subl(ESP, Immediate(adjustment));
2920 }
2921
2922 // Load the value to the FP stack, using temporaries if needed.
2923 PushOntoFPStack(in, 0, adjustment, false, true);
2924
2925 if (out.IsDoubleStackSlot()) {
2926 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2927 } else {
2928 __ fstpl(Address(ESP, 0));
2929 Location stack_temp = Location::DoubleStackSlot(0);
2930 codegen_->Move64(out, stack_temp);
2931 }
2932
2933 // Remove the temporary stack space we allocated.
2934 if (adjustment != 0) {
2935 __ addl(ESP, Immediate(adjustment));
2936 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002937 break;
2938 }
2939
Roland Levillaincff13742014-11-17 14:32:17 +00002940 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002941 // Processing a Dex `float-to-double' instruction.
2942 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002943 break;
2944
2945 default:
2946 LOG(FATAL) << "Unexpected type conversion from " << input_type
2947 << " to " << result_type;
2948 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002949 break;
2950
2951 default:
2952 LOG(FATAL) << "Unexpected type conversion from " << input_type
2953 << " to " << result_type;
2954 }
2955}
2956
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002957void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002958 LocationSummary* locations =
2959 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002960 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002961 case Primitive::kPrimInt: {
2962 locations->SetInAt(0, Location::RequiresRegister());
2963 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2964 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2965 break;
2966 }
2967
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002968 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002969 locations->SetInAt(0, Location::RequiresRegister());
2970 locations->SetInAt(1, Location::Any());
2971 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002972 break;
2973 }
2974
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002975 case Primitive::kPrimFloat:
2976 case Primitive::kPrimDouble: {
2977 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002978 if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2979 DCHECK(add->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002980 } else if (add->InputAt(1)->IsConstant()) {
2981 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002982 } else {
2983 locations->SetInAt(1, Location::Any());
2984 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002985 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002986 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002987 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002988
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002989 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002990 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2991 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002992 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002993}
2994
2995void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2996 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002997 Location first = locations->InAt(0);
2998 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002999 Location out = locations->Out();
3000
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003001 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003002 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003003 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05003004 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3005 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04003006 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
3007 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05003008 } else {
3009 __ leal(out.AsRegister<Register>(), Address(
3010 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
3011 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003012 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05003013 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
3014 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3015 __ addl(out.AsRegister<Register>(), Immediate(value));
3016 } else {
3017 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
3018 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003019 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05003020 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003021 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003022 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003023 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003024 }
3025
3026 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003027 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003028 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3029 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003030 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003031 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3032 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003033 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003034 } else {
3035 DCHECK(second.IsConstant()) << second;
3036 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3037 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3038 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003039 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003040 break;
3041 }
3042
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003043 case Primitive::kPrimFloat: {
3044 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003045 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04003046 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3047 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003048 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003049 __ addss(first.AsFpuRegister<XmmRegister>(),
3050 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003051 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3052 const_area->GetBaseMethodAddress(),
3053 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003054 } else {
3055 DCHECK(second.IsStackSlot());
3056 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003057 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003058 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003059 }
3060
3061 case Primitive::kPrimDouble: {
3062 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003063 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04003064 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3065 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003066 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003067 __ addsd(first.AsFpuRegister<XmmRegister>(),
3068 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003069 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3070 const_area->GetBaseMethodAddress(),
3071 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003072 } else {
3073 DCHECK(second.IsDoubleStackSlot());
3074 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003075 }
3076 break;
3077 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003078
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003079 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003080 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003081 }
3082}
3083
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003084void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003085 LocationSummary* locations =
3086 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003087 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003088 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003089 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003090 locations->SetInAt(0, Location::RequiresRegister());
3091 locations->SetInAt(1, Location::Any());
3092 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003093 break;
3094 }
Calin Juravle11351682014-10-23 15:38:15 +01003095 case Primitive::kPrimFloat:
3096 case Primitive::kPrimDouble: {
3097 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003098 if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3099 DCHECK(sub->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003100 } else if (sub->InputAt(1)->IsConstant()) {
3101 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003102 } else {
3103 locations->SetInAt(1, Location::Any());
3104 }
Calin Juravle11351682014-10-23 15:38:15 +01003105 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003106 break;
Calin Juravle11351682014-10-23 15:38:15 +01003107 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003108
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003109 default:
Calin Juravle11351682014-10-23 15:38:15 +01003110 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003111 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003112}
3113
3114void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
3115 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003116 Location first = locations->InAt(0);
3117 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01003118 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003119 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003120 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003121 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003122 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003123 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003124 __ subl(first.AsRegister<Register>(),
3125 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003126 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003127 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003128 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003129 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003130 }
3131
3132 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003133 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01003134 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3135 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003136 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01003137 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003138 __ sbbl(first.AsRegisterPairHigh<Register>(),
3139 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003140 } else {
3141 DCHECK(second.IsConstant()) << second;
3142 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3143 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3144 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003145 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003146 break;
3147 }
3148
Calin Juravle11351682014-10-23 15:38:15 +01003149 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003150 if (second.IsFpuRegister()) {
3151 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3152 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3153 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003154 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003155 __ subss(first.AsFpuRegister<XmmRegister>(),
3156 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003157 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3158 const_area->GetBaseMethodAddress(),
3159 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003160 } else {
3161 DCHECK(second.IsStackSlot());
3162 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3163 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003164 break;
Calin Juravle11351682014-10-23 15:38:15 +01003165 }
3166
3167 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003168 if (second.IsFpuRegister()) {
3169 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3170 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3171 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003172 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003173 __ subsd(first.AsFpuRegister<XmmRegister>(),
3174 codegen_->LiteralDoubleAddress(
3175 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003176 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003177 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3178 } else {
3179 DCHECK(second.IsDoubleStackSlot());
3180 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3181 }
Calin Juravle11351682014-10-23 15:38:15 +01003182 break;
3183 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003184
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003185 default:
Calin Juravle11351682014-10-23 15:38:15 +01003186 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003187 }
3188}
3189
Calin Juravle34bacdf2014-10-07 20:23:36 +01003190void LocationsBuilderX86::VisitMul(HMul* mul) {
3191 LocationSummary* locations =
3192 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3193 switch (mul->GetResultType()) {
3194 case Primitive::kPrimInt:
3195 locations->SetInAt(0, Location::RequiresRegister());
3196 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003197 if (mul->InputAt(1)->IsIntConstant()) {
3198 // Can use 3 operand multiply.
3199 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3200 } else {
3201 locations->SetOut(Location::SameAsFirstInput());
3202 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003203 break;
3204 case Primitive::kPrimLong: {
3205 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003206 locations->SetInAt(1, Location::Any());
3207 locations->SetOut(Location::SameAsFirstInput());
3208 // Needed for imul on 32bits with 64bits output.
3209 locations->AddTemp(Location::RegisterLocation(EAX));
3210 locations->AddTemp(Location::RegisterLocation(EDX));
3211 break;
3212 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003213 case Primitive::kPrimFloat:
3214 case Primitive::kPrimDouble: {
3215 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003216 if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3217 DCHECK(mul->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003218 } else if (mul->InputAt(1)->IsConstant()) {
3219 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003220 } else {
3221 locations->SetInAt(1, Location::Any());
3222 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003223 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003224 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003225 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003226
3227 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003228 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003229 }
3230}
3231
3232void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
3233 LocationSummary* locations = mul->GetLocations();
3234 Location first = locations->InAt(0);
3235 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003236 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003237
3238 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003239 case Primitive::kPrimInt:
3240 // The constant may have ended up in a register, so test explicitly to avoid
3241 // problems where the output may not be the same as the first operand.
3242 if (mul->InputAt(1)->IsIntConstant()) {
3243 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3244 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
3245 } else if (second.IsRegister()) {
3246 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003247 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003248 } else {
3249 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003250 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003251 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003252 }
3253 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003254
3255 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003256 Register in1_hi = first.AsRegisterPairHigh<Register>();
3257 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003258 Register eax = locations->GetTemp(0).AsRegister<Register>();
3259 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003260
3261 DCHECK_EQ(EAX, eax);
3262 DCHECK_EQ(EDX, edx);
3263
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003264 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01003265 // output: in1
3266 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3267 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3268 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003269 if (second.IsConstant()) {
3270 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003271
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003272 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3273 int32_t low_value = Low32Bits(value);
3274 int32_t high_value = High32Bits(value);
3275 Immediate low(low_value);
3276 Immediate high(high_value);
3277
3278 __ movl(eax, high);
3279 // eax <- in1.lo * in2.hi
3280 __ imull(eax, in1_lo);
3281 // in1.hi <- in1.hi * in2.lo
3282 __ imull(in1_hi, low);
3283 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3284 __ addl(in1_hi, eax);
3285 // move in2_lo to eax to prepare for double precision
3286 __ movl(eax, low);
3287 // edx:eax <- in1.lo * in2.lo
3288 __ mull(in1_lo);
3289 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3290 __ addl(in1_hi, edx);
3291 // in1.lo <- (in1.lo * in2.lo)[31:0];
3292 __ movl(in1_lo, eax);
3293 } else if (second.IsRegisterPair()) {
3294 Register in2_hi = second.AsRegisterPairHigh<Register>();
3295 Register in2_lo = second.AsRegisterPairLow<Register>();
3296
3297 __ movl(eax, in2_hi);
3298 // eax <- in1.lo * in2.hi
3299 __ imull(eax, in1_lo);
3300 // in1.hi <- in1.hi * in2.lo
3301 __ imull(in1_hi, in2_lo);
3302 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3303 __ addl(in1_hi, eax);
3304 // move in1_lo to eax to prepare for double precision
3305 __ movl(eax, in1_lo);
3306 // edx:eax <- in1.lo * in2.lo
3307 __ mull(in2_lo);
3308 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3309 __ addl(in1_hi, edx);
3310 // in1.lo <- (in1.lo * in2.lo)[31:0];
3311 __ movl(in1_lo, eax);
3312 } else {
3313 DCHECK(second.IsDoubleStackSlot()) << second;
3314 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
3315 Address in2_lo(ESP, second.GetStackIndex());
3316
3317 __ movl(eax, in2_hi);
3318 // eax <- in1.lo * in2.hi
3319 __ imull(eax, in1_lo);
3320 // in1.hi <- in1.hi * in2.lo
3321 __ imull(in1_hi, in2_lo);
3322 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3323 __ addl(in1_hi, eax);
3324 // move in1_lo to eax to prepare for double precision
3325 __ movl(eax, in1_lo);
3326 // edx:eax <- in1.lo * in2.lo
3327 __ mull(in2_lo);
3328 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3329 __ addl(in1_hi, edx);
3330 // in1.lo <- (in1.lo * in2.lo)[31:0];
3331 __ movl(in1_lo, eax);
3332 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003333
3334 break;
3335 }
3336
Calin Juravleb5bfa962014-10-21 18:02:24 +01003337 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003338 DCHECK(first.Equals(locations->Out()));
3339 if (second.IsFpuRegister()) {
3340 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3341 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3342 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003343 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003344 __ mulss(first.AsFpuRegister<XmmRegister>(),
3345 codegen_->LiteralFloatAddress(
3346 const_area->GetConstant()->AsFloatConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003347 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003348 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3349 } else {
3350 DCHECK(second.IsStackSlot());
3351 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3352 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003353 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003354 }
3355
3356 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003357 DCHECK(first.Equals(locations->Out()));
3358 if (second.IsFpuRegister()) {
3359 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3360 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3361 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003362 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003363 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3364 codegen_->LiteralDoubleAddress(
3365 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003366 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003367 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3368 } else {
3369 DCHECK(second.IsDoubleStackSlot());
3370 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3371 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003372 break;
3373 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003374
3375 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003376 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003377 }
3378}
3379
Roland Levillain232ade02015-04-20 15:14:36 +01003380void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3381 uint32_t temp_offset,
3382 uint32_t stack_adjustment,
3383 bool is_fp,
3384 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003385 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003386 DCHECK(!is_wide);
3387 if (is_fp) {
3388 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3389 } else {
3390 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3391 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003392 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003393 DCHECK(is_wide);
3394 if (is_fp) {
3395 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3396 } else {
3397 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3398 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003399 } else {
3400 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003401 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003402 Location stack_temp = Location::StackSlot(temp_offset);
3403 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003404 if (is_fp) {
3405 __ flds(Address(ESP, temp_offset));
3406 } else {
3407 __ filds(Address(ESP, temp_offset));
3408 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003409 } else {
3410 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3411 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003412 if (is_fp) {
3413 __ fldl(Address(ESP, temp_offset));
3414 } else {
3415 __ fildl(Address(ESP, temp_offset));
3416 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003417 }
3418 }
3419}
3420
3421void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
3422 Primitive::Type type = rem->GetResultType();
3423 bool is_float = type == Primitive::kPrimFloat;
3424 size_t elem_size = Primitive::ComponentSize(type);
3425 LocationSummary* locations = rem->GetLocations();
3426 Location first = locations->InAt(0);
3427 Location second = locations->InAt(1);
3428 Location out = locations->Out();
3429
3430 // Create stack space for 2 elements.
3431 // TODO: enhance register allocator to ask for stack temporaries.
3432 __ subl(ESP, Immediate(2 * elem_size));
3433
3434 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003435 const bool is_wide = !is_float;
3436 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
3437 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003438
3439 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003440 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003441 __ Bind(&retry);
3442 __ fprem();
3443
3444 // Move FP status to AX.
3445 __ fstsw();
3446
3447 // And see if the argument reduction is complete. This is signaled by the
3448 // C2 FPU flag bit set to 0.
3449 __ andl(EAX, Immediate(kC2ConditionMask));
3450 __ j(kNotEqual, &retry);
3451
3452 // We have settled on the final value. Retrieve it into an XMM register.
3453 // Store FP top of stack to real stack.
3454 if (is_float) {
3455 __ fsts(Address(ESP, 0));
3456 } else {
3457 __ fstl(Address(ESP, 0));
3458 }
3459
3460 // Pop the 2 items from the FP stack.
3461 __ fucompp();
3462
3463 // Load the value from the stack into an XMM register.
3464 DCHECK(out.IsFpuRegister()) << out;
3465 if (is_float) {
3466 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3467 } else {
3468 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3469 }
3470
3471 // And remove the temporary stack space we allocated.
3472 __ addl(ESP, Immediate(2 * elem_size));
3473}
3474
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003475
3476void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3477 DCHECK(instruction->IsDiv() || instruction->IsRem());
3478
3479 LocationSummary* locations = instruction->GetLocations();
3480 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003481 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003482
3483 Register out_register = locations->Out().AsRegister<Register>();
3484 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003485 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003486
3487 DCHECK(imm == 1 || imm == -1);
3488
3489 if (instruction->IsRem()) {
3490 __ xorl(out_register, out_register);
3491 } else {
3492 __ movl(out_register, input_register);
3493 if (imm == -1) {
3494 __ negl(out_register);
3495 }
3496 }
3497}
3498
3499
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003500void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003501 LocationSummary* locations = instruction->GetLocations();
3502
3503 Register out_register = locations->Out().AsRegister<Register>();
3504 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003505 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003506 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3507 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003508
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003509 Register num = locations->GetTemp(0).AsRegister<Register>();
3510
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003511 __ leal(num, Address(input_register, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003512 __ testl(input_register, input_register);
3513 __ cmovl(kGreaterEqual, num, input_register);
3514 int shift = CTZ(imm);
3515 __ sarl(num, Immediate(shift));
3516
3517 if (imm < 0) {
3518 __ negl(num);
3519 }
3520
3521 __ movl(out_register, num);
3522}
3523
3524void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3525 DCHECK(instruction->IsDiv() || instruction->IsRem());
3526
3527 LocationSummary* locations = instruction->GetLocations();
3528 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3529
3530 Register eax = locations->InAt(0).AsRegister<Register>();
3531 Register out = locations->Out().AsRegister<Register>();
3532 Register num;
3533 Register edx;
3534
3535 if (instruction->IsDiv()) {
3536 edx = locations->GetTemp(0).AsRegister<Register>();
3537 num = locations->GetTemp(1).AsRegister<Register>();
3538 } else {
3539 edx = locations->Out().AsRegister<Register>();
3540 num = locations->GetTemp(0).AsRegister<Register>();
3541 }
3542
3543 DCHECK_EQ(EAX, eax);
3544 DCHECK_EQ(EDX, edx);
3545 if (instruction->IsDiv()) {
3546 DCHECK_EQ(EAX, out);
3547 } else {
3548 DCHECK_EQ(EDX, out);
3549 }
3550
3551 int64_t magic;
3552 int shift;
3553 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3554
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003555 // Save the numerator.
3556 __ movl(num, eax);
3557
3558 // EAX = magic
3559 __ movl(eax, Immediate(magic));
3560
3561 // EDX:EAX = magic * numerator
3562 __ imull(num);
3563
3564 if (imm > 0 && magic < 0) {
3565 // EDX += num
3566 __ addl(edx, num);
3567 } else if (imm < 0 && magic > 0) {
3568 __ subl(edx, num);
3569 }
3570
3571 // Shift if needed.
3572 if (shift != 0) {
3573 __ sarl(edx, Immediate(shift));
3574 }
3575
3576 // EDX += 1 if EDX < 0
3577 __ movl(eax, edx);
3578 __ shrl(edx, Immediate(31));
3579 __ addl(edx, eax);
3580
3581 if (instruction->IsRem()) {
3582 __ movl(eax, num);
3583 __ imull(edx, Immediate(imm));
3584 __ subl(eax, edx);
3585 __ movl(edx, eax);
3586 } else {
3587 __ movl(eax, edx);
3588 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003589}
3590
Calin Juravlebacfec32014-11-14 15:54:36 +00003591void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3592 DCHECK(instruction->IsDiv() || instruction->IsRem());
3593
3594 LocationSummary* locations = instruction->GetLocations();
3595 Location out = locations->Out();
3596 Location first = locations->InAt(0);
3597 Location second = locations->InAt(1);
3598 bool is_div = instruction->IsDiv();
3599
3600 switch (instruction->GetResultType()) {
3601 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003602 DCHECK_EQ(EAX, first.AsRegister<Register>());
3603 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003604
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003605 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003606 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003607
3608 if (imm == 0) {
3609 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3610 } else if (imm == 1 || imm == -1) {
3611 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003612 } else if (is_div && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003613 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003614 } else {
3615 DCHECK(imm <= -2 || imm >= 2);
3616 GenerateDivRemWithAnyConstant(instruction);
3617 }
3618 } else {
David Srbecky9cd6d372016-02-09 15:24:47 +00003619 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(
3620 instruction, out.AsRegister<Register>(), is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003621 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003622
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003623 Register second_reg = second.AsRegister<Register>();
3624 // 0x80000000/-1 triggers an arithmetic exception!
3625 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3626 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003627
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003628 __ cmpl(second_reg, Immediate(-1));
3629 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003630
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003631 // edx:eax <- sign-extended of eax
3632 __ cdq();
3633 // eax = quotient, edx = remainder
3634 __ idivl(second_reg);
3635 __ Bind(slow_path->GetExitLabel());
3636 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003637 break;
3638 }
3639
3640 case Primitive::kPrimLong: {
3641 InvokeRuntimeCallingConvention calling_convention;
3642 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3643 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3644 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3645 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3646 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3647 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3648
3649 if (is_div) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003650 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003651 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003652 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003653 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003654 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003655 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003656 break;
3657 }
3658
3659 default:
3660 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3661 }
3662}
3663
Calin Juravle7c4954d2014-10-28 16:57:40 +00003664void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003665 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003666 ? LocationSummary::kCallOnMainOnly
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003667 : LocationSummary::kNoCall;
3668 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3669
Calin Juravle7c4954d2014-10-28 16:57:40 +00003670 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003671 case Primitive::kPrimInt: {
3672 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003673 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003674 locations->SetOut(Location::SameAsFirstInput());
3675 // Intel uses edx:eax as the dividend.
3676 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003677 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3678 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3679 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003680 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003681 locations->AddTemp(Location::RequiresRegister());
3682 }
Calin Juravled0d48522014-11-04 16:40:20 +00003683 break;
3684 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003685 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003686 InvokeRuntimeCallingConvention calling_convention;
3687 locations->SetInAt(0, Location::RegisterPairLocation(
3688 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3689 locations->SetInAt(1, Location::RegisterPairLocation(
3690 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3691 // Runtime helper puts the result in EAX, EDX.
3692 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003693 break;
3694 }
3695 case Primitive::kPrimFloat:
3696 case Primitive::kPrimDouble: {
3697 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003698 if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3699 DCHECK(div->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003700 } else if (div->InputAt(1)->IsConstant()) {
3701 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003702 } else {
3703 locations->SetInAt(1, Location::Any());
3704 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003705 locations->SetOut(Location::SameAsFirstInput());
3706 break;
3707 }
3708
3709 default:
3710 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3711 }
3712}
3713
3714void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3715 LocationSummary* locations = div->GetLocations();
3716 Location first = locations->InAt(0);
3717 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003718
3719 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003720 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00003721 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003722 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003723 break;
3724 }
3725
3726 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003727 if (second.IsFpuRegister()) {
3728 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3729 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3730 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003731 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003732 __ divss(first.AsFpuRegister<XmmRegister>(),
3733 codegen_->LiteralFloatAddress(
3734 const_area->GetConstant()->AsFloatConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003735 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003736 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3737 } else {
3738 DCHECK(second.IsStackSlot());
3739 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3740 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003741 break;
3742 }
3743
3744 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003745 if (second.IsFpuRegister()) {
3746 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3747 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3748 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003749 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003750 __ divsd(first.AsFpuRegister<XmmRegister>(),
3751 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003752 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3753 const_area->GetBaseMethodAddress(),
3754 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003755 } else {
3756 DCHECK(second.IsDoubleStackSlot());
3757 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3758 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003759 break;
3760 }
3761
3762 default:
3763 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3764 }
3765}
3766
Calin Juravlebacfec32014-11-14 15:54:36 +00003767void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003768 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003769
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003770 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003771 ? LocationSummary::kCallOnMainOnly
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003772 : LocationSummary::kNoCall;
3773 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003774
Calin Juravled2ec87d2014-12-08 14:24:46 +00003775 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003776 case Primitive::kPrimInt: {
3777 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003778 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003779 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003780 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3781 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3782 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003783 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003784 locations->AddTemp(Location::RequiresRegister());
3785 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003786 break;
3787 }
3788 case Primitive::kPrimLong: {
3789 InvokeRuntimeCallingConvention calling_convention;
3790 locations->SetInAt(0, Location::RegisterPairLocation(
3791 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3792 locations->SetInAt(1, Location::RegisterPairLocation(
3793 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3794 // Runtime helper puts the result in EAX, EDX.
3795 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3796 break;
3797 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003798 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003799 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003800 locations->SetInAt(0, Location::Any());
3801 locations->SetInAt(1, Location::Any());
3802 locations->SetOut(Location::RequiresFpuRegister());
3803 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003804 break;
3805 }
3806
3807 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003808 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003809 }
3810}
3811
3812void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
3813 Primitive::Type type = rem->GetResultType();
3814 switch (type) {
3815 case Primitive::kPrimInt:
3816 case Primitive::kPrimLong: {
3817 GenerateDivRemIntegral(rem);
3818 break;
3819 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003820 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00003821 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003822 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003823 break;
3824 }
3825 default:
3826 LOG(FATAL) << "Unexpected rem type " << type;
3827 }
3828}
3829
Calin Juravled0d48522014-11-04 16:40:20 +00003830void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003831 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003832 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003833 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003834 case Primitive::kPrimByte:
3835 case Primitive::kPrimChar:
3836 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003837 case Primitive::kPrimInt: {
3838 locations->SetInAt(0, Location::Any());
3839 break;
3840 }
3841 case Primitive::kPrimLong: {
3842 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
3843 if (!instruction->IsConstant()) {
3844 locations->AddTemp(Location::RequiresRegister());
3845 }
3846 break;
3847 }
3848 default:
3849 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3850 }
Calin Juravled0d48522014-11-04 16:40:20 +00003851}
3852
3853void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003854 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003855 codegen_->AddSlowPath(slow_path);
3856
3857 LocationSummary* locations = instruction->GetLocations();
3858 Location value = locations->InAt(0);
3859
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003860 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003861 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003862 case Primitive::kPrimByte:
3863 case Primitive::kPrimChar:
3864 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003865 case Primitive::kPrimInt: {
3866 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003867 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003868 __ j(kEqual, slow_path->GetEntryLabel());
3869 } else if (value.IsStackSlot()) {
3870 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
3871 __ j(kEqual, slow_path->GetEntryLabel());
3872 } else {
3873 DCHECK(value.IsConstant()) << value;
3874 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01003875 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003876 }
3877 }
3878 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003879 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003880 case Primitive::kPrimLong: {
3881 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003882 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003883 __ movl(temp, value.AsRegisterPairLow<Register>());
3884 __ orl(temp, value.AsRegisterPairHigh<Register>());
3885 __ j(kEqual, slow_path->GetEntryLabel());
3886 } else {
3887 DCHECK(value.IsConstant()) << value;
3888 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3889 __ jmp(slow_path->GetEntryLabel());
3890 }
3891 }
3892 break;
3893 }
3894 default:
3895 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003896 }
Calin Juravled0d48522014-11-04 16:40:20 +00003897}
3898
Calin Juravle9aec02f2014-11-18 23:06:35 +00003899void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3900 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3901
3902 LocationSummary* locations =
3903 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3904
3905 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00003906 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00003907 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003908 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003909 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003910 // The shift count needs to be in CL or a constant.
3911 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003912 locations->SetOut(Location::SameAsFirstInput());
3913 break;
3914 }
3915 default:
3916 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3917 }
3918}
3919
3920void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3921 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3922
3923 LocationSummary* locations = op->GetLocations();
3924 Location first = locations->InAt(0);
3925 Location second = locations->InAt(1);
3926 DCHECK(first.Equals(locations->Out()));
3927
3928 switch (op->GetResultType()) {
3929 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00003930 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003931 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003932 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003933 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003934 DCHECK_EQ(ECX, second_reg);
3935 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003936 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003937 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003938 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003939 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003940 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003941 }
3942 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003943 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003944 if (shift == 0) {
3945 return;
3946 }
3947 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003948 if (op->IsShl()) {
3949 __ shll(first_reg, imm);
3950 } else if (op->IsShr()) {
3951 __ sarl(first_reg, imm);
3952 } else {
3953 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003954 }
3955 }
3956 break;
3957 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003958 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003959 if (second.IsRegister()) {
3960 Register second_reg = second.AsRegister<Register>();
3961 DCHECK_EQ(ECX, second_reg);
3962 if (op->IsShl()) {
3963 GenerateShlLong(first, second_reg);
3964 } else if (op->IsShr()) {
3965 GenerateShrLong(first, second_reg);
3966 } else {
3967 GenerateUShrLong(first, second_reg);
3968 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003969 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003970 // Shift by a constant.
Roland Levillain5b5b9312016-03-22 14:57:31 +00003971 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003972 // Nothing to do if the shift is 0, as the input is already the output.
3973 if (shift != 0) {
3974 if (op->IsShl()) {
3975 GenerateShlLong(first, shift);
3976 } else if (op->IsShr()) {
3977 GenerateShrLong(first, shift);
3978 } else {
3979 GenerateUShrLong(first, shift);
3980 }
3981 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003982 }
3983 break;
3984 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003985 default:
3986 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3987 }
3988}
3989
Mark P Mendell73945692015-04-29 14:56:17 +00003990void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3991 Register low = loc.AsRegisterPairLow<Register>();
3992 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003993 if (shift == 1) {
3994 // This is just an addition.
3995 __ addl(low, low);
3996 __ adcl(high, high);
3997 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003998 // Shift by 32 is easy. High gets low, and low gets 0.
3999 codegen_->EmitParallelMoves(
4000 loc.ToLow(),
4001 loc.ToHigh(),
4002 Primitive::kPrimInt,
4003 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
4004 loc.ToLow(),
4005 Primitive::kPrimInt);
4006 } else if (shift > 32) {
4007 // Low part becomes 0. High part is low part << (shift-32).
4008 __ movl(high, low);
4009 __ shll(high, Immediate(shift - 32));
4010 __ xorl(low, low);
4011 } else {
4012 // Between 1 and 31.
4013 __ shld(high, low, Immediate(shift));
4014 __ shll(low, Immediate(shift));
4015 }
4016}
4017
Calin Juravle9aec02f2014-11-18 23:06:35 +00004018void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004019 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004020 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
4021 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
4022 __ testl(shifter, Immediate(32));
4023 __ j(kEqual, &done);
4024 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
4025 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
4026 __ Bind(&done);
4027}
4028
Mark P Mendell73945692015-04-29 14:56:17 +00004029void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
4030 Register low = loc.AsRegisterPairLow<Register>();
4031 Register high = loc.AsRegisterPairHigh<Register>();
4032 if (shift == 32) {
4033 // Need to copy the sign.
4034 DCHECK_NE(low, high);
4035 __ movl(low, high);
4036 __ sarl(high, Immediate(31));
4037 } else if (shift > 32) {
4038 DCHECK_NE(low, high);
4039 // High part becomes sign. Low part is shifted by shift - 32.
4040 __ movl(low, high);
4041 __ sarl(high, Immediate(31));
4042 __ sarl(low, Immediate(shift - 32));
4043 } else {
4044 // Between 1 and 31.
4045 __ shrd(low, high, Immediate(shift));
4046 __ sarl(high, Immediate(shift));
4047 }
4048}
4049
Calin Juravle9aec02f2014-11-18 23:06:35 +00004050void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004051 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004052 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4053 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
4054 __ testl(shifter, Immediate(32));
4055 __ j(kEqual, &done);
4056 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4057 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
4058 __ Bind(&done);
4059}
4060
Mark P Mendell73945692015-04-29 14:56:17 +00004061void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
4062 Register low = loc.AsRegisterPairLow<Register>();
4063 Register high = loc.AsRegisterPairHigh<Register>();
4064 if (shift == 32) {
4065 // Shift by 32 is easy. Low gets high, and high gets 0.
4066 codegen_->EmitParallelMoves(
4067 loc.ToHigh(),
4068 loc.ToLow(),
4069 Primitive::kPrimInt,
4070 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
4071 loc.ToHigh(),
4072 Primitive::kPrimInt);
4073 } else if (shift > 32) {
4074 // Low part is high >> (shift - 32). High part becomes 0.
4075 __ movl(low, high);
4076 __ shrl(low, Immediate(shift - 32));
4077 __ xorl(high, high);
4078 } else {
4079 // Between 1 and 31.
4080 __ shrd(low, high, Immediate(shift));
4081 __ shrl(high, Immediate(shift));
4082 }
4083}
4084
Calin Juravle9aec02f2014-11-18 23:06:35 +00004085void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004086 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004087 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4088 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
4089 __ testl(shifter, Immediate(32));
4090 __ j(kEqual, &done);
4091 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4092 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
4093 __ Bind(&done);
4094}
4095
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004096void LocationsBuilderX86::VisitRor(HRor* ror) {
4097 LocationSummary* locations =
4098 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
4099
4100 switch (ror->GetResultType()) {
4101 case Primitive::kPrimLong:
4102 // Add the temporary needed.
4103 locations->AddTemp(Location::RequiresRegister());
4104 FALLTHROUGH_INTENDED;
4105 case Primitive::kPrimInt:
4106 locations->SetInAt(0, Location::RequiresRegister());
4107 // The shift count needs to be in CL (unless it is a constant).
4108 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
4109 locations->SetOut(Location::SameAsFirstInput());
4110 break;
4111 default:
4112 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4113 UNREACHABLE();
4114 }
4115}
4116
4117void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
4118 LocationSummary* locations = ror->GetLocations();
4119 Location first = locations->InAt(0);
4120 Location second = locations->InAt(1);
4121
4122 if (ror->GetResultType() == Primitive::kPrimInt) {
4123 Register first_reg = first.AsRegister<Register>();
4124 if (second.IsRegister()) {
4125 Register second_reg = second.AsRegister<Register>();
4126 __ rorl(first_reg, second_reg);
4127 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004128 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004129 __ rorl(first_reg, imm);
4130 }
4131 return;
4132 }
4133
4134 DCHECK_EQ(ror->GetResultType(), Primitive::kPrimLong);
4135 Register first_reg_lo = first.AsRegisterPairLow<Register>();
4136 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
4137 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
4138 if (second.IsRegister()) {
4139 Register second_reg = second.AsRegister<Register>();
4140 DCHECK_EQ(second_reg, ECX);
4141 __ movl(temp_reg, first_reg_hi);
4142 __ shrd(first_reg_hi, first_reg_lo, second_reg);
4143 __ shrd(first_reg_lo, temp_reg, second_reg);
4144 __ movl(temp_reg, first_reg_hi);
4145 __ testl(second_reg, Immediate(32));
4146 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
4147 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
4148 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004149 int32_t shift_amt = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004150 if (shift_amt == 0) {
4151 // Already fine.
4152 return;
4153 }
4154 if (shift_amt == 32) {
4155 // Just swap.
4156 __ movl(temp_reg, first_reg_lo);
4157 __ movl(first_reg_lo, first_reg_hi);
4158 __ movl(first_reg_hi, temp_reg);
4159 return;
4160 }
4161
4162 Immediate imm(shift_amt);
4163 // Save the constents of the low value.
4164 __ movl(temp_reg, first_reg_lo);
4165
4166 // Shift right into low, feeding bits from high.
4167 __ shrd(first_reg_lo, first_reg_hi, imm);
4168
4169 // Shift right into high, feeding bits from the original low.
4170 __ shrd(first_reg_hi, temp_reg, imm);
4171
4172 // Swap if needed.
4173 if (shift_amt > 32) {
4174 __ movl(temp_reg, first_reg_lo);
4175 __ movl(first_reg_lo, first_reg_hi);
4176 __ movl(first_reg_hi, temp_reg);
4177 }
4178 }
4179}
4180
Calin Juravle9aec02f2014-11-18 23:06:35 +00004181void LocationsBuilderX86::VisitShl(HShl* shl) {
4182 HandleShift(shl);
4183}
4184
4185void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
4186 HandleShift(shl);
4187}
4188
4189void LocationsBuilderX86::VisitShr(HShr* shr) {
4190 HandleShift(shr);
4191}
4192
4193void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
4194 HandleShift(shr);
4195}
4196
4197void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
4198 HandleShift(ushr);
4199}
4200
4201void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
4202 HandleShift(ushr);
4203}
4204
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004205void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004206 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004207 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004208 locations->SetOut(Location::RegisterLocation(EAX));
David Brazdil6de19382016-01-08 17:37:10 +00004209 if (instruction->IsStringAlloc()) {
4210 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4211 } else {
4212 InvokeRuntimeCallingConvention calling_convention;
4213 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00004214 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004215}
4216
4217void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004218 // Note: if heap poisoning is enabled, the entry point takes cares
4219 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004220 if (instruction->IsStringAlloc()) {
4221 // String is allocated through StringFactory. Call NewEmptyString entry point.
4222 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07004223 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004224 __ fs()->movl(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString)));
4225 __ call(Address(temp, code_offset.Int32Value()));
4226 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4227 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01004228 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00004229 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00004230 DCHECK(!codegen_->IsLeafMethod());
4231 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004232}
4233
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004234void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
4235 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004236 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004237 locations->SetOut(Location::RegisterLocation(EAX));
4238 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004239 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4240 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004241}
4242
4243void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004244 // Note: if heap poisoning is enabled, the entry point takes cares
4245 // of poisoning the reference.
Nicolas Geoffrayd0958442017-01-30 14:57:16 +00004246 QuickEntrypointEnum entrypoint =
4247 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
4248 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004249 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004250 DCHECK(!codegen_->IsLeafMethod());
4251}
4252
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004253void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004254 LocationSummary* locations =
4255 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004256 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4257 if (location.IsStackSlot()) {
4258 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4259 } else if (location.IsDoubleStackSlot()) {
4260 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004261 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004262 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004263}
4264
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004265void InstructionCodeGeneratorX86::VisitParameterValue(
4266 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4267}
4268
4269void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
4270 LocationSummary* locations =
4271 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4272 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4273}
4274
4275void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004276}
4277
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004278void LocationsBuilderX86::VisitClassTableGet(HClassTableGet* instruction) {
4279 LocationSummary* locations =
4280 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4281 locations->SetInAt(0, Location::RequiresRegister());
4282 locations->SetOut(Location::RequiresRegister());
4283}
4284
4285void InstructionCodeGeneratorX86::VisitClassTableGet(HClassTableGet* instruction) {
4286 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004287 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004288 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004289 instruction->GetIndex(), kX86PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004290 __ movl(locations->Out().AsRegister<Register>(),
4291 Address(locations->InAt(0).AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004292 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004293 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004294 instruction->GetIndex(), kX86PointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004295 __ movl(locations->Out().AsRegister<Register>(),
4296 Address(locations->InAt(0).AsRegister<Register>(),
4297 mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
4298 // temp = temp->GetImtEntryAt(method_offset);
4299 __ movl(locations->Out().AsRegister<Register>(),
4300 Address(locations->Out().AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004301 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004302}
4303
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004304void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004305 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004306 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004307 locations->SetInAt(0, Location::RequiresRegister());
4308 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004309}
4310
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004311void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
4312 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01004313 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004314 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01004315 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004316 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004317 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004318 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004319 break;
4320
4321 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01004322 __ notl(out.AsRegisterPairLow<Register>());
4323 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004324 break;
4325
4326 default:
4327 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4328 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004329}
4330
David Brazdil66d126e2015-04-03 16:02:44 +01004331void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
4332 LocationSummary* locations =
4333 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4334 locations->SetInAt(0, Location::RequiresRegister());
4335 locations->SetOut(Location::SameAsFirstInput());
4336}
4337
4338void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004339 LocationSummary* locations = bool_not->GetLocations();
4340 Location in = locations->InAt(0);
4341 Location out = locations->Out();
4342 DCHECK(in.Equals(out));
4343 __ xorl(out.AsRegister<Register>(), Immediate(1));
4344}
4345
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004346void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004347 LocationSummary* locations =
4348 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004349 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004350 case Primitive::kPrimBoolean:
4351 case Primitive::kPrimByte:
4352 case Primitive::kPrimShort:
4353 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004354 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00004355 case Primitive::kPrimLong: {
4356 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00004357 locations->SetInAt(1, Location::Any());
4358 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4359 break;
4360 }
4361 case Primitive::kPrimFloat:
4362 case Primitive::kPrimDouble: {
4363 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004364 if (compare->InputAt(1)->IsX86LoadFromConstantTable()) {
4365 DCHECK(compare->InputAt(1)->IsEmittedAtUseSite());
4366 } else if (compare->InputAt(1)->IsConstant()) {
4367 locations->SetInAt(1, Location::RequiresFpuRegister());
4368 } else {
4369 locations->SetInAt(1, Location::Any());
4370 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004371 locations->SetOut(Location::RequiresRegister());
4372 break;
4373 }
4374 default:
4375 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4376 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004377}
4378
4379void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004380 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004381 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004382 Location left = locations->InAt(0);
4383 Location right = locations->InAt(1);
4384
Mark Mendell0c9497d2015-08-21 09:30:05 -04004385 NearLabel less, greater, done;
Aart Bika19616e2016-02-01 18:57:58 -08004386 Condition less_cond = kLess;
4387
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004388 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004389 case Primitive::kPrimBoolean:
4390 case Primitive::kPrimByte:
4391 case Primitive::kPrimShort:
4392 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004393 case Primitive::kPrimInt: {
Roland Levillain0b671c02016-08-19 12:02:34 +01004394 codegen_->GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08004395 break;
4396 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004397 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004398 Register left_low = left.AsRegisterPairLow<Register>();
4399 Register left_high = left.AsRegisterPairHigh<Register>();
4400 int32_t val_low = 0;
4401 int32_t val_high = 0;
4402 bool right_is_const = false;
4403
4404 if (right.IsConstant()) {
4405 DCHECK(right.GetConstant()->IsLongConstant());
4406 right_is_const = true;
4407 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
4408 val_low = Low32Bits(val);
4409 val_high = High32Bits(val);
4410 }
4411
Calin Juravleddb7df22014-11-25 20:56:51 +00004412 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004413 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004414 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004415 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004416 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004417 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004418 codegen_->Compare32BitValue(left_high, val_high);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004419 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004420 __ j(kLess, &less); // Signed compare.
4421 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004422 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004423 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004424 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004425 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004426 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004427 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004428 codegen_->Compare32BitValue(left_low, val_low);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004429 }
Aart Bika19616e2016-02-01 18:57:58 -08004430 less_cond = kBelow; // for CF (unsigned).
Calin Juravleddb7df22014-11-25 20:56:51 +00004431 break;
4432 }
4433 case Primitive::kPrimFloat: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004434 GenerateFPCompare(left, right, compare, false);
Calin Juravleddb7df22014-11-25 20:56:51 +00004435 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004436 less_cond = kBelow; // for CF (floats).
Calin Juravleddb7df22014-11-25 20:56:51 +00004437 break;
4438 }
4439 case Primitive::kPrimDouble: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004440 GenerateFPCompare(left, right, compare, true);
Calin Juravleddb7df22014-11-25 20:56:51 +00004441 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004442 less_cond = kBelow; // for CF (floats).
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004443 break;
4444 }
4445 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004446 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004447 }
Aart Bika19616e2016-02-01 18:57:58 -08004448
Calin Juravleddb7df22014-11-25 20:56:51 +00004449 __ movl(out, Immediate(0));
4450 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08004451 __ j(less_cond, &less);
Calin Juravleddb7df22014-11-25 20:56:51 +00004452
4453 __ Bind(&greater);
4454 __ movl(out, Immediate(1));
4455 __ jmp(&done);
4456
4457 __ Bind(&less);
4458 __ movl(out, Immediate(-1));
4459
4460 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004461}
4462
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004463void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004464 LocationSummary* locations =
4465 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004466 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004467 locations->SetInAt(i, Location::Any());
4468 }
4469 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004470}
4471
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004472void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004473 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004474}
4475
Roland Levillain7c1559a2015-12-15 10:55:36 +00004476void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004477 /*
4478 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
4479 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
4480 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4481 */
4482 switch (kind) {
4483 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004484 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004485 break;
4486 }
4487 case MemBarrierKind::kAnyStore:
4488 case MemBarrierKind::kLoadAny:
4489 case MemBarrierKind::kStoreStore: {
4490 // nop
4491 break;
4492 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004493 case MemBarrierKind::kNTStoreStore:
4494 // Non-Temporal Store/Store needs an explicit fence.
4495 MemoryFence(/* non-temporal */ true);
4496 break;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004497 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004498}
4499
Vladimir Markodc151b22015-10-15 18:02:30 +01004500HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
4501 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004502 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004503 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004504}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004505
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004506Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4507 Register temp) {
4508 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Markoc53c0792015-11-19 15:48:33 +00004509 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004510 if (!invoke->GetLocations()->Intrinsified()) {
4511 return location.AsRegister<Register>();
4512 }
4513 // For intrinsics we allow any location, so it may be on the stack.
4514 if (!location.IsRegister()) {
4515 __ movl(temp, Address(ESP, location.GetStackIndex()));
4516 return temp;
4517 }
4518 // For register locations, check if the register was saved. If so, get it from the stack.
4519 // Note: There is a chance that the register was saved but not overwritten, so we could
4520 // save one load. However, since this is just an intrinsic slow path we prefer this
4521 // simple and more robust approach rather that trying to determine if that's the case.
4522 SlowPathCode* slow_path = GetCurrentSlowPath();
Vladimir Marko4ee8e292017-06-02 15:39:30 +00004523 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
4524 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4525 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4526 __ movl(temp, Address(ESP, stack_offset));
4527 return temp;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004528 }
4529 return location.AsRegister<Register>();
4530}
4531
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004532void CodeGeneratorX86::GenerateStaticOrDirectCall(
4533 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Vladimir Marko58155012015-08-19 12:49:41 +00004534 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4535 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004536 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00004537 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004538 uint32_t offset =
4539 GetThreadOffset<kX86PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
4540 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004541 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004542 }
Vladimir Marko58155012015-08-19 12:49:41 +00004543 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004544 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004545 break;
Vladimir Marko65979462017-05-19 17:25:12 +01004546 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
4547 DCHECK(GetCompilerOptions().IsBootImage());
4548 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4549 temp.AsRegister<Register>());
4550 __ leal(temp.AsRegister<Register>(), Address(base_reg, CodeGeneratorX86::kDummy32BitOffset));
4551 RecordBootMethodPatch(invoke);
4552 break;
4553 }
Vladimir Marko58155012015-08-19 12:49:41 +00004554 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4555 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
4556 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004557 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004558 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4559 temp.AsRegister<Register>());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004560 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004561 // Bind a new fixup label at the end of the "movl" insn.
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004562 __ Bind(NewMethodBssEntryPatch(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004563 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress(),
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004564 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex())));
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004565 break;
4566 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004567 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
4568 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
4569 return; // No code pointer retrieval; the runtime performs the call directly.
Vladimir Marko9b688a02015-05-06 14:12:42 +01004570 }
Vladimir Marko58155012015-08-19 12:49:41 +00004571 }
4572
4573 switch (invoke->GetCodePtrLocation()) {
4574 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4575 __ call(GetFrameEntryLabel());
4576 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004577 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4578 // (callee_method + offset_of_quick_compiled_code)()
4579 __ call(Address(callee_method.AsRegister<Register>(),
4580 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07004581 kX86PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004582 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04004583 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004584 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Mark Mendell09ed1a32015-03-25 08:30:06 -04004585
4586 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04004587}
4588
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004589void CodeGeneratorX86::GenerateVirtualCall(
4590 HInvokeVirtual* invoke, Location temp_in, SlowPathCode* slow_path) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004591 Register temp = temp_in.AsRegister<Register>();
4592 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4593 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004594
4595 // Use the calling convention instead of the location of the receiver, as
4596 // intrinsics may have put the receiver in a different register. In the intrinsics
4597 // slow path, the arguments have been moved to the right place, so here we are
4598 // guaranteed that the receiver is the first register of the calling convention.
4599 InvokeDexCallingConvention calling_convention;
4600 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004601 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004602 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004603 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004604 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004605 // Instead of simply (possibly) unpoisoning `temp` here, we should
4606 // emit a read barrier for the previous class reference load.
4607 // However this is not required in practice, as this is an
4608 // intermediate/temporary reference and because the current
4609 // concurrent copying collector keeps the from-space memory
4610 // intact/accessible until the end of the marking phase (the
4611 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004612 __ MaybeUnpoisonHeapReference(temp);
4613 // temp = temp->GetMethodAt(method_offset);
4614 __ movl(temp, Address(temp, method_offset));
4615 // call temp->GetEntryPoint();
4616 __ call(Address(
Andreas Gampe542451c2016-07-26 09:02:02 -07004617 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004618 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004619}
4620
Vladimir Marko65979462017-05-19 17:25:12 +01004621void CodeGeneratorX86::RecordBootMethodPatch(HInvokeStaticOrDirect* invoke) {
4622 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
4623 HX86ComputeBaseMethodAddress* address =
4624 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
4625 boot_image_method_patches_.emplace_back(address,
4626 *invoke->GetTargetMethod().dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07004627 invoke->GetTargetMethod().index);
Vladimir Marko65979462017-05-19 17:25:12 +01004628 __ Bind(&boot_image_method_patches_.back().label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004629}
4630
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004631Label* CodeGeneratorX86::NewMethodBssEntryPatch(
4632 HX86ComputeBaseMethodAddress* method_address,
4633 MethodReference target_method) {
4634 // Add the patch entry and bind its label at the end of the instruction.
4635 method_bss_entry_patches_.emplace_back(method_address,
4636 *target_method.dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07004637 target_method.index);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004638 return &method_bss_entry_patches_.back().label;
4639}
4640
Vladimir Marko1998cd02017-01-13 13:02:58 +00004641void CodeGeneratorX86::RecordBootTypePatch(HLoadClass* load_class) {
Vladimir Marko00916b92017-05-17 17:45:45 +01004642 HX86ComputeBaseMethodAddress* address = load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004643 boot_image_type_patches_.emplace_back(address,
4644 load_class->GetDexFile(),
Vladimir Marko1998cd02017-01-13 13:02:58 +00004645 load_class->GetTypeIndex().index_);
4646 __ Bind(&boot_image_type_patches_.back().label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004647}
4648
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004649Label* CodeGeneratorX86::NewTypeBssEntryPatch(HLoadClass* load_class) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004650 HX86ComputeBaseMethodAddress* address =
4651 load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
4652 type_bss_entry_patches_.emplace_back(
4653 address, load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00004654 return &type_bss_entry_patches_.back().label;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004655}
4656
Vladimir Marko65979462017-05-19 17:25:12 +01004657void CodeGeneratorX86::RecordBootStringPatch(HLoadString* load_string) {
Vladimir Marko65979462017-05-19 17:25:12 +01004658 HX86ComputeBaseMethodAddress* address = load_string->InputAt(0)->AsX86ComputeBaseMethodAddress();
4659 string_patches_.emplace_back(address,
4660 load_string->GetDexFile(),
4661 load_string->GetStringIndex().index_);
4662 __ Bind(&string_patches_.back().label);
4663}
4664
Vladimir Markoaad75c62016-10-03 08:46:48 +00004665Label* CodeGeneratorX86::NewStringBssEntryPatch(HLoadString* load_string) {
4666 DCHECK(!GetCompilerOptions().IsBootImage());
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004667 HX86ComputeBaseMethodAddress* address =
4668 load_string->InputAt(0)->AsX86ComputeBaseMethodAddress();
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004669 string_bss_entry_patches_.emplace_back(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004670 address, load_string->GetDexFile(), load_string->GetStringIndex().index_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004671 return &string_bss_entry_patches_.back().label;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004672}
4673
Vladimir Markoaad75c62016-10-03 08:46:48 +00004674// The label points to the end of the "movl" or another instruction but the literal offset
4675// for method patch needs to point to the embedded constant which occupies the last 4 bytes.
4676constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
4677
4678template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
4679inline void CodeGeneratorX86::EmitPcRelativeLinkerPatches(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004680 const ArenaDeque<X86PcRelativePatchInfo>& infos,
Vladimir Markoaad75c62016-10-03 08:46:48 +00004681 ArenaVector<LinkerPatch>* linker_patches) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004682 for (const X86PcRelativePatchInfo& info : infos) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00004683 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004684 linker_patches->push_back(Factory(
4685 literal_offset, &info.dex_file, GetMethodAddressOffset(info.method_address), info.index));
Vladimir Markoaad75c62016-10-03 08:46:48 +00004686 }
4687}
4688
Vladimir Marko58155012015-08-19 12:49:41 +00004689void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4690 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004691 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01004692 boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004693 method_bss_entry_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +00004694 boot_image_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01004695 type_bss_entry_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004696 string_patches_.size() +
4697 string_bss_entry_patches_.size();
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004698 linker_patches->reserve(size);
Vladimir Marko764d4542017-05-16 10:31:41 +01004699 if (GetCompilerOptions().IsBootImage()) {
Vladimir Marko65979462017-05-19 17:25:12 +01004700 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeMethodPatch>(boot_image_method_patches_,
4701 linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00004702 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(boot_image_type_patches_,
4703 linker_patches);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004704 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(string_patches_, linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004705 } else {
Vladimir Marko65979462017-05-19 17:25:12 +01004706 DCHECK(boot_image_method_patches_.empty());
Vladimir Marko94ec2db2017-09-06 17:21:03 +01004707 EmitPcRelativeLinkerPatches<LinkerPatch::TypeClassTablePatch>(boot_image_type_patches_,
4708 linker_patches);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004709 EmitPcRelativeLinkerPatches<LinkerPatch::StringInternTablePatch>(string_patches_,
4710 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004711 }
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004712 EmitPcRelativeLinkerPatches<LinkerPatch::MethodBssEntryPatch>(method_bss_entry_patches_,
4713 linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00004714 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
4715 linker_patches);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01004716 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(string_bss_entry_patches_,
4717 linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00004718 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00004719}
4720
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004721void CodeGeneratorX86::MarkGCCard(Register temp,
4722 Register card,
4723 Register object,
4724 Register value,
4725 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004726 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004727 if (value_can_be_null) {
4728 __ testl(value, value);
4729 __ j(kEqual, &is_null);
4730 }
Andreas Gampe542451c2016-07-26 09:02:02 -07004731 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86PointerSize>().Int32Value()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004732 __ movl(temp, object);
4733 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004734 __ movb(Address(temp, card, TIMES_1, 0),
4735 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004736 if (value_can_be_null) {
4737 __ Bind(&is_null);
4738 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004739}
4740
Calin Juravle52c48962014-12-16 17:02:57 +00004741void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4742 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00004743
4744 bool object_field_get_with_read_barrier =
4745 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004746 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004747 new (GetGraph()->GetArena()) LocationSummary(instruction,
4748 kEmitCompilerReadBarrier ?
4749 LocationSummary::kCallOnSlowPath :
4750 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004751 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004752 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004753 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004754 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004755
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004756 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4757 locations->SetOut(Location::RequiresFpuRegister());
4758 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004759 // The output overlaps in case of long: we don't want the low move
4760 // to overwrite the object's location. Likewise, in the case of
4761 // an object field get with read barriers enabled, we do not want
4762 // the move to overwrite the object's location, as we need it to emit
4763 // the read barrier.
4764 locations->SetOut(
4765 Location::RequiresRegister(),
4766 (object_field_get_with_read_barrier || instruction->GetType() == Primitive::kPrimLong) ?
4767 Location::kOutputOverlap :
4768 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004769 }
Calin Juravle52c48962014-12-16 17:02:57 +00004770
4771 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
4772 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00004773 // So we use an XMM register as a temp to achieve atomicity (first
4774 // load the temp into the XMM and then copy the XMM into the
4775 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00004776 locations->AddTemp(Location::RequiresFpuRegister());
4777 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004778}
4779
Calin Juravle52c48962014-12-16 17:02:57 +00004780void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
4781 const FieldInfo& field_info) {
4782 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004783
Calin Juravle52c48962014-12-16 17:02:57 +00004784 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004785 Location base_loc = locations->InAt(0);
4786 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004787 Location out = locations->Out();
4788 bool is_volatile = field_info.IsVolatile();
4789 Primitive::Type field_type = field_info.GetFieldType();
4790 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4791
4792 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004793 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00004794 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004795 break;
4796 }
4797
4798 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00004799 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004800 break;
4801 }
4802
4803 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00004804 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004805 break;
4806 }
4807
4808 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00004809 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004810 break;
4811 }
4812
4813 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004814 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004815 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00004816
4817 case Primitive::kPrimNot: {
4818 // /* HeapReference<Object> */ out = *(base + offset)
4819 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004820 // Note that a potential implicit null check is handled in this
4821 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4822 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00004823 instruction, out, base, offset, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00004824 if (is_volatile) {
4825 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4826 }
4827 } else {
4828 __ movl(out.AsRegister<Register>(), Address(base, offset));
4829 codegen_->MaybeRecordImplicitNullCheck(instruction);
4830 if (is_volatile) {
4831 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4832 }
4833 // If read barriers are enabled, emit read barriers other than
4834 // Baker's using a slow path (and also unpoison the loaded
4835 // reference, if heap poisoning is enabled).
4836 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4837 }
4838 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004839 }
4840
4841 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00004842 if (is_volatile) {
4843 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4844 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004845 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004846 __ movd(out.AsRegisterPairLow<Register>(), temp);
4847 __ psrlq(temp, Immediate(32));
4848 __ movd(out.AsRegisterPairHigh<Register>(), temp);
4849 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004850 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00004851 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004852 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004853 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
4854 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004855 break;
4856 }
4857
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004858 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004859 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004860 break;
4861 }
4862
4863 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004864 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004865 break;
4866 }
4867
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004868 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004869 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004870 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004871 }
Calin Juravle52c48962014-12-16 17:02:57 +00004872
Roland Levillain7c1559a2015-12-15 10:55:36 +00004873 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimLong) {
4874 // Potential implicit null checks, in the case of reference or
4875 // long fields, are handled in the previous switch statement.
4876 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004877 codegen_->MaybeRecordImplicitNullCheck(instruction);
4878 }
4879
Calin Juravle52c48962014-12-16 17:02:57 +00004880 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004881 if (field_type == Primitive::kPrimNot) {
4882 // Memory barriers, in the case of references, are also handled
4883 // in the previous switch statement.
4884 } else {
4885 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4886 }
Roland Levillain4d027112015-07-01 15:41:14 +01004887 }
Calin Juravle52c48962014-12-16 17:02:57 +00004888}
4889
4890void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4891 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4892
4893 LocationSummary* locations =
4894 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4895 locations->SetInAt(0, Location::RequiresRegister());
4896 bool is_volatile = field_info.IsVolatile();
4897 Primitive::Type field_type = field_info.GetFieldType();
4898 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
4899 || (field_type == Primitive::kPrimByte);
4900
4901 // The register allocator does not support multiple
4902 // inputs that die at entry with one in a specific register.
4903 if (is_byte_type) {
4904 // Ensure the value is in a byte register.
4905 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004906 } else if (Primitive::IsFloatingPointType(field_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05004907 if (is_volatile && field_type == Primitive::kPrimDouble) {
4908 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4909 locations->SetInAt(1, Location::RequiresFpuRegister());
4910 } else {
4911 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4912 }
4913 } else if (is_volatile && field_type == Primitive::kPrimLong) {
4914 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00004915 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004916
Calin Juravle52c48962014-12-16 17:02:57 +00004917 // 64bits value can be atomically written to an address with movsd and an XMM register.
4918 // We need two XMM registers because there's no easier way to (bit) copy a register pair
4919 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
4920 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
4921 // isolated cases when we need this it isn't worth adding the extra complexity.
4922 locations->AddTemp(Location::RequiresFpuRegister());
4923 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004924 } else {
4925 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4926
4927 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4928 // Temporary registers for the write barrier.
4929 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
4930 // Ensure the card is in a byte register.
4931 locations->AddTemp(Location::RegisterLocation(ECX));
4932 }
Calin Juravle52c48962014-12-16 17:02:57 +00004933 }
4934}
4935
4936void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004937 const FieldInfo& field_info,
4938 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004939 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4940
4941 LocationSummary* locations = instruction->GetLocations();
4942 Register base = locations->InAt(0).AsRegister<Register>();
4943 Location value = locations->InAt(1);
4944 bool is_volatile = field_info.IsVolatile();
4945 Primitive::Type field_type = field_info.GetFieldType();
4946 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004947 bool needs_write_barrier =
4948 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004949
4950 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004951 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004952 }
4953
Mark Mendell81489372015-11-04 11:30:41 -05004954 bool maybe_record_implicit_null_check_done = false;
4955
Calin Juravle52c48962014-12-16 17:02:57 +00004956 switch (field_type) {
4957 case Primitive::kPrimBoolean:
4958 case Primitive::kPrimByte: {
4959 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
4960 break;
4961 }
4962
4963 case Primitive::kPrimShort:
4964 case Primitive::kPrimChar: {
Mark Mendell81489372015-11-04 11:30:41 -05004965 if (value.IsConstant()) {
Nicolas Geoffray78612082017-07-24 14:18:53 +01004966 __ movw(Address(base, offset),
4967 Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Mark Mendell81489372015-11-04 11:30:41 -05004968 } else {
4969 __ movw(Address(base, offset), value.AsRegister<Register>());
4970 }
Calin Juravle52c48962014-12-16 17:02:57 +00004971 break;
4972 }
4973
4974 case Primitive::kPrimInt:
4975 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01004976 if (kPoisonHeapReferences && needs_write_barrier) {
4977 // Note that in the case where `value` is a null reference,
4978 // we do not enter this block, as the reference does not
4979 // need poisoning.
4980 DCHECK_EQ(field_type, Primitive::kPrimNot);
4981 Register temp = locations->GetTemp(0).AsRegister<Register>();
4982 __ movl(temp, value.AsRegister<Register>());
4983 __ PoisonHeapReference(temp);
4984 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05004985 } else if (value.IsConstant()) {
4986 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4987 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01004988 } else {
Nicolas Geoffray03971632016-03-17 10:44:24 +00004989 DCHECK(value.IsRegister()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01004990 __ movl(Address(base, offset), value.AsRegister<Register>());
4991 }
Calin Juravle52c48962014-12-16 17:02:57 +00004992 break;
4993 }
4994
4995 case Primitive::kPrimLong: {
4996 if (is_volatile) {
4997 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4998 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
4999 __ movd(temp1, value.AsRegisterPairLow<Register>());
5000 __ movd(temp2, value.AsRegisterPairHigh<Register>());
5001 __ punpckldq(temp1, temp2);
5002 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00005003 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05005004 } else if (value.IsConstant()) {
5005 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
5006 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
5007 codegen_->MaybeRecordImplicitNullCheck(instruction);
5008 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00005009 } else {
5010 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005011 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005012 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
5013 }
Mark Mendell81489372015-11-04 11:30:41 -05005014 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00005015 break;
5016 }
5017
5018 case Primitive::kPrimFloat: {
Mark Mendell81489372015-11-04 11:30:41 -05005019 if (value.IsConstant()) {
5020 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5021 __ movl(Address(base, offset), Immediate(v));
5022 } else {
5023 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
5024 }
Calin Juravle52c48962014-12-16 17:02:57 +00005025 break;
5026 }
5027
5028 case Primitive::kPrimDouble: {
Mark Mendell81489372015-11-04 11:30:41 -05005029 if (value.IsConstant()) {
5030 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
5031 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
5032 codegen_->MaybeRecordImplicitNullCheck(instruction);
5033 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
5034 maybe_record_implicit_null_check_done = true;
5035 } else {
5036 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
5037 }
Calin Juravle52c48962014-12-16 17:02:57 +00005038 break;
5039 }
5040
5041 case Primitive::kPrimVoid:
5042 LOG(FATAL) << "Unreachable type " << field_type;
5043 UNREACHABLE();
5044 }
5045
Mark Mendell81489372015-11-04 11:30:41 -05005046 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005047 codegen_->MaybeRecordImplicitNullCheck(instruction);
5048 }
5049
Roland Levillain4d027112015-07-01 15:41:14 +01005050 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005051 Register temp = locations->GetTemp(0).AsRegister<Register>();
5052 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005053 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00005054 }
5055
Calin Juravle52c48962014-12-16 17:02:57 +00005056 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005057 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00005058 }
5059}
5060
5061void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5062 HandleFieldGet(instruction, instruction->GetFieldInfo());
5063}
5064
5065void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5066 HandleFieldGet(instruction, instruction->GetFieldInfo());
5067}
5068
5069void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5070 HandleFieldSet(instruction, instruction->GetFieldInfo());
5071}
5072
5073void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005074 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005075}
5076
5077void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5078 HandleFieldSet(instruction, instruction->GetFieldInfo());
5079}
5080
5081void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005082 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005083}
5084
5085void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5086 HandleFieldGet(instruction, instruction->GetFieldInfo());
5087}
5088
5089void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5090 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005091}
5092
Calin Juravlee460d1d2015-09-29 04:52:17 +01005093void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
5094 HUnresolvedInstanceFieldGet* instruction) {
5095 FieldAccessCallingConventionX86 calling_convention;
5096 codegen_->CreateUnresolvedFieldLocationSummary(
5097 instruction, instruction->GetFieldType(), calling_convention);
5098}
5099
5100void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
5101 HUnresolvedInstanceFieldGet* instruction) {
5102 FieldAccessCallingConventionX86 calling_convention;
5103 codegen_->GenerateUnresolvedFieldAccess(instruction,
5104 instruction->GetFieldType(),
5105 instruction->GetFieldIndex(),
5106 instruction->GetDexPc(),
5107 calling_convention);
5108}
5109
5110void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
5111 HUnresolvedInstanceFieldSet* instruction) {
5112 FieldAccessCallingConventionX86 calling_convention;
5113 codegen_->CreateUnresolvedFieldLocationSummary(
5114 instruction, instruction->GetFieldType(), calling_convention);
5115}
5116
5117void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
5118 HUnresolvedInstanceFieldSet* instruction) {
5119 FieldAccessCallingConventionX86 calling_convention;
5120 codegen_->GenerateUnresolvedFieldAccess(instruction,
5121 instruction->GetFieldType(),
5122 instruction->GetFieldIndex(),
5123 instruction->GetDexPc(),
5124 calling_convention);
5125}
5126
5127void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
5128 HUnresolvedStaticFieldGet* instruction) {
5129 FieldAccessCallingConventionX86 calling_convention;
5130 codegen_->CreateUnresolvedFieldLocationSummary(
5131 instruction, instruction->GetFieldType(), calling_convention);
5132}
5133
5134void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
5135 HUnresolvedStaticFieldGet* instruction) {
5136 FieldAccessCallingConventionX86 calling_convention;
5137 codegen_->GenerateUnresolvedFieldAccess(instruction,
5138 instruction->GetFieldType(),
5139 instruction->GetFieldIndex(),
5140 instruction->GetDexPc(),
5141 calling_convention);
5142}
5143
5144void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
5145 HUnresolvedStaticFieldSet* instruction) {
5146 FieldAccessCallingConventionX86 calling_convention;
5147 codegen_->CreateUnresolvedFieldLocationSummary(
5148 instruction, instruction->GetFieldType(), calling_convention);
5149}
5150
5151void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
5152 HUnresolvedStaticFieldSet* instruction) {
5153 FieldAccessCallingConventionX86 calling_convention;
5154 codegen_->GenerateUnresolvedFieldAccess(instruction,
5155 instruction->GetFieldType(),
5156 instruction->GetFieldIndex(),
5157 instruction->GetDexPc(),
5158 calling_convention);
5159}
5160
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005161void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005162 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5163 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
5164 ? Location::RequiresRegister()
5165 : Location::Any();
5166 locations->SetInAt(0, loc);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005167}
5168
Calin Juravle2ae48182016-03-16 14:05:09 +00005169void CodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
5170 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005171 return;
5172 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005173 LocationSummary* locations = instruction->GetLocations();
5174 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005175
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005176 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00005177 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005178}
5179
Calin Juravle2ae48182016-03-16 14:05:09 +00005180void CodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07005181 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005182 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005183
5184 LocationSummary* locations = instruction->GetLocations();
5185 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005186
5187 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04005188 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005189 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005190 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005191 } else {
5192 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00005193 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005194 __ jmp(slow_path->GetEntryLabel());
5195 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005196 }
5197 __ j(kEqual, slow_path->GetEntryLabel());
5198}
5199
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005200void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005201 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005202}
5203
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005204void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005205 bool object_array_get_with_read_barrier =
5206 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005207 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00005208 new (GetGraph()->GetArena()) LocationSummary(instruction,
5209 object_array_get_with_read_barrier ?
5210 LocationSummary::kCallOnSlowPath :
5211 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005212 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005213 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005214 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005215 locations->SetInAt(0, Location::RequiresRegister());
5216 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005217 if (Primitive::IsFloatingPointType(instruction->GetType())) {
5218 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5219 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005220 // The output overlaps in case of long: we don't want the low move
5221 // to overwrite the array's location. Likewise, in the case of an
5222 // object array get with read barriers enabled, we do not want the
5223 // move to overwrite the array's location, as we need it to emit
5224 // the read barrier.
5225 locations->SetOut(
5226 Location::RequiresRegister(),
5227 (instruction->GetType() == Primitive::kPrimLong || object_array_get_with_read_barrier) ?
5228 Location::kOutputOverlap :
5229 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005230 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005231}
5232
5233void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
5234 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005235 Location obj_loc = locations->InAt(0);
5236 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005237 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005238 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005239 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005240
Calin Juravle77520bc2015-01-12 18:45:46 +00005241 Primitive::Type type = instruction->GetType();
5242 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005243 case Primitive::kPrimBoolean: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005244 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005245 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005246 break;
5247 }
5248
5249 case Primitive::kPrimByte: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005250 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005251 __ movsxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005252 break;
5253 }
5254
5255 case Primitive::kPrimShort: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005256 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005257 __ movsxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005258 break;
5259 }
5260
5261 case Primitive::kPrimChar: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005262 Register out = out_loc.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07005263 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5264 // Branch cases into compressed and uncompressed for each index's type.
5265 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
5266 NearLabel done, not_compressed;
Vladimir Marko3c89d422017-02-17 11:30:23 +00005267 __ testb(Address(obj, count_offset), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005268 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005269 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
5270 "Expecting 0=compressed, 1=uncompressed");
5271 __ j(kNotZero, &not_compressed);
jessicahandojo4877b792016-09-08 19:49:13 -07005272 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
5273 __ jmp(&done);
5274 __ Bind(&not_compressed);
5275 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5276 __ Bind(&done);
5277 } else {
5278 // Common case for charAt of array of char or when string compression's
5279 // feature is turned off.
5280 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5281 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005282 break;
5283 }
5284
Roland Levillain7c1559a2015-12-15 10:55:36 +00005285 case Primitive::kPrimInt: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005286 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005287 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005288 break;
5289 }
5290
Roland Levillain7c1559a2015-12-15 10:55:36 +00005291 case Primitive::kPrimNot: {
5292 static_assert(
5293 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5294 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00005295 // /* HeapReference<Object> */ out =
5296 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5297 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005298 // Note that a potential implicit null check is handled in this
5299 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
5300 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00005301 instruction, out_loc, obj, data_offset, index, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005302 } else {
5303 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005304 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
5305 codegen_->MaybeRecordImplicitNullCheck(instruction);
5306 // If read barriers are enabled, emit read barriers other than
5307 // Baker's using a slow path (and also unpoison the loaded
5308 // reference, if heap poisoning is enabled).
Roland Levillain7c1559a2015-12-15 10:55:36 +00005309 if (index.IsConstant()) {
5310 uint32_t offset =
5311 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005312 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5313 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005314 codegen_->MaybeGenerateReadBarrierSlow(
5315 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5316 }
5317 }
5318 break;
5319 }
5320
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005321 case Primitive::kPrimLong: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005322 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005323 __ movl(out_loc.AsRegisterPairLow<Register>(),
5324 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
5325 codegen_->MaybeRecordImplicitNullCheck(instruction);
5326 __ movl(out_loc.AsRegisterPairHigh<Register>(),
5327 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005328 break;
5329 }
5330
Mark Mendell7c8d0092015-01-26 11:21:33 -05005331 case Primitive::kPrimFloat: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005332 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005333 __ movss(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005334 break;
5335 }
5336
5337 case Primitive::kPrimDouble: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005338 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005339 __ movsd(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005340 break;
5341 }
5342
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005343 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00005344 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005345 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005346 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005347
Roland Levillain7c1559a2015-12-15 10:55:36 +00005348 if (type == Primitive::kPrimNot || type == Primitive::kPrimLong) {
5349 // Potential implicit null checks, in the case of reference or
5350 // long arrays, are handled in the previous switch statement.
5351 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005352 codegen_->MaybeRecordImplicitNullCheck(instruction);
5353 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005354}
5355
5356void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005357 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005358
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005359 bool needs_write_barrier =
5360 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005361 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005362
Nicolas Geoffray39468442014-09-02 15:17:15 +01005363 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
5364 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01005365 may_need_runtime_call_for_type_check ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00005366 LocationSummary::kCallOnSlowPath :
5367 LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005368
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005369 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
5370 || (value_type == Primitive::kPrimByte);
5371 // We need the inputs to be different than the output in case of long operation.
5372 // In case of a byte operation, the register allocator does not support multiple
5373 // inputs that die at entry with one in a specific register.
5374 locations->SetInAt(0, Location::RequiresRegister());
5375 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5376 if (is_byte_type) {
5377 // Ensure the value is in a byte register.
5378 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
5379 } else if (Primitive::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05005380 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005381 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005382 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
5383 }
5384 if (needs_write_barrier) {
5385 // Temporary registers for the write barrier.
5386 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5387 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005388 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005389 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005390}
5391
5392void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
5393 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005394 Location array_loc = locations->InAt(0);
5395 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005396 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005397 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005398 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005399 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5400 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5401 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005402 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005403 bool needs_write_barrier =
5404 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005405
5406 switch (value_type) {
5407 case Primitive::kPrimBoolean:
5408 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005409 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005410 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_1, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005411 if (value.IsRegister()) {
5412 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005413 } else {
Nicolas Geoffray78612082017-07-24 14:18:53 +01005414 __ movb(address, Immediate(CodeGenerator::GetInt8ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005415 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005416 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005417 break;
5418 }
5419
5420 case Primitive::kPrimShort:
5421 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005422 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005423 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_2, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005424 if (value.IsRegister()) {
5425 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005426 } else {
Nicolas Geoffray78612082017-07-24 14:18:53 +01005427 __ movw(address, Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005428 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005429 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005430 break;
5431 }
5432
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005433 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005434 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005435 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005436
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005437 if (!value.IsRegister()) {
5438 // Just setting null.
5439 DCHECK(instruction->InputAt(2)->IsNullConstant());
5440 DCHECK(value.IsConstant()) << value;
5441 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00005442 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005443 DCHECK(!needs_write_barrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005444 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005445 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005446 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005447
5448 DCHECK(needs_write_barrier);
5449 Register register_value = value.AsRegister<Register>();
Roland Levillain16d9f942016-08-25 17:27:56 +01005450 // We cannot use a NearLabel for `done`, as its range may be too
5451 // short when Baker read barriers are enabled.
5452 Label done;
5453 NearLabel not_null, do_put;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005454 SlowPathCode* slow_path = nullptr;
Roland Levillain16d9f942016-08-25 17:27:56 +01005455 Location temp_loc = locations->GetTemp(0);
5456 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005457 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005458 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86(instruction);
5459 codegen_->AddSlowPath(slow_path);
5460 if (instruction->GetValueCanBeNull()) {
5461 __ testl(register_value, register_value);
5462 __ j(kNotEqual, &not_null);
5463 __ movl(address, Immediate(0));
5464 codegen_->MaybeRecordImplicitNullCheck(instruction);
5465 __ jmp(&done);
5466 __ Bind(&not_null);
5467 }
5468
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005469 // Note that when Baker read barriers are enabled, the type
5470 // checks are performed without read barriers. This is fine,
5471 // even in the case where a class object is in the from-space
5472 // after the flip, as a comparison involving such a type would
5473 // not produce a false positive; it may of course produce a
5474 // false negative, in which case we would take the ArraySet
5475 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01005476
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005477 // /* HeapReference<Class> */ temp = array->klass_
5478 __ movl(temp, Address(array, class_offset));
5479 codegen_->MaybeRecordImplicitNullCheck(instruction);
5480 __ MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01005481
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005482 // /* HeapReference<Class> */ temp = temp->component_type_
5483 __ movl(temp, Address(temp, component_offset));
5484 // If heap poisoning is enabled, no need to unpoison `temp`
5485 // nor the object reference in `register_value->klass`, as
5486 // we are comparing two poisoned references.
5487 __ cmpl(temp, Address(register_value, class_offset));
Roland Levillain16d9f942016-08-25 17:27:56 +01005488
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005489 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5490 __ j(kEqual, &do_put);
5491 // If heap poisoning is enabled, the `temp` reference has
5492 // not been unpoisoned yet; unpoison it now.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005493 __ MaybeUnpoisonHeapReference(temp);
5494
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005495 // If heap poisoning is enabled, no need to unpoison the
5496 // heap reference loaded below, as it is only used for a
5497 // comparison with null.
5498 __ cmpl(Address(temp, super_offset), Immediate(0));
5499 __ j(kNotEqual, slow_path->GetEntryLabel());
5500 __ Bind(&do_put);
5501 } else {
5502 __ j(kNotEqual, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005503 }
5504 }
5505
5506 if (kPoisonHeapReferences) {
5507 __ movl(temp, register_value);
5508 __ PoisonHeapReference(temp);
5509 __ movl(address, temp);
5510 } else {
5511 __ movl(address, register_value);
5512 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005513 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005514 codegen_->MaybeRecordImplicitNullCheck(instruction);
5515 }
5516
5517 Register card = locations->GetTemp(1).AsRegister<Register>();
5518 codegen_->MarkGCCard(
5519 temp, card, array, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
5520 __ Bind(&done);
5521
5522 if (slow_path != nullptr) {
5523 __ Bind(slow_path->GetExitLabel());
5524 }
5525
5526 break;
5527 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005528
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005529 case Primitive::kPrimInt: {
5530 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005531 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005532 if (value.IsRegister()) {
5533 __ movl(address, value.AsRegister<Register>());
5534 } else {
5535 DCHECK(value.IsConstant()) << value;
5536 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5537 __ movl(address, Immediate(v));
5538 }
5539 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005540 break;
5541 }
5542
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005543 case Primitive::kPrimLong: {
5544 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005545 if (value.IsRegisterPair()) {
5546 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
5547 value.AsRegisterPairLow<Register>());
5548 codegen_->MaybeRecordImplicitNullCheck(instruction);
5549 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
5550 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005551 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005552 DCHECK(value.IsConstant());
5553 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
5554 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
5555 Immediate(Low32Bits(val)));
5556 codegen_->MaybeRecordImplicitNullCheck(instruction);
5557 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
5558 Immediate(High32Bits(val)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005559 }
5560 break;
5561 }
5562
Mark Mendell7c8d0092015-01-26 11:21:33 -05005563 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005564 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005565 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005566 if (value.IsFpuRegister()) {
5567 __ movss(address, value.AsFpuRegister<XmmRegister>());
5568 } else {
5569 DCHECK(value.IsConstant());
5570 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
5571 __ movl(address, Immediate(v));
5572 }
5573 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005574 break;
5575 }
5576
5577 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005578 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005579 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005580 if (value.IsFpuRegister()) {
5581 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5582 } else {
5583 DCHECK(value.IsConstant());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005584 Address address_hi =
5585 CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset + kX86WordSize);
Mark Mendell81489372015-11-04 11:30:41 -05005586 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5587 __ movl(address, Immediate(Low32Bits(v)));
5588 codegen_->MaybeRecordImplicitNullCheck(instruction);
5589 __ movl(address_hi, Immediate(High32Bits(v)));
5590 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005591 break;
5592 }
5593
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005594 case Primitive::kPrimVoid:
5595 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005596 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005597 }
5598}
5599
5600void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
5601 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005602 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04005603 if (!instruction->IsEmittedAtUseSite()) {
5604 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5605 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005606}
5607
5608void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04005609 if (instruction->IsEmittedAtUseSite()) {
5610 return;
5611 }
5612
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005613 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005614 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005615 Register obj = locations->InAt(0).AsRegister<Register>();
5616 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005617 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005618 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo4877b792016-09-08 19:49:13 -07005619 // Mask out most significant bit in case the array is String's array of char.
5620 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005621 __ shrl(out, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005622 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005623}
5624
5625void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005626 RegisterSet caller_saves = RegisterSet::Empty();
5627 InvokeRuntimeCallingConvention calling_convention;
5628 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5629 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5630 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005631 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04005632 HInstruction* length = instruction->InputAt(1);
5633 if (!length->IsEmittedAtUseSite()) {
5634 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5635 }
jessicahandojo4877b792016-09-08 19:49:13 -07005636 // Need register to see array's length.
5637 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5638 locations->AddTemp(Location::RequiresRegister());
5639 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005640}
5641
5642void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
jessicahandojo4877b792016-09-08 19:49:13 -07005643 const bool is_string_compressed_char_at =
5644 mirror::kUseStringCompression && instruction->IsStringCharAt();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005645 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005646 Location index_loc = locations->InAt(0);
5647 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07005648 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005649 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005650
Mark Mendell99dbd682015-04-22 16:18:52 -04005651 if (length_loc.IsConstant()) {
5652 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5653 if (index_loc.IsConstant()) {
5654 // BCE will remove the bounds check if we are guarenteed to pass.
5655 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5656 if (index < 0 || index >= length) {
5657 codegen_->AddSlowPath(slow_path);
5658 __ jmp(slow_path->GetEntryLabel());
5659 } else {
5660 // Some optimization after BCE may have generated this, and we should not
5661 // generate a bounds check if it is a valid range.
5662 }
5663 return;
5664 }
5665
5666 // We have to reverse the jump condition because the length is the constant.
5667 Register index_reg = index_loc.AsRegister<Register>();
5668 __ cmpl(index_reg, Immediate(length));
5669 codegen_->AddSlowPath(slow_path);
5670 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005671 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04005672 HInstruction* array_length = instruction->InputAt(1);
5673 if (array_length->IsEmittedAtUseSite()) {
5674 // Address the length field in the array.
5675 DCHECK(array_length->IsArrayLength());
5676 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
5677 Location array_loc = array_length->GetLocations()->InAt(0);
5678 Address array_len(array_loc.AsRegister<Register>(), len_offset);
jessicahandojo4877b792016-09-08 19:49:13 -07005679 if (is_string_compressed_char_at) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005680 // TODO: if index_loc.IsConstant(), compare twice the index (to compensate for
5681 // the string compression flag) with the in-memory length and avoid the temporary.
jessicahandojo4877b792016-09-08 19:49:13 -07005682 Register length_reg = locations->GetTemp(0).AsRegister<Register>();
5683 __ movl(length_reg, array_len);
5684 codegen_->MaybeRecordImplicitNullCheck(array_length);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005685 __ shrl(length_reg, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005686 codegen_->GenerateIntCompare(length_reg, index_loc);
Mark Mendellee8d9712016-07-12 11:13:15 -04005687 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07005688 // Checking bounds for general case:
5689 // Array of char or string's array with feature compression off.
5690 if (index_loc.IsConstant()) {
5691 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5692 __ cmpl(array_len, Immediate(value));
5693 } else {
5694 __ cmpl(array_len, index_loc.AsRegister<Register>());
5695 }
5696 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendellee8d9712016-07-12 11:13:15 -04005697 }
Mark Mendell99dbd682015-04-22 16:18:52 -04005698 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005699 codegen_->GenerateIntCompare(length_loc, index_loc);
Mark Mendell99dbd682015-04-22 16:18:52 -04005700 }
5701 codegen_->AddSlowPath(slow_path);
5702 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005703 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005704}
5705
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005706void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005707 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005708}
5709
5710void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005711 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5712}
5713
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005714void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005715 LocationSummary* locations =
5716 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Aart Bikb13c65b2017-03-21 20:14:07 -07005717 // In suspend check slow path, usually there are no caller-save registers at all.
5718 // If SIMD instructions are present, however, we force spilling all live SIMD
5719 // registers in full width (since the runtime only saves/restores lower part).
Aart Bik5576f372017-03-23 16:17:37 -07005720 locations->SetCustomSlowPathCallerSaves(
5721 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005722}
5723
5724void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005725 HBasicBlock* block = instruction->GetBlock();
5726 if (block->GetLoopInformation() != nullptr) {
5727 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5728 // The back edge will generate the suspend check.
5729 return;
5730 }
5731 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5732 // The goto will generate the suspend check.
5733 return;
5734 }
5735 GenerateSuspendCheck(instruction, nullptr);
5736}
5737
5738void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
5739 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005740 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005741 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
5742 if (slow_path == nullptr) {
5743 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
5744 instruction->SetSlowPath(slow_path);
5745 codegen_->AddSlowPath(slow_path);
5746 if (successor != nullptr) {
5747 DCHECK(successor->IsLoopHeader());
5748 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5749 }
5750 } else {
5751 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5752 }
5753
Andreas Gampe542451c2016-07-26 09:02:02 -07005754 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86PointerSize>().Int32Value()),
Roland Levillain7c1559a2015-12-15 10:55:36 +00005755 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005756 if (successor == nullptr) {
5757 __ j(kNotEqual, slow_path->GetEntryLabel());
5758 __ Bind(slow_path->GetReturnLabel());
5759 } else {
5760 __ j(kEqual, codegen_->GetLabelOf(successor));
5761 __ jmp(slow_path->GetEntryLabel());
5762 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005763}
5764
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005765X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
5766 return codegen_->GetAssembler();
5767}
5768
Mark Mendell7c8d0092015-01-26 11:21:33 -05005769void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005770 ScratchRegisterScope ensure_scratch(
5771 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5772 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5773 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5774 __ movl(temp_reg, Address(ESP, src + stack_offset));
5775 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005776}
5777
5778void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005779 ScratchRegisterScope ensure_scratch(
5780 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5781 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5782 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5783 __ movl(temp_reg, Address(ESP, src + stack_offset));
5784 __ movl(Address(ESP, dst + stack_offset), temp_reg);
5785 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
5786 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005787}
5788
5789void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005790 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005791 Location source = move->GetSource();
5792 Location destination = move->GetDestination();
5793
5794 if (source.IsRegister()) {
5795 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005796 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005797 } else if (destination.IsFpuRegister()) {
5798 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005799 } else {
5800 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005801 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005802 }
David Brazdil74eb1b22015-12-14 11:44:01 +00005803 } else if (source.IsRegisterPair()) {
5804 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
5805 // Create stack space for 2 elements.
5806 __ subl(ESP, Immediate(2 * elem_size));
5807 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
5808 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
5809 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
5810 // And remove the temporary stack space we allocated.
5811 __ addl(ESP, Immediate(2 * elem_size));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005812 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005813 if (destination.IsRegister()) {
5814 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
5815 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005816 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005817 } else if (destination.IsRegisterPair()) {
5818 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
5819 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
5820 __ psrlq(src_reg, Immediate(32));
5821 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005822 } else if (destination.IsStackSlot()) {
5823 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07005824 } else if (destination.IsDoubleStackSlot()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005825 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07005826 } else {
5827 DCHECK(destination.IsSIMDStackSlot());
5828 __ movups(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005829 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005830 } else if (source.IsStackSlot()) {
5831 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005832 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005833 } else if (destination.IsFpuRegister()) {
5834 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005835 } else {
5836 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005837 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
5838 }
5839 } else if (source.IsDoubleStackSlot()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005840 if (destination.IsRegisterPair()) {
5841 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
5842 __ movl(destination.AsRegisterPairHigh<Register>(),
5843 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
5844 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005845 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
5846 } else {
5847 DCHECK(destination.IsDoubleStackSlot()) << destination;
5848 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005849 }
Aart Bik5576f372017-03-23 16:17:37 -07005850 } else if (source.IsSIMDStackSlot()) {
5851 DCHECK(destination.IsFpuRegister());
5852 __ movups(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005853 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005854 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005855 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005856 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005857 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005858 if (value == 0) {
5859 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
5860 } else {
5861 __ movl(destination.AsRegister<Register>(), Immediate(value));
5862 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005863 } else {
5864 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05005865 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005866 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005867 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005868 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005869 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005870 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005871 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005872 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5873 if (value == 0) {
5874 // Easy handling of 0.0.
5875 __ xorps(dest, dest);
5876 } else {
5877 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005878 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5879 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
5880 __ movl(temp, Immediate(value));
5881 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005882 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005883 } else {
5884 DCHECK(destination.IsStackSlot()) << destination;
5885 __ movl(Address(ESP, destination.GetStackIndex()), imm);
5886 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005887 } else if (constant->IsLongConstant()) {
5888 int64_t value = constant->AsLongConstant()->GetValue();
5889 int32_t low_value = Low32Bits(value);
5890 int32_t high_value = High32Bits(value);
5891 Immediate low(low_value);
5892 Immediate high(high_value);
5893 if (destination.IsDoubleStackSlot()) {
5894 __ movl(Address(ESP, destination.GetStackIndex()), low);
5895 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5896 } else {
5897 __ movl(destination.AsRegisterPairLow<Register>(), low);
5898 __ movl(destination.AsRegisterPairHigh<Register>(), high);
5899 }
5900 } else {
5901 DCHECK(constant->IsDoubleConstant());
5902 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005903 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005904 int32_t low_value = Low32Bits(value);
5905 int32_t high_value = High32Bits(value);
5906 Immediate low(low_value);
5907 Immediate high(high_value);
5908 if (destination.IsFpuRegister()) {
5909 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5910 if (value == 0) {
5911 // Easy handling of 0.0.
5912 __ xorpd(dest, dest);
5913 } else {
5914 __ pushl(high);
5915 __ pushl(low);
5916 __ movsd(dest, Address(ESP, 0));
5917 __ addl(ESP, Immediate(8));
5918 }
5919 } else {
5920 DCHECK(destination.IsDoubleStackSlot()) << destination;
5921 __ movl(Address(ESP, destination.GetStackIndex()), low);
5922 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5923 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005924 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005925 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005926 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005927 }
5928}
5929
Mark Mendella5c19ce2015-04-01 12:51:05 -04005930void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005931 Register suggested_scratch = reg == EAX ? EBX : EAX;
5932 ScratchRegisterScope ensure_scratch(
5933 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
5934
5935 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5936 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
5937 __ movl(Address(ESP, mem + stack_offset), reg);
5938 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005939}
5940
Mark Mendell7c8d0092015-01-26 11:21:33 -05005941void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005942 ScratchRegisterScope ensure_scratch(
5943 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5944
5945 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5946 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5947 __ movl(temp_reg, Address(ESP, mem + stack_offset));
5948 __ movss(Address(ESP, mem + stack_offset), reg);
5949 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005950}
5951
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005952void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005953 ScratchRegisterScope ensure_scratch1(
5954 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005955
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005956 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
5957 ScratchRegisterScope ensure_scratch2(
5958 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005959
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005960 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
5961 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
5962 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
5963 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
5964 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
5965 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005966}
5967
5968void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005969 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005970 Location source = move->GetSource();
5971 Location destination = move->GetDestination();
5972
5973 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04005974 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
5975 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
5976 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
5977 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
5978 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005979 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005980 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005981 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005982 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005983 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5984 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005985 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
5986 // Use XOR Swap algorithm to avoid a temporary.
5987 DCHECK_NE(source.reg(), destination.reg());
5988 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5989 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5990 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5991 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
5992 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
5993 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
5994 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005995 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
5996 // Take advantage of the 16 bytes in the XMM register.
5997 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
5998 Address stack(ESP, destination.GetStackIndex());
5999 // Load the double into the high doubleword.
6000 __ movhpd(reg, stack);
6001
6002 // Store the low double into the destination.
6003 __ movsd(stack, reg);
6004
6005 // Move the high double to the low double.
6006 __ psrldq(reg, Immediate(8));
6007 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
6008 // Take advantage of the 16 bytes in the XMM register.
6009 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
6010 Address stack(ESP, source.GetStackIndex());
6011 // Load the double into the high doubleword.
6012 __ movhpd(reg, stack);
6013
6014 // Store the low double into the destination.
6015 __ movsd(stack, reg);
6016
6017 // Move the high double to the low double.
6018 __ psrldq(reg, Immediate(8));
6019 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
6020 Exchange(destination.GetStackIndex(), source.GetStackIndex());
6021 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006022 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006023 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006024 }
6025}
6026
6027void ParallelMoveResolverX86::SpillScratch(int reg) {
6028 __ pushl(static_cast<Register>(reg));
6029}
6030
6031void ParallelMoveResolverX86::RestoreScratch(int reg) {
6032 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006033}
6034
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006035HLoadClass::LoadKind CodeGeneratorX86::GetSupportedLoadClassKind(
6036 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006037 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006038 case HLoadClass::LoadKind::kInvalid:
6039 LOG(FATAL) << "UNREACHABLE";
6040 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006041 case HLoadClass::LoadKind::kReferrersClass:
6042 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006043 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006044 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006045 case HLoadClass::LoadKind::kBssEntry:
Vladimir Marko764d4542017-05-16 10:31:41 +01006046 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006047 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006048 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006049 DCHECK(Runtime::Current()->UseJitCompilation());
6050 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01006051 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006052 case HLoadClass::LoadKind::kRuntimeCall:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006053 break;
6054 }
6055 return desired_class_load_kind;
6056}
6057
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006058void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00006059 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006060 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006061 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00006062 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006063 cls,
6064 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00006065 Location::RegisterLocation(EAX));
Vladimir Markoea4c1262017-02-06 19:59:33 +00006066 DCHECK_EQ(calling_convention.GetRegisterAt(0), EAX);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006067 return;
6068 }
Vladimir Marko41559982017-01-06 14:04:23 +00006069 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006070
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006071 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
6072 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006073 ? LocationSummary::kCallOnSlowPath
6074 : LocationSummary::kNoCall;
6075 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006076 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006077 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006078 }
6079
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006080 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006081 load_kind == HLoadClass::LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006082 load_kind == HLoadClass::LoadKind::kBootImageClassTable ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006083 load_kind == HLoadClass::LoadKind::kBssEntry) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006084 locations->SetInAt(0, Location::RequiresRegister());
6085 }
6086 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006087 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
6088 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6089 // Rely on the type resolution and/or initialization to save everything.
6090 RegisterSet caller_saves = RegisterSet::Empty();
6091 InvokeRuntimeCallingConvention calling_convention;
6092 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6093 locations->SetCustomSlowPathCallerSaves(caller_saves);
6094 } else {
6095 // For non-Baker read barrier we have a temp-clobbering call.
6096 }
6097 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006098}
6099
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006100Label* CodeGeneratorX86::NewJitRootClassPatch(const DexFile& dex_file,
6101 dex::TypeIndex dex_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006102 Handle<mirror::Class> handle) {
6103 jit_class_roots_.Overwrite(TypeReference(&dex_file, dex_index),
6104 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006105 // Add a patch entry and return the label.
6106 jit_class_patches_.emplace_back(dex_file, dex_index.index_);
6107 PatchInfo<Label>* info = &jit_class_patches_.back();
6108 return &info->label;
6109}
6110
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006111// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6112// move.
6113void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00006114 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006115 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00006116 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01006117 return;
6118 }
Vladimir Marko41559982017-01-06 14:04:23 +00006119 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01006120
Vladimir Marko41559982017-01-06 14:04:23 +00006121 LocationSummary* locations = cls->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006122 Location out_loc = locations->Out();
6123 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006124
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006125 bool generate_null_check = false;
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006126 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
6127 ? kWithoutReadBarrier
6128 : kCompilerReadBarrierOption;
Vladimir Marko41559982017-01-06 14:04:23 +00006129 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006130 case HLoadClass::LoadKind::kReferrersClass: {
6131 DCHECK(!cls->CanCallRuntime());
6132 DCHECK(!cls->MustGenerateClinitCheck());
6133 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6134 Register current_method = locations->InAt(0).AsRegister<Register>();
6135 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006136 cls,
6137 out_loc,
6138 Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()),
Roland Levillain00468f32016-10-27 18:02:48 +01006139 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006140 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006141 break;
6142 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006143 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006144 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006145 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006146 Register method_address = locations->InAt(0).AsRegister<Register>();
6147 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Marko1998cd02017-01-13 13:02:58 +00006148 codegen_->RecordBootTypePatch(cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006149 break;
6150 }
6151 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006152 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006153 uint32_t address = dchecked_integral_cast<uint32_t>(
6154 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
6155 DCHECK_NE(address, 0u);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006156 __ movl(out, Immediate(address));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006157 break;
6158 }
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006159 case HLoadClass::LoadKind::kBootImageClassTable: {
6160 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6161 Register method_address = locations->InAt(0).AsRegister<Register>();
6162 __ movl(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
6163 codegen_->RecordBootTypePatch(cls);
6164 // Extract the reference from the slot data, i.e. clear the hash bits.
6165 int32_t masked_hash = ClassTable::TableSlot::MaskHash(
6166 ComputeModifiedUtf8Hash(cls->GetDexFile().StringByTypeIdx(cls->GetTypeIndex())));
6167 if (masked_hash != 0) {
6168 __ subl(out, Immediate(masked_hash));
6169 }
6170 break;
6171 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006172 case HLoadClass::LoadKind::kBssEntry: {
6173 Register method_address = locations->InAt(0).AsRegister<Register>();
6174 Address address(method_address, CodeGeneratorX86::kDummy32BitOffset);
6175 Label* fixup_label = codegen_->NewTypeBssEntryPatch(cls);
6176 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
6177 generate_null_check = true;
6178 break;
6179 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006180 case HLoadClass::LoadKind::kJitTableAddress: {
6181 Address address = Address::Absolute(CodeGeneratorX86::kDummy32BitOffset);
6182 Label* fixup_label = codegen_->NewJitRootClassPatch(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006183 cls->GetDexFile(), cls->GetTypeIndex(), cls->GetClass());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006184 // /* GcRoot<mirror::Class> */ out = *address
Vladimir Markoea4c1262017-02-06 19:59:33 +00006185 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006186 break;
6187 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006188 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006189 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00006190 LOG(FATAL) << "UNREACHABLE";
6191 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006192 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006193
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006194 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6195 DCHECK(cls->CanCallRuntime());
6196 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
6197 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
6198 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006199
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006200 if (generate_null_check) {
6201 __ testl(out, out);
6202 __ j(kEqual, slow_path->GetEntryLabel());
6203 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006204
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006205 if (cls->MustGenerateClinitCheck()) {
6206 GenerateClassInitializationCheck(slow_path, out);
6207 } else {
6208 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006209 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006210 }
6211}
6212
6213void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
6214 LocationSummary* locations =
6215 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
6216 locations->SetInAt(0, Location::RequiresRegister());
6217 if (check->HasUses()) {
6218 locations->SetOut(Location::SameAsFirstInput());
6219 }
6220}
6221
6222void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006223 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07006224 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006225 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006226 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00006227 GenerateClassInitializationCheck(slow_path,
6228 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006229}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006230
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006231void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07006232 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006233 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
6234 Immediate(mirror::Class::kStatusInitialized));
6235 __ j(kLess, slow_path->GetEntryLabel());
6236 __ Bind(slow_path->GetExitLabel());
6237 // No need for memory fence, thanks to the X86 memory model.
6238}
6239
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006240HLoadString::LoadKind CodeGeneratorX86::GetSupportedLoadStringKind(
6241 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006242 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006243 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006244 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00006245 case HLoadString::LoadKind::kBssEntry:
Vladimir Marko764d4542017-05-16 10:31:41 +01006246 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006247 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006248 case HLoadString::LoadKind::kJitTableAddress:
6249 DCHECK(Runtime::Current()->UseJitCompilation());
6250 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01006251 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006252 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006253 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006254 }
6255 return desired_string_load_kind;
6256}
6257
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006258void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006259 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006260 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006261 HLoadString::LoadKind load_kind = load->GetLoadKind();
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006262 if (load_kind == HLoadString::LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006263 load_kind == HLoadString::LoadKind::kBootImageInternTable ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00006264 load_kind == HLoadString::LoadKind::kBssEntry) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006265 locations->SetInAt(0, Location::RequiresRegister());
6266 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006267 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006268 locations->SetOut(Location::RegisterLocation(EAX));
6269 } else {
6270 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006271 if (load_kind == HLoadString::LoadKind::kBssEntry) {
6272 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00006273 // Rely on the pResolveString to save everything.
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006274 RegisterSet caller_saves = RegisterSet::Empty();
6275 InvokeRuntimeCallingConvention calling_convention;
6276 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6277 locations->SetCustomSlowPathCallerSaves(caller_saves);
6278 } else {
6279 // For non-Baker read barrier we have a temp-clobbering call.
6280 }
6281 }
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006282 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006283}
6284
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006285Label* CodeGeneratorX86::NewJitRootStringPatch(const DexFile& dex_file,
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006286 dex::StringIndex dex_index,
6287 Handle<mirror::String> handle) {
6288 jit_string_roots_.Overwrite(
6289 StringReference(&dex_file, dex_index), reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006290 // Add a patch entry and return the label.
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006291 jit_string_patches_.emplace_back(dex_file, dex_index.index_);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006292 PatchInfo<Label>* info = &jit_string_patches_.back();
6293 return &info->label;
6294}
6295
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006296// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6297// move.
6298void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006299 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006300 Location out_loc = locations->Out();
6301 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006302
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006303 switch (load->GetLoadKind()) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006304 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006305 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006306 Register method_address = locations->InAt(0).AsRegister<Register>();
6307 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Markoaad75c62016-10-03 08:46:48 +00006308 codegen_->RecordBootStringPatch(load);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006309 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006310 }
6311 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006312 uint32_t address = dchecked_integral_cast<uint32_t>(
6313 reinterpret_cast<uintptr_t>(load->GetString().Get()));
6314 DCHECK_NE(address, 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006315 __ movl(out, Immediate(address));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006316 return;
6317 }
6318 case HLoadString::LoadKind::kBootImageInternTable: {
6319 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6320 Register method_address = locations->InAt(0).AsRegister<Register>();
6321 __ movl(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
6322 codegen_->RecordBootStringPatch(load);
6323 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006324 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00006325 case HLoadString::LoadKind::kBssEntry: {
6326 Register method_address = locations->InAt(0).AsRegister<Register>();
6327 Address address = Address(method_address, CodeGeneratorX86::kDummy32BitOffset);
6328 Label* fixup_label = codegen_->NewStringBssEntryPatch(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006329 // /* GcRoot<mirror::String> */ out = *address /* PC-relative */
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006330 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
Vladimir Markoaad75c62016-10-03 08:46:48 +00006331 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
6332 codegen_->AddSlowPath(slow_path);
6333 __ testl(out, out);
6334 __ j(kEqual, slow_path->GetEntryLabel());
6335 __ Bind(slow_path->GetExitLabel());
6336 return;
6337 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006338 case HLoadString::LoadKind::kJitTableAddress: {
6339 Address address = Address::Absolute(CodeGeneratorX86::kDummy32BitOffset);
6340 Label* fixup_label = codegen_->NewJitRootStringPatch(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006341 load->GetDexFile(), load->GetStringIndex(), load->GetString());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006342 // /* GcRoot<mirror::String> */ out = *address
6343 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
6344 return;
6345 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006346 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006347 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006348 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006349
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006350 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006351 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006352 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006353 __ movl(calling_convention.GetRegisterAt(0), Immediate(load->GetStringIndex().index_));
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006354 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6355 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006356}
6357
David Brazdilcb1c0552015-08-04 16:22:25 +01006358static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006359 return Address::Absolute(Thread::ExceptionOffset<kX86PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01006360}
6361
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006362void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
6363 LocationSummary* locations =
6364 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
6365 locations->SetOut(Location::RequiresRegister());
6366}
6367
6368void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01006369 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
6370}
6371
6372void LocationsBuilderX86::VisitClearException(HClearException* clear) {
6373 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
6374}
6375
6376void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6377 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006378}
6379
6380void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
6381 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006382 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006383 InvokeRuntimeCallingConvention calling_convention;
6384 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6385}
6386
6387void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006388 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006389 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006390}
6391
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006392// Temp is used for read barrier.
6393static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
6394 if (kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00006395 !kUseBakerReadBarrier &&
6396 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain7c1559a2015-12-15 10:55:36 +00006397 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006398 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
6399 return 1;
6400 }
6401 return 0;
6402}
6403
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006404// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006405// interface pointer, one for loading the current interface.
6406// The other checks have one temp for loading the object's class.
6407static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
6408 if (type_check_kind == TypeCheckKind::kInterfaceCheck && !kPoisonHeapReferences) {
6409 return 2;
6410 }
6411 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006412}
6413
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006414void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006415 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006416 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01006417 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006418 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006419 case TypeCheckKind::kExactCheck:
6420 case TypeCheckKind::kAbstractClassCheck:
6421 case TypeCheckKind::kClassHierarchyCheck:
6422 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006423 call_kind =
6424 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01006425 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006426 break;
6427 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006428 case TypeCheckKind::kUnresolvedCheck:
6429 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006430 call_kind = LocationSummary::kCallOnSlowPath;
6431 break;
6432 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006433
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006434 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01006435 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006436 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006437 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006438 locations->SetInAt(0, Location::RequiresRegister());
6439 locations->SetInAt(1, Location::Any());
6440 // Note that TypeCheckSlowPathX86 uses this "out" register too.
6441 locations->SetOut(Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006442 // When read barriers are enabled, we need a temporary register for some cases.
6443 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006444}
6445
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006446void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006447 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006448 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006449 Location obj_loc = locations->InAt(0);
6450 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006451 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006452 Location out_loc = locations->Out();
6453 Register out = out_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006454 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
6455 DCHECK_LE(num_temps, 1u);
6456 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006457 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006458 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6459 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6460 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07006461 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006462 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006463
6464 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006465 // Avoid null check if we know obj is not null.
6466 if (instruction->MustDoNullCheck()) {
6467 __ testl(obj, obj);
6468 __ j(kEqual, &zero);
6469 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006470
Roland Levillain7c1559a2015-12-15 10:55:36 +00006471 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006472 case TypeCheckKind::kExactCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006473 // /* HeapReference<Class> */ out = obj->klass_
6474 GenerateReferenceLoadTwoRegisters(instruction,
6475 out_loc,
6476 obj_loc,
6477 class_offset,
6478 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006479 if (cls.IsRegister()) {
6480 __ cmpl(out, cls.AsRegister<Register>());
6481 } else {
6482 DCHECK(cls.IsStackSlot()) << cls;
6483 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6484 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006485
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006486 // Classes must be equal for the instanceof to succeed.
6487 __ j(kNotEqual, &zero);
6488 __ movl(out, Immediate(1));
6489 __ jmp(&done);
6490 break;
6491 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006492
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006493 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006494 // /* HeapReference<Class> */ out = obj->klass_
6495 GenerateReferenceLoadTwoRegisters(instruction,
6496 out_loc,
6497 obj_loc,
6498 class_offset,
6499 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006500 // If the class is abstract, we eagerly fetch the super class of the
6501 // object to avoid doing a comparison we know will fail.
6502 NearLabel loop;
6503 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006504 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006505 GenerateReferenceLoadOneRegister(instruction,
6506 out_loc,
6507 super_offset,
6508 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006509 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006510 __ testl(out, out);
6511 // If `out` is null, we use it for the result, and jump to `done`.
6512 __ j(kEqual, &done);
6513 if (cls.IsRegister()) {
6514 __ cmpl(out, cls.AsRegister<Register>());
6515 } else {
6516 DCHECK(cls.IsStackSlot()) << cls;
6517 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6518 }
6519 __ j(kNotEqual, &loop);
6520 __ movl(out, Immediate(1));
6521 if (zero.IsLinked()) {
6522 __ jmp(&done);
6523 }
6524 break;
6525 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006526
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006527 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006528 // /* HeapReference<Class> */ out = obj->klass_
6529 GenerateReferenceLoadTwoRegisters(instruction,
6530 out_loc,
6531 obj_loc,
6532 class_offset,
6533 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006534 // Walk over the class hierarchy to find a match.
6535 NearLabel loop, success;
6536 __ Bind(&loop);
6537 if (cls.IsRegister()) {
6538 __ cmpl(out, cls.AsRegister<Register>());
6539 } else {
6540 DCHECK(cls.IsStackSlot()) << cls;
6541 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6542 }
6543 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006544 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006545 GenerateReferenceLoadOneRegister(instruction,
6546 out_loc,
6547 super_offset,
6548 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006549 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006550 __ testl(out, out);
6551 __ j(kNotEqual, &loop);
6552 // If `out` is null, we use it for the result, and jump to `done`.
6553 __ jmp(&done);
6554 __ Bind(&success);
6555 __ movl(out, Immediate(1));
6556 if (zero.IsLinked()) {
6557 __ jmp(&done);
6558 }
6559 break;
6560 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006561
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006562 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006563 // /* HeapReference<Class> */ out = obj->klass_
6564 GenerateReferenceLoadTwoRegisters(instruction,
6565 out_loc,
6566 obj_loc,
6567 class_offset,
6568 kCompilerReadBarrierOption);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006569 // Do an exact check.
6570 NearLabel exact_check;
6571 if (cls.IsRegister()) {
6572 __ cmpl(out, cls.AsRegister<Register>());
6573 } else {
6574 DCHECK(cls.IsStackSlot()) << cls;
6575 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6576 }
6577 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006578 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006579 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006580 GenerateReferenceLoadOneRegister(instruction,
6581 out_loc,
6582 component_offset,
6583 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006584 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006585 __ testl(out, out);
6586 // If `out` is null, we use it for the result, and jump to `done`.
6587 __ j(kEqual, &done);
6588 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
6589 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006590 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006591 __ movl(out, Immediate(1));
6592 __ jmp(&done);
6593 break;
6594 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006595
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006596 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006597 // No read barrier since the slow path will retry upon failure.
6598 // /* HeapReference<Class> */ out = obj->klass_
6599 GenerateReferenceLoadTwoRegisters(instruction,
6600 out_loc,
6601 obj_loc,
6602 class_offset,
6603 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006604 if (cls.IsRegister()) {
6605 __ cmpl(out, cls.AsRegister<Register>());
6606 } else {
6607 DCHECK(cls.IsStackSlot()) << cls;
6608 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6609 }
6610 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006611 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6612 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006613 codegen_->AddSlowPath(slow_path);
6614 __ j(kNotEqual, slow_path->GetEntryLabel());
6615 __ movl(out, Immediate(1));
6616 if (zero.IsLinked()) {
6617 __ jmp(&done);
6618 }
6619 break;
6620 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006621
Calin Juravle98893e12015-10-02 21:05:03 +01006622 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006623 case TypeCheckKind::kInterfaceCheck: {
6624 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006625 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00006626 // cases.
6627 //
6628 // We cannot directly call the InstanceofNonTrivial runtime
6629 // entry point without resorting to a type checking slow path
6630 // here (i.e. by calling InvokeRuntime directly), as it would
6631 // require to assign fixed registers for the inputs of this
6632 // HInstanceOf instruction (following the runtime calling
6633 // convention), which might be cluttered by the potential first
6634 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006635 //
6636 // TODO: Introduce a new runtime entry point taking the object
6637 // to test (instead of its class) as argument, and let it deal
6638 // with the read barrier issues. This will let us refactor this
6639 // case of the `switch` code as it was previously (with a direct
6640 // call to the runtime not using a type checking slow path).
6641 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006642 DCHECK(locations->OnlyCallsOnSlowPath());
6643 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6644 /* is_fatal */ false);
6645 codegen_->AddSlowPath(slow_path);
6646 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006647 if (zero.IsLinked()) {
6648 __ jmp(&done);
6649 }
6650 break;
6651 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006652 }
6653
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006654 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006655 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006656 __ xorl(out, out);
6657 }
6658
6659 if (done.IsLinked()) {
6660 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006661 }
6662
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006663 if (slow_path != nullptr) {
6664 __ Bind(slow_path->GetExitLabel());
6665 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006666}
6667
Mathieu Chartier5ac321b2016-11-09 16:33:54 -08006668static bool IsTypeCheckSlowPathFatal(TypeCheckKind type_check_kind, bool throws_into_catch) {
6669 switch (type_check_kind) {
6670 case TypeCheckKind::kExactCheck:
6671 case TypeCheckKind::kAbstractClassCheck:
6672 case TypeCheckKind::kClassHierarchyCheck:
6673 case TypeCheckKind::kArrayObjectCheck:
6674 return !throws_into_catch && !kEmitCompilerReadBarrier;
6675 case TypeCheckKind::kInterfaceCheck:
6676 return !throws_into_catch && !kEmitCompilerReadBarrier && !kPoisonHeapReferences;
6677 case TypeCheckKind::kArrayCheck:
6678 case TypeCheckKind::kUnresolvedCheck:
6679 return false;
6680 }
6681 LOG(FATAL) << "Unreachable";
6682 UNREACHABLE();
6683}
6684
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006685void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006686 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006687 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Mathieu Chartier5ac321b2016-11-09 16:33:54 -08006688 LocationSummary::CallKind call_kind =
6689 IsTypeCheckSlowPathFatal(type_check_kind, throws_into_catch)
6690 ? LocationSummary::kNoCall
6691 : LocationSummary::kCallOnSlowPath;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006692 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6693 locations->SetInAt(0, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006694 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
6695 // Require a register for the interface check since there is a loop that compares the class to
6696 // a memory address.
6697 locations->SetInAt(1, Location::RequiresRegister());
6698 } else {
6699 locations->SetInAt(1, Location::Any());
6700 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006701 // Note that TypeCheckSlowPathX86 uses this "temp" register too.
6702 locations->AddTemp(Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006703 // When read barriers are enabled, we need an additional temporary register for some cases.
6704 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
6705}
6706
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006707void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006708 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006709 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006710 Location obj_loc = locations->InAt(0);
6711 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006712 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006713 Location temp_loc = locations->GetTemp(0);
6714 Register temp = temp_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006715 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
6716 DCHECK_GE(num_temps, 1u);
6717 DCHECK_LE(num_temps, 2u);
6718 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
6719 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6720 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6721 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6722 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
6723 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
6724 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
6725 const uint32_t object_array_data_offset =
6726 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006727
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006728 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
6729 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
6730 // read barriers is done for performance and code size reasons.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006731 bool is_type_check_slow_path_fatal =
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006732 IsTypeCheckSlowPathFatal(type_check_kind, instruction->CanThrowIntoCatchBlock());
6733
Roland Levillain0d5a2812015-11-13 10:07:31 +00006734 SlowPathCode* type_check_slow_path =
6735 new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6736 is_type_check_slow_path_fatal);
6737 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006738
Roland Levillain0d5a2812015-11-13 10:07:31 +00006739 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006740 // Avoid null check if we know obj is not null.
6741 if (instruction->MustDoNullCheck()) {
6742 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006743 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006744 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006745
Roland Levillain0d5a2812015-11-13 10:07:31 +00006746 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006747 case TypeCheckKind::kExactCheck:
6748 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006749 // /* HeapReference<Class> */ temp = obj->klass_
6750 GenerateReferenceLoadTwoRegisters(instruction,
6751 temp_loc,
6752 obj_loc,
6753 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006754 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006755
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006756 if (cls.IsRegister()) {
6757 __ cmpl(temp, cls.AsRegister<Register>());
6758 } else {
6759 DCHECK(cls.IsStackSlot()) << cls;
6760 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6761 }
6762 // Jump to slow path for throwing the exception or doing a
6763 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006764 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006765 break;
6766 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006767
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006768 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006769 // /* HeapReference<Class> */ temp = obj->klass_
6770 GenerateReferenceLoadTwoRegisters(instruction,
6771 temp_loc,
6772 obj_loc,
6773 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006774 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006775
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006776 // If the class is abstract, we eagerly fetch the super class of the
6777 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006778 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006779 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006780 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006781 GenerateReferenceLoadOneRegister(instruction,
6782 temp_loc,
6783 super_offset,
6784 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006785 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006786
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006787 // If the class reference currently in `temp` is null, jump to the slow path to throw the
6788 // exception.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006789 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006790 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006791
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006792 // Otherwise, compare the classes
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006793 if (cls.IsRegister()) {
6794 __ cmpl(temp, cls.AsRegister<Register>());
6795 } else {
6796 DCHECK(cls.IsStackSlot()) << cls;
6797 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6798 }
6799 __ j(kNotEqual, &loop);
6800 break;
6801 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006802
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006803 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006804 // /* HeapReference<Class> */ temp = obj->klass_
6805 GenerateReferenceLoadTwoRegisters(instruction,
6806 temp_loc,
6807 obj_loc,
6808 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006809 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006810
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006811 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006812 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006813 __ Bind(&loop);
6814 if (cls.IsRegister()) {
6815 __ cmpl(temp, cls.AsRegister<Register>());
6816 } else {
6817 DCHECK(cls.IsStackSlot()) << cls;
6818 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6819 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006820 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006821
Roland Levillain0d5a2812015-11-13 10:07:31 +00006822 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006823 GenerateReferenceLoadOneRegister(instruction,
6824 temp_loc,
6825 super_offset,
6826 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006827 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006828
6829 // If the class reference currently in `temp` is not null, jump
6830 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006831 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006832 __ j(kNotZero, &loop);
6833 // Otherwise, jump to the slow path to throw the exception.;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006834 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006835 break;
6836 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006837
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006838 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006839 // /* HeapReference<Class> */ temp = obj->klass_
6840 GenerateReferenceLoadTwoRegisters(instruction,
6841 temp_loc,
6842 obj_loc,
6843 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006844 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006845
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006846 // Do an exact check.
6847 if (cls.IsRegister()) {
6848 __ cmpl(temp, cls.AsRegister<Register>());
6849 } else {
6850 DCHECK(cls.IsStackSlot()) << cls;
6851 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6852 }
6853 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006854
6855 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006856 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006857 GenerateReferenceLoadOneRegister(instruction,
6858 temp_loc,
6859 component_offset,
6860 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006861 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006862
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006863 // If the component type is null (i.e. the object not an array), jump to the slow path to
6864 // throw the exception. Otherwise proceed with the check.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006865 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006866 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006867
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006868 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006869 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006870 break;
6871 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006872
Calin Juravle98893e12015-10-02 21:05:03 +01006873 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006874 // We always go into the type check slow path for the unresolved check case.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006875 // We cannot directly call the CheckCast runtime entry point
6876 // without resorting to a type checking slow path here (i.e. by
6877 // calling InvokeRuntime directly), as it would require to
6878 // assign fixed registers for the inputs of this HInstanceOf
6879 // instruction (following the runtime calling convention), which
6880 // might be cluttered by the potential first read barrier
6881 // emission at the beginning of this method.
6882 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006883 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006884
6885 case TypeCheckKind::kInterfaceCheck: {
6886 // Fast path for the interface check. Since we compare with a memory location in the inner
6887 // loop we would need to have cls poisoned. However unpoisoning cls would reset the
6888 // conditional flags and cause the conditional jump to be incorrect. Therefore we just jump
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006889 // to the slow path if we are running under poisoning.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006890 if (!kPoisonHeapReferences) {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006891 // Try to avoid read barriers to improve the fast path. We can not get false positives by
6892 // doing this.
6893 // /* HeapReference<Class> */ temp = obj->klass_
6894 GenerateReferenceLoadTwoRegisters(instruction,
6895 temp_loc,
6896 obj_loc,
6897 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006898 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006899
6900 // /* HeapReference<Class> */ temp = temp->iftable_
6901 GenerateReferenceLoadTwoRegisters(instruction,
6902 temp_loc,
6903 temp_loc,
6904 iftable_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006905 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08006906 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006907 __ movl(maybe_temp2_loc.AsRegister<Register>(), Address(temp, array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08006908 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006909 NearLabel start_loop;
6910 __ Bind(&start_loop);
Mathieu Chartier6beced42016-11-15 15:51:31 -08006911 // Need to subtract first to handle the empty array case.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006912 __ subl(maybe_temp2_loc.AsRegister<Register>(), Immediate(2));
Mathieu Chartier6beced42016-11-15 15:51:31 -08006913 __ j(kNegative, type_check_slow_path->GetEntryLabel());
6914 // Go to next interface if the classes do not match.
6915 __ cmpl(cls.AsRegister<Register>(),
6916 CodeGeneratorX86::ArrayAddress(temp,
6917 maybe_temp2_loc,
6918 TIMES_4,
6919 object_array_data_offset));
6920 __ j(kNotEqual, &start_loop);
6921 } else {
6922 __ jmp(type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006923 }
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006924 break;
6925 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006926 }
6927 __ Bind(&done);
6928
Roland Levillain0d5a2812015-11-13 10:07:31 +00006929 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006930}
6931
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006932void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
6933 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006934 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006935 InvokeRuntimeCallingConvention calling_convention;
6936 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6937}
6938
6939void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006940 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject
6941 : kQuickUnlockObject,
Alexandre Rames8158f282015-08-07 10:26:17 +01006942 instruction,
Serban Constantinescuba45db02016-07-12 22:53:02 +01006943 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006944 if (instruction->IsEnter()) {
6945 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6946 } else {
6947 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6948 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006949}
6950
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006951void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6952void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6953void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6954
6955void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6956 LocationSummary* locations =
6957 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6958 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6959 || instruction->GetResultType() == Primitive::kPrimLong);
6960 locations->SetInAt(0, Location::RequiresRegister());
6961 locations->SetInAt(1, Location::Any());
6962 locations->SetOut(Location::SameAsFirstInput());
6963}
6964
6965void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
6966 HandleBitwiseOperation(instruction);
6967}
6968
6969void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
6970 HandleBitwiseOperation(instruction);
6971}
6972
6973void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
6974 HandleBitwiseOperation(instruction);
6975}
6976
6977void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6978 LocationSummary* locations = instruction->GetLocations();
6979 Location first = locations->InAt(0);
6980 Location second = locations->InAt(1);
6981 DCHECK(first.Equals(locations->Out()));
6982
6983 if (instruction->GetResultType() == Primitive::kPrimInt) {
6984 if (second.IsRegister()) {
6985 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006986 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006987 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006988 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006989 } else {
6990 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006991 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006992 }
6993 } else if (second.IsConstant()) {
6994 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006995 __ andl(first.AsRegister<Register>(),
6996 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006997 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006998 __ orl(first.AsRegister<Register>(),
6999 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007000 } else {
7001 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00007002 __ xorl(first.AsRegister<Register>(),
7003 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007004 }
7005 } else {
7006 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007007 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007008 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007009 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007010 } else {
7011 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00007012 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007013 }
7014 }
7015 } else {
7016 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
7017 if (second.IsRegisterPair()) {
7018 if (instruction->IsAnd()) {
7019 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7020 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7021 } else if (instruction->IsOr()) {
7022 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7023 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7024 } else {
7025 DCHECK(instruction->IsXor());
7026 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7027 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7028 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007029 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007030 if (instruction->IsAnd()) {
7031 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7032 __ andl(first.AsRegisterPairHigh<Register>(),
7033 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7034 } else if (instruction->IsOr()) {
7035 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7036 __ orl(first.AsRegisterPairHigh<Register>(),
7037 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7038 } else {
7039 DCHECK(instruction->IsXor());
7040 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7041 __ xorl(first.AsRegisterPairHigh<Register>(),
7042 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7043 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007044 } else {
7045 DCHECK(second.IsConstant()) << second;
7046 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007047 int32_t low_value = Low32Bits(value);
7048 int32_t high_value = High32Bits(value);
7049 Immediate low(low_value);
7050 Immediate high(high_value);
7051 Register first_low = first.AsRegisterPairLow<Register>();
7052 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007053 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007054 if (low_value == 0) {
7055 __ xorl(first_low, first_low);
7056 } else if (low_value != -1) {
7057 __ andl(first_low, low);
7058 }
7059 if (high_value == 0) {
7060 __ xorl(first_high, first_high);
7061 } else if (high_value != -1) {
7062 __ andl(first_high, high);
7063 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007064 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007065 if (low_value != 0) {
7066 __ orl(first_low, low);
7067 }
7068 if (high_value != 0) {
7069 __ orl(first_high, high);
7070 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007071 } else {
7072 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007073 if (low_value != 0) {
7074 __ xorl(first_low, low);
7075 }
7076 if (high_value != 0) {
7077 __ xorl(first_high, high);
7078 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007079 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007080 }
7081 }
7082}
7083
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007084void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(
7085 HInstruction* instruction,
7086 Location out,
7087 uint32_t offset,
7088 Location maybe_temp,
7089 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007090 Register out_reg = out.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007091 if (read_barrier_option == kWithReadBarrier) {
7092 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007093 if (kUseBakerReadBarrier) {
7094 // Load with fast path based Baker's read barrier.
7095 // /* HeapReference<Object> */ out = *(out + offset)
7096 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00007097 instruction, out, out_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007098 } else {
7099 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007100 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain7c1559a2015-12-15 10:55:36 +00007101 // in the following move operation, as we will need it for the
7102 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00007103 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007104 __ movl(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007105 // /* HeapReference<Object> */ out = *(out + offset)
7106 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007107 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007108 }
7109 } else {
7110 // Plain load with no read barrier.
7111 // /* HeapReference<Object> */ out = *(out + offset)
7112 __ movl(out_reg, Address(out_reg, offset));
7113 __ MaybeUnpoisonHeapReference(out_reg);
7114 }
7115}
7116
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007117void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(
7118 HInstruction* instruction,
7119 Location out,
7120 Location obj,
7121 uint32_t offset,
7122 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007123 Register out_reg = out.AsRegister<Register>();
7124 Register obj_reg = obj.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007125 if (read_barrier_option == kWithReadBarrier) {
7126 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007127 if (kUseBakerReadBarrier) {
7128 // Load with fast path based Baker's read barrier.
7129 // /* HeapReference<Object> */ out = *(obj + offset)
7130 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00007131 instruction, out, obj_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007132 } else {
7133 // Load with slow path based read barrier.
7134 // /* HeapReference<Object> */ out = *(obj + offset)
7135 __ movl(out_reg, Address(obj_reg, offset));
7136 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
7137 }
7138 } else {
7139 // Plain load with no read barrier.
7140 // /* HeapReference<Object> */ out = *(obj + offset)
7141 __ movl(out_reg, Address(obj_reg, offset));
7142 __ MaybeUnpoisonHeapReference(out_reg);
7143 }
7144}
7145
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007146void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(
7147 HInstruction* instruction,
7148 Location root,
7149 const Address& address,
7150 Label* fixup_label,
7151 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007152 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007153 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07007154 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007155 if (kUseBakerReadBarrier) {
7156 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
7157 // Baker's read barrier are used:
7158 //
Roland Levillaind966ce72017-02-09 16:20:14 +00007159 // root = obj.field;
7160 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
7161 // if (temp != null) {
7162 // root = temp(root)
Roland Levillain7c1559a2015-12-15 10:55:36 +00007163 // }
7164
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007165 // /* GcRoot<mirror::Object> */ root = *address
7166 __ movl(root_reg, address);
7167 if (fixup_label != nullptr) {
7168 __ Bind(fixup_label);
7169 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007170 static_assert(
7171 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
7172 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
7173 "have different sizes.");
7174 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
7175 "art::mirror::CompressedReference<mirror::Object> and int32_t "
7176 "have different sizes.");
7177
Vladimir Marko953437b2016-08-24 08:30:46 +00007178 // Slow path marking the GC root `root`.
7179 SlowPathCode* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007180 instruction, root, /* unpoison_ref_before_marking */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007181 codegen_->AddSlowPath(slow_path);
7182
Roland Levillaind966ce72017-02-09 16:20:14 +00007183 // Test the entrypoint (`Thread::Current()->pReadBarrierMarkReg ## root.reg()`).
7184 const int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +01007185 Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(root.reg());
Roland Levillaind966ce72017-02-09 16:20:14 +00007186 __ fs()->cmpl(Address::Absolute(entry_point_offset), Immediate(0));
7187 // The entrypoint is null when the GC is not marking.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007188 __ j(kNotEqual, slow_path->GetEntryLabel());
7189 __ Bind(slow_path->GetExitLabel());
7190 } else {
7191 // GC root loaded through a slow path for read barriers other
7192 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007193 // /* GcRoot<mirror::Object>* */ root = address
7194 __ leal(root_reg, address);
7195 if (fixup_label != nullptr) {
7196 __ Bind(fixup_label);
7197 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007198 // /* mirror::Object* */ root = root->Read()
7199 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
7200 }
7201 } else {
7202 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007203 // /* GcRoot<mirror::Object> */ root = *address
7204 __ movl(root_reg, address);
7205 if (fixup_label != nullptr) {
7206 __ Bind(fixup_label);
7207 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007208 // Note that GC roots are not affected by heap poisoning, thus we
7209 // do not have to unpoison `root_reg` here.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007210 }
7211}
7212
7213void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
7214 Location ref,
7215 Register obj,
7216 uint32_t offset,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007217 bool needs_null_check) {
7218 DCHECK(kEmitCompilerReadBarrier);
7219 DCHECK(kUseBakerReadBarrier);
7220
7221 // /* HeapReference<Object> */ ref = *(obj + offset)
7222 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007223 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007224}
7225
7226void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
7227 Location ref,
7228 Register obj,
7229 uint32_t data_offset,
7230 Location index,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007231 bool needs_null_check) {
7232 DCHECK(kEmitCompilerReadBarrier);
7233 DCHECK(kUseBakerReadBarrier);
7234
Roland Levillain3d312422016-06-23 13:53:42 +01007235 static_assert(
7236 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
7237 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00007238 // /* HeapReference<Object> */ ref =
7239 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007240 Address src = CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007241 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007242}
7243
7244void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
7245 Location ref,
7246 Register obj,
7247 const Address& src,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007248 bool needs_null_check,
7249 bool always_update_field,
7250 Register* temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007251 DCHECK(kEmitCompilerReadBarrier);
7252 DCHECK(kUseBakerReadBarrier);
7253
7254 // In slow path based read barriers, the read barrier call is
7255 // inserted after the original load. However, in fast path based
7256 // Baker's read barriers, we need to perform the load of
7257 // mirror::Object::monitor_ *before* the original reference load.
7258 // This load-load ordering is required by the read barrier.
7259 // The fast path/slow path (for Baker's algorithm) should look like:
7260 //
7261 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7262 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7263 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007264 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain7c1559a2015-12-15 10:55:36 +00007265 // if (is_gray) {
7266 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7267 // }
7268 //
7269 // Note: the original implementation in ReadBarrier::Barrier is
7270 // slightly more complex as:
7271 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007272 // the high-bits of rb_state, which are expected to be all zeroes
7273 // (we use CodeGeneratorX86::GenerateMemoryBarrier instead here,
7274 // which is a no-op thanks to the x86 memory model);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007275 // - it performs additional checks that we do not do here for
7276 // performance reasons.
7277
7278 Register ref_reg = ref.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +00007279 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7280
Vladimir Marko953437b2016-08-24 08:30:46 +00007281 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007282 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
7283 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko953437b2016-08-24 08:30:46 +00007284 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
7285 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
7286 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
7287
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007288 // if (rb_state == ReadBarrier::GrayState())
Vladimir Marko953437b2016-08-24 08:30:46 +00007289 // ref = ReadBarrier::Mark(ref);
7290 // At this point, just do the "if" and make sure that flags are preserved until the branch.
7291 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain7c1559a2015-12-15 10:55:36 +00007292 if (needs_null_check) {
7293 MaybeRecordImplicitNullCheck(instruction);
7294 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007295
7296 // Load fence to prevent load-load reordering.
7297 // Note that this is a no-op, thanks to the x86 memory model.
7298 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
7299
7300 // The actual reference load.
7301 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00007302 __ movl(ref_reg, src); // Flags are unaffected.
7303
7304 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
7305 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007306 SlowPathCode* slow_path;
7307 if (always_update_field) {
7308 DCHECK(temp != nullptr);
7309 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkAndUpdateFieldSlowPathX86(
7310 instruction, ref, obj, src, /* unpoison_ref_before_marking */ true, *temp);
7311 } else {
7312 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(
7313 instruction, ref, /* unpoison_ref_before_marking */ true);
7314 }
Vladimir Marko953437b2016-08-24 08:30:46 +00007315 AddSlowPath(slow_path);
7316
7317 // We have done the "if" of the gray bit check above, now branch based on the flags.
7318 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain7c1559a2015-12-15 10:55:36 +00007319
7320 // Object* ref = ref_addr->AsMirrorPtr()
7321 __ MaybeUnpoisonHeapReference(ref_reg);
7322
Roland Levillain7c1559a2015-12-15 10:55:36 +00007323 __ Bind(slow_path->GetExitLabel());
7324}
7325
7326void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
7327 Location out,
7328 Location ref,
7329 Location obj,
7330 uint32_t offset,
7331 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007332 DCHECK(kEmitCompilerReadBarrier);
7333
Roland Levillain7c1559a2015-12-15 10:55:36 +00007334 // Insert a slow path based read barrier *after* the reference load.
7335 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007336 // If heap poisoning is enabled, the unpoisoning of the loaded
7337 // reference will be carried out by the runtime within the slow
7338 // path.
7339 //
7340 // Note that `ref` currently does not get unpoisoned (when heap
7341 // poisoning is enabled), which is alright as the `ref` argument is
7342 // not used by the artReadBarrierSlow entry point.
7343 //
7344 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
7345 SlowPathCode* slow_path = new (GetGraph()->GetArena())
7346 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
7347 AddSlowPath(slow_path);
7348
Roland Levillain0d5a2812015-11-13 10:07:31 +00007349 __ jmp(slow_path->GetEntryLabel());
7350 __ Bind(slow_path->GetExitLabel());
7351}
7352
Roland Levillain7c1559a2015-12-15 10:55:36 +00007353void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7354 Location out,
7355 Location ref,
7356 Location obj,
7357 uint32_t offset,
7358 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007359 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007360 // Baker's read barriers shall be handled by the fast path
7361 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
7362 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007363 // If heap poisoning is enabled, unpoisoning will be taken care of
7364 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007365 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007366 } else if (kPoisonHeapReferences) {
7367 __ UnpoisonHeapReference(out.AsRegister<Register>());
7368 }
7369}
7370
Roland Levillain7c1559a2015-12-15 10:55:36 +00007371void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7372 Location out,
7373 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007374 DCHECK(kEmitCompilerReadBarrier);
7375
Roland Levillain7c1559a2015-12-15 10:55:36 +00007376 // Insert a slow path based read barrier *after* the GC root load.
7377 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007378 // Note that GC roots are not affected by heap poisoning, so we do
7379 // not need to do anything special for this here.
7380 SlowPathCode* slow_path =
7381 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86(instruction, out, root);
7382 AddSlowPath(slow_path);
7383
Roland Levillain0d5a2812015-11-13 10:07:31 +00007384 __ jmp(slow_path->GetEntryLabel());
7385 __ Bind(slow_path->GetExitLabel());
7386}
7387
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007388void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007389 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007390 LOG(FATAL) << "Unreachable";
7391}
7392
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007393void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007394 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007395 LOG(FATAL) << "Unreachable";
7396}
7397
Mark Mendellfe57faa2015-09-18 09:26:15 -04007398// Simple implementation of packed switch - generate cascaded compare/jumps.
7399void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7400 LocationSummary* locations =
7401 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7402 locations->SetInAt(0, Location::RequiresRegister());
7403}
7404
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007405void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
7406 int32_t lower_bound,
7407 uint32_t num_entries,
7408 HBasicBlock* switch_block,
7409 HBasicBlock* default_block) {
7410 // Figure out the correct compare values and jump conditions.
7411 // Handle the first compare/branch as a special case because it might
7412 // jump to the default case.
7413 DCHECK_GT(num_entries, 2u);
7414 Condition first_condition;
7415 uint32_t index;
7416 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
7417 if (lower_bound != 0) {
7418 first_condition = kLess;
7419 __ cmpl(value_reg, Immediate(lower_bound));
7420 __ j(first_condition, codegen_->GetLabelOf(default_block));
7421 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007422
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007423 index = 1;
7424 } else {
7425 // Handle all the compare/jumps below.
7426 first_condition = kBelow;
7427 index = 0;
7428 }
7429
7430 // Handle the rest of the compare/jumps.
7431 for (; index + 1 < num_entries; index += 2) {
7432 int32_t compare_to_value = lower_bound + index + 1;
7433 __ cmpl(value_reg, Immediate(compare_to_value));
7434 // Jump to successors[index] if value < case_value[index].
7435 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
7436 // Jump to successors[index + 1] if value == case_value[index + 1].
7437 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
7438 }
7439
7440 if (index != num_entries) {
7441 // There are an odd number of entries. Handle the last one.
7442 DCHECK_EQ(index + 1, num_entries);
7443 __ cmpl(value_reg, Immediate(lower_bound + index));
7444 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007445 }
7446
7447 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007448 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
7449 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007450 }
7451}
7452
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007453void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7454 int32_t lower_bound = switch_instr->GetStartValue();
7455 uint32_t num_entries = switch_instr->GetNumEntries();
7456 LocationSummary* locations = switch_instr->GetLocations();
7457 Register value_reg = locations->InAt(0).AsRegister<Register>();
7458
7459 GenPackedSwitchWithCompares(value_reg,
7460 lower_bound,
7461 num_entries,
7462 switch_instr->GetBlock(),
7463 switch_instr->GetDefaultBlock());
7464}
7465
Mark Mendell805b3b52015-09-18 14:10:29 -04007466void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7467 LocationSummary* locations =
7468 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7469 locations->SetInAt(0, Location::RequiresRegister());
7470
7471 // Constant area pointer.
7472 locations->SetInAt(1, Location::RequiresRegister());
7473
7474 // And the temporary we need.
7475 locations->AddTemp(Location::RequiresRegister());
7476}
7477
7478void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7479 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007480 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04007481 LocationSummary* locations = switch_instr->GetLocations();
7482 Register value_reg = locations->InAt(0).AsRegister<Register>();
7483 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7484
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007485 if (num_entries <= kPackedSwitchJumpTableThreshold) {
7486 GenPackedSwitchWithCompares(value_reg,
7487 lower_bound,
7488 num_entries,
7489 switch_instr->GetBlock(),
7490 default_block);
7491 return;
7492 }
7493
Mark Mendell805b3b52015-09-18 14:10:29 -04007494 // Optimizing has a jump area.
7495 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7496 Register constant_area = locations->InAt(1).AsRegister<Register>();
7497
7498 // Remove the bias, if needed.
7499 if (lower_bound != 0) {
7500 __ leal(temp_reg, Address(value_reg, -lower_bound));
7501 value_reg = temp_reg;
7502 }
7503
7504 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007505 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04007506 __ cmpl(value_reg, Immediate(num_entries - 1));
7507 __ j(kAbove, codegen_->GetLabelOf(default_block));
7508
7509 // We are in the range of the table.
7510 // Load (target-constant_area) from the jump table, indexing by the value.
7511 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
7512
7513 // Compute the actual target address by adding in constant_area.
7514 __ addl(temp_reg, constant_area);
7515
7516 // And jump.
7517 __ jmp(temp_reg);
7518}
7519
Mark Mendell0616ae02015-04-17 12:49:27 -04007520void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
7521 HX86ComputeBaseMethodAddress* insn) {
7522 LocationSummary* locations =
7523 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7524 locations->SetOut(Location::RequiresRegister());
7525}
7526
7527void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
7528 HX86ComputeBaseMethodAddress* insn) {
7529 LocationSummary* locations = insn->GetLocations();
7530 Register reg = locations->Out().AsRegister<Register>();
7531
7532 // Generate call to next instruction.
7533 Label next_instruction;
7534 __ call(&next_instruction);
7535 __ Bind(&next_instruction);
7536
7537 // Remember this offset for later use with constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007538 codegen_->AddMethodAddressOffset(insn, GetAssembler()->CodeSize());
Mark Mendell0616ae02015-04-17 12:49:27 -04007539
7540 // Grab the return address off the stack.
7541 __ popl(reg);
7542}
7543
7544void LocationsBuilderX86::VisitX86LoadFromConstantTable(
7545 HX86LoadFromConstantTable* insn) {
7546 LocationSummary* locations =
7547 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7548
7549 locations->SetInAt(0, Location::RequiresRegister());
7550 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
7551
7552 // If we don't need to be materialized, we only need the inputs to be set.
David Brazdilb3e773e2016-01-26 11:28:37 +00007553 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007554 return;
7555 }
7556
7557 switch (insn->GetType()) {
7558 case Primitive::kPrimFloat:
7559 case Primitive::kPrimDouble:
7560 locations->SetOut(Location::RequiresFpuRegister());
7561 break;
7562
7563 case Primitive::kPrimInt:
7564 locations->SetOut(Location::RequiresRegister());
7565 break;
7566
7567 default:
7568 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7569 }
7570}
7571
7572void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
David Brazdilb3e773e2016-01-26 11:28:37 +00007573 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007574 return;
7575 }
7576
7577 LocationSummary* locations = insn->GetLocations();
7578 Location out = locations->Out();
7579 Register const_area = locations->InAt(0).AsRegister<Register>();
7580 HConstant *value = insn->GetConstant();
7581
7582 switch (insn->GetType()) {
7583 case Primitive::kPrimFloat:
7584 __ movss(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007585 codegen_->LiteralFloatAddress(
7586 value->AsFloatConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04007587 break;
7588
7589 case Primitive::kPrimDouble:
7590 __ movsd(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007591 codegen_->LiteralDoubleAddress(
7592 value->AsDoubleConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04007593 break;
7594
7595 case Primitive::kPrimInt:
7596 __ movl(out.AsRegister<Register>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007597 codegen_->LiteralInt32Address(
7598 value->AsIntConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04007599 break;
7600
7601 default:
7602 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7603 }
7604}
7605
Mark Mendell0616ae02015-04-17 12:49:27 -04007606/**
7607 * Class to handle late fixup of offsets into constant area.
7608 */
Vladimir Marko5233f932015-09-29 19:01:15 +01007609class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04007610 public:
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007611 RIPFixup(CodeGeneratorX86& codegen,
7612 HX86ComputeBaseMethodAddress* base_method_address,
7613 size_t offset)
7614 : codegen_(&codegen),
7615 base_method_address_(base_method_address),
7616 offset_into_constant_area_(offset) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04007617
7618 protected:
7619 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
7620
7621 CodeGeneratorX86* codegen_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007622 HX86ComputeBaseMethodAddress* base_method_address_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007623
7624 private:
7625 void Process(const MemoryRegion& region, int pos) OVERRIDE {
7626 // Patch the correct offset for the instruction. The place to patch is the
7627 // last 4 bytes of the instruction.
7628 // The value to patch is the distance from the offset in the constant area
7629 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04007630 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007631 int32_t relative_position =
7632 constant_offset - codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell0616ae02015-04-17 12:49:27 -04007633
7634 // Patch in the right value.
7635 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
7636 }
7637
Mark Mendell0616ae02015-04-17 12:49:27 -04007638 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04007639 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007640};
7641
Mark Mendell805b3b52015-09-18 14:10:29 -04007642/**
7643 * Class to handle late fixup of offsets to a jump table that will be created in the
7644 * constant area.
7645 */
7646class JumpTableRIPFixup : public RIPFixup {
7647 public:
7648 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007649 : RIPFixup(codegen, switch_instr->GetBaseMethodAddress(), static_cast<size_t>(-1)),
7650 switch_instr_(switch_instr) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04007651
7652 void CreateJumpTable() {
7653 X86Assembler* assembler = codegen_->GetAssembler();
7654
7655 // Ensure that the reference to the jump table has the correct offset.
7656 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
7657 SetOffset(offset_in_constant_table);
7658
7659 // The label values in the jump table are computed relative to the
7660 // instruction addressing the constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007661 const int32_t relative_offset = codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell805b3b52015-09-18 14:10:29 -04007662
7663 // Populate the jump table with the correct values for the jump table.
7664 int32_t num_entries = switch_instr_->GetNumEntries();
7665 HBasicBlock* block = switch_instr_->GetBlock();
7666 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
7667 // The value that we want is the target offset - the position of the table.
7668 for (int32_t i = 0; i < num_entries; i++) {
7669 HBasicBlock* b = successors[i];
7670 Label* l = codegen_->GetLabelOf(b);
7671 DCHECK(l->IsBound());
7672 int32_t offset_to_block = l->Position() - relative_offset;
7673 assembler->AppendInt32(offset_to_block);
7674 }
7675 }
7676
7677 private:
7678 const HX86PackedSwitch* switch_instr_;
7679};
7680
7681void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
7682 // Generate the constant area if needed.
7683 X86Assembler* assembler = GetAssembler();
7684 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
7685 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
7686 // byte values.
7687 assembler->Align(4, 0);
7688 constant_area_start_ = assembler->CodeSize();
7689
7690 // Populate any jump tables.
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007691 for (JumpTableRIPFixup* jump_table : fixups_to_jump_tables_) {
Mark Mendell805b3b52015-09-18 14:10:29 -04007692 jump_table->CreateJumpTable();
7693 }
7694
7695 // And now add the constant area to the generated code.
7696 assembler->AddConstantArea();
7697 }
7698
7699 // And finish up.
7700 CodeGenerator::Finalize(allocator);
7701}
7702
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007703Address CodeGeneratorX86::LiteralDoubleAddress(double v,
7704 HX86ComputeBaseMethodAddress* method_base,
7705 Register reg) {
7706 AssemblerFixup* fixup =
7707 new (GetGraph()->GetArena()) RIPFixup(*this, method_base, __ AddDouble(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04007708 return Address(reg, kDummy32BitOffset, fixup);
7709}
7710
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007711Address CodeGeneratorX86::LiteralFloatAddress(float v,
7712 HX86ComputeBaseMethodAddress* method_base,
7713 Register reg) {
7714 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, method_base, __ AddFloat(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04007715 return Address(reg, kDummy32BitOffset, fixup);
7716}
7717
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007718Address CodeGeneratorX86::LiteralInt32Address(int32_t v,
7719 HX86ComputeBaseMethodAddress* method_base,
7720 Register reg) {
7721 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, method_base, __ AddInt32(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04007722 return Address(reg, kDummy32BitOffset, fixup);
7723}
7724
Nicolas Geoffray133719e2017-01-22 15:44:39 +00007725Address CodeGeneratorX86::LiteralInt64Address(int64_t v,
7726 HX86ComputeBaseMethodAddress* method_base,
7727 Register reg) {
7728 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, method_base, __ AddInt64(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04007729 return Address(reg, kDummy32BitOffset, fixup);
7730}
7731
Aart Bika19616e2016-02-01 18:57:58 -08007732void CodeGeneratorX86::Load32BitValue(Register dest, int32_t value) {
7733 if (value == 0) {
7734 __ xorl(dest, dest);
7735 } else {
7736 __ movl(dest, Immediate(value));
7737 }
7738}
7739
7740void CodeGeneratorX86::Compare32BitValue(Register dest, int32_t value) {
7741 if (value == 0) {
7742 __ testl(dest, dest);
7743 } else {
7744 __ cmpl(dest, Immediate(value));
7745 }
7746}
7747
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007748void CodeGeneratorX86::GenerateIntCompare(Location lhs, Location rhs) {
7749 Register lhs_reg = lhs.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07007750 GenerateIntCompare(lhs_reg, rhs);
7751}
7752
7753void CodeGeneratorX86::GenerateIntCompare(Register lhs, Location rhs) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007754 if (rhs.IsConstant()) {
7755 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
jessicahandojo4877b792016-09-08 19:49:13 -07007756 Compare32BitValue(lhs, value);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007757 } else if (rhs.IsStackSlot()) {
jessicahandojo4877b792016-09-08 19:49:13 -07007758 __ cmpl(lhs, Address(ESP, rhs.GetStackIndex()));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007759 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07007760 __ cmpl(lhs, rhs.AsRegister<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007761 }
7762}
7763
7764Address CodeGeneratorX86::ArrayAddress(Register obj,
7765 Location index,
7766 ScaleFactor scale,
7767 uint32_t data_offset) {
7768 return index.IsConstant() ?
7769 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << scale) + data_offset) :
7770 Address(obj, index.AsRegister<Register>(), scale, data_offset);
7771}
7772
Mark Mendell805b3b52015-09-18 14:10:29 -04007773Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
7774 Register reg,
7775 Register value) {
7776 // Create a fixup to be used to create and address the jump table.
7777 JumpTableRIPFixup* table_fixup =
7778 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
7779
7780 // We have to populate the jump tables.
7781 fixups_to_jump_tables_.push_back(table_fixup);
7782
7783 // We want a scaled address, as we are extracting the correct offset from the table.
7784 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
7785}
7786
Andreas Gampe85b62f22015-09-09 13:15:38 -07007787// TODO: target as memory.
7788void CodeGeneratorX86::MoveFromReturnRegister(Location target, Primitive::Type type) {
7789 if (!target.IsValid()) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007790 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007791 return;
7792 }
7793
7794 DCHECK_NE(type, Primitive::kPrimVoid);
7795
7796 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
7797 if (target.Equals(return_loc)) {
7798 return;
7799 }
7800
7801 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7802 // with the else branch.
7803 if (type == Primitive::kPrimLong) {
7804 HParallelMove parallel_move(GetGraph()->GetArena());
7805 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), Primitive::kPrimInt, nullptr);
7806 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), Primitive::kPrimInt, nullptr);
7807 GetMoveResolver()->EmitNativeCode(&parallel_move);
7808 } else {
7809 // Let the parallel move resolver take care of all of this.
7810 HParallelMove parallel_move(GetGraph()->GetArena());
7811 parallel_move.AddMove(return_loc, target, type, nullptr);
7812 GetMoveResolver()->EmitNativeCode(&parallel_move);
7813 }
7814}
7815
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007816void CodeGeneratorX86::PatchJitRootUse(uint8_t* code,
7817 const uint8_t* roots_data,
7818 const PatchInfo<Label>& info,
7819 uint64_t index_in_table) const {
7820 uint32_t code_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
7821 uintptr_t address =
7822 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
7823 typedef __attribute__((__aligned__(1))) uint32_t unaligned_uint32_t;
7824 reinterpret_cast<unaligned_uint32_t*>(code + code_offset)[0] =
7825 dchecked_integral_cast<uint32_t>(address);
7826}
7827
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007828void CodeGeneratorX86::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
7829 for (const PatchInfo<Label>& info : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007830 const auto it = jit_string_roots_.find(
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007831 StringReference(&info.dex_file, dex::StringIndex(info.index)));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007832 DCHECK(it != jit_string_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007833 uint64_t index_in_table = it->second;
7834 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007835 }
7836
7837 for (const PatchInfo<Label>& info : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007838 const auto it = jit_class_roots_.find(
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007839 TypeReference(&info.dex_file, dex::TypeIndex(info.index)));
7840 DCHECK(it != jit_class_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01007841 uint64_t index_in_table = it->second;
7842 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007843 }
7844}
7845
Roland Levillain4d027112015-07-01 15:41:14 +01007846#undef __
7847
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007848} // namespace x86
7849} // namespace art